Wk 4 Binary Search Trees and Balanced Trees (Red-Black) Flashcards
What is the expected fast search algorithm?
lg n
What is the main difference between a heap and a BST?
heap had no relationship between siblings. Also heap was full, whereas BST could be not full
Why doesn’t a sorted linear array work for a BST?
We would have to go through the entire thing (n) each time we did an add or a drop. Would be too costly. Instead we use pointers, etc.
What are the max and min of a BST?
The max is the right-most right child. The min is the left-most left child.
Is a linked list a BST?
Yes, albeit it an ugly one with each node having degree of one.
How many ways can a BST be created from a particular input?
LOTS.
When do we call a BST balanced?
The difference between the longest and shortest paths is a constant.
What is the algorithm for IN-ORDER WALK? What is its complexity? Why? What is its output?
if not at nil,
INORDERWALK(L)
Print key (this)
INORDERWALK(R)
Each node is visited exactly 3 times.
O(n) assuming the tree is already built. It’s output is the sorted list of keys.
What changes in a pre/post order walk compared to a inorder walk?
only the ordering of the print
What is the complexity of search in a BST? Why?
O(h) where h is the height of the tree. If the tree is balanced, the height should be lg n. Worst case you have to go all the way to the bottom at the leaves.
How does search of a BST relate to insert?
For an insert, I’m essentially searching for a place to insert the value, so its the same algorithm with a write at the end. (if this were already in here, where would it be?)
How would I use a BST to sort random input?
Run an insert at each node. Then at end, do an in-order walk. This would be lg h to add each node for n nodes. plus one in-order walk which would be linear as well. Overall n lg n if balanced, could be as bad as O(n^2) if the tree is a linked list with each node degree of one.
What is analogy of D/S as a spring or having potential energy?
Energy you spend putting things into it, minimizes energy spent taking things out. By spending some energy and putting some in by organizing and/or balancing, it becomes easier to get out later. If you spend no energy organizing and just dump everything in it will take more energy to get out later. Overall, the total energy (of information !?) is net.
What is the successor of a node in a BST?
For a non-leaf, the successor is the min of the right child.
For a leaf, it is move up and find the first right turn.
What is complexity of finding a successor? Why?
lg (h) Could have to do one entire walk of the tree from root to leaf or vice versa.