Binary Search Trees CSE 331 Section 2 James Daly
Reminder Project1 is due on Friday Turn in with Handin Link on class page
Review: Binary Tree Every node has at most 2 children Left and right child Variation: n-ary trees have at most n children
Review: Binary Search Tree For every node Left descendents smaller (l ≤ k) Right descendents bigger (r ≥ k) k <k>k
Find(t, x) If (t = null) null Else If (x < t.data) Find(t.left, x) Else If (x > t.data) Find(t.right, x) Else t ?
Insert(t, x) If (t = null) t = new Node(x) Else If (x < t.data) Insert(t.left, x) Else If (x > t.data) Insert(t.right, x) Construct a BST for 5, 3, 9, 2
Delete: 1 st Case Leaf Node
Delete: 2 nd Case One Child
Delete: 3 rd Case Two children Swap with least successor (or greatest predecessor) Then delete from the right (or left) subtree
Delete: 3 rd Case
Running Times Find, Insert, and Remove all have the same time Dependent on the height of the tree Could be n tall in the worst case. Can limit height to be only O(log n)
AVL Trees Named for Adelson-Velskii & Landis Self-balancing binary search tree Goal: Keep BST in balance Ability to check / track balance
AVL Trees For every node, the height of both subtrees differs by at most 1.
Height(t) : Int If (t = null) Return -1 Else Return 1 + max(Height(t.left), Height(t.right))
IsBalanced(t) : (Boolean, Int) If (t = null) Return true Else (leftBal, leftHeight) ← IsBalanced(t.left) (rightBal, rightHeight) ← IsBalanced(t.right) Return leftBal and rightBal and Abs(leftHeight – rightHeight) ≤ 1
Operations on AVL Trees Find, FindMin, FindMax Work as for normal BSTs O(log n) Insert, Delete Need extra steps Use rotations
Insert Example Single rotation
Insert Example Double rotation
Insert Example Single rotation
Insert Example Double rotation
Four Cases Left subtree of left child Right subtree of left child Right subtree of right child Left subtree of right child
RotateRight(&t) // Rotates t down to the right // Brings up left child left ← t.left temp = left.right left.right ← t t.left ← temp t ← left
Left-Left / Right-Right Case D B a6a6 c5c5 e5e5 B D e5e5 c5c5 a6a6
Left-Right / Right-Left Case F B a5a5 e4e4 g5g5 D c5c5 D B a5a5 c5c5 F e4e4 g5g5 B a5a5 c5c5 e4e4 D F g5g5
Insertion Insert new node Check each of its ancestors to determine whether they are still balanced Most recent ancestor first Do this as you go back up the tree (popping recursive calls) If necessary, rebalance that ancestor
Deletion Delete old node Check each of its ancestors just like for insertion
Red-Black Trees Another type of balanced BST Nodes are colored according to some rules Each node is either red or black All leaves are black and empty Every red node must have two black children Every paths from a node to each of its descendant leaves contain the same number of black nodes Guarantees no leaf path is more than twice that of another
Example
Insertion New nodes are always red with two leaf children If parent is black, we are done If parent and “uncle” are both red Parent and uncle are painted black Grandparent is painted red Recurse upward
Example
Insertion Continued If parent is red and uncle is black If new node is left-right or right-left grand child, rotate it above the parent and swap roles Then rotate the (new) parent above the grandparent and swap colors
Example
5 Example
5 Example
Deletion Swap values as normal if there are two children If node is red, simply move its child up If the node’s child is red, paint it black If both are black Move child up Rebalance upper tree
Next Time Tree Sets B-Trees