Binary_Search_Tree_Algorithms_Flashcards
(5 cards)
How does a binary search tree (BST) work?
Each node has at most two children; the left child contains smaller values, the right child contains larger values.
What is the main advantage of using a binary search tree?
It allows efficient searching, insertion, and deletion with average time complexity of O(log n) if balanced.
What is the difference between searching in a BST and an array?
BST uses node comparisons and has average O(log n) complexity; array search is linear (O(n)) unless it’s a binary search on a sorted array.
What are the steps to insert a value into a binary search tree?
Compare the value to the current node, go left if smaller or right if larger, and repeat until an empty position is found.
What are the steps to remove a node from a binary search tree?
Handle three cases: node has no children (remove directly), one child (replace with child), or two children (replace with in-order successor or predecessor).