Lecture 4 - Binary Search, AVL trees & sort Flashcards
Describe the structure of a Binary Search Tree.
X is root,
All nodes on left of X are <= X
All nodes on right of X are >= X
How would you measure the height of a BST recursively?
Height(x) = 1 + max( height(left(x)), height(right(x)) )
What’s the average run time of Search, Insert, Delete in a BST?
Omega(h)
Why is it easier to determine height of tree from the depth of the lowest node?
All we have to do is count the nodes traversing from deepest node to the root.
Define a balanced BST.
A BST where the nodes are well distributed between left and right
Compare the runtime of Height(BST,n) on a completely balanced vs completely unbalanced BST.
h = Omega(logn) vs Omega(n)
Define an AVL tree.
BST such that the heights of the two child subtrees of any node differ by at most one. They are self-balanced BSTs.
What’s the runtime of Insert, Delete & Search in average and worst cases?
O(logn)
Let Nh be the minimum # nodes in an AVL tree of height h. Prove that h = O(logn)
Nh = 1 + Nh-1 + Nh-2
Nh > Omega(2^(h/2))
h = O (logn)
What does a Rotation do in an AVL
They allow you to change the tree structure while preserving the BST property. This allows you to restore an AVL tree after inserting a node that breaks it.
Why do zigzag patterns cause problems when rotating BST trees to form AVLs?
Because they don’t restore the property immediately, we have to remove the zig-zag pattern and put all nodes on one side of the root we’re trying to rotate.
What’s the running time AVL insertion?
O(h) insertion + 2* O(1) rotation = O(logn) insertion in AVL trees.
Compare the worst case runtime of BST sort and AVL sort.
BST sort: O(n^2)
AVL soft: O(n*logn)
How does an inorder traversal print a tree like A / x \ B?
A x B
Describe BST sort on a list of keys.
- Build a BST tree from the list of keys
2. print in-order traversal