Download presentation
Presentation is loading. Please wait.
1
Binary Search Trees CSE 331 Section 2 James Daly
2
Reminder Project1 is due on Friday Turn in with Handin Link on class page
3
Review: Binary Tree Every node has at most 2 children Left and right child Variation: n-ary trees have at most n children
4
Review: Binary Search Tree For every node Left descendents smaller (l ≤ k) Right descendents bigger (r ≥ k) k <k>k
5
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 4 3 2 8 69 57 7?
6
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) 5 39 2 Construct a BST for 5, 3, 9, 2
7
Delete: 1 st Case Leaf Node 6 57 6 5
8
Delete: 2 nd Case One Child 6788 7
9
Delete: 3 rd Case Two children Swap with least successor (or greatest predecessor) Then delete from the right (or left) subtree
10
Delete: 3 rd Case 428136957
11
528136947
12
528136947
13
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)
14
AVL Trees Named for Adelson-Velskii & Landis Self-balancing binary search tree Goal: Keep BST in balance Ability to check / track balance
15
AVL Trees For every node, the height of both subtrees differs by at most 1.
16
Height(t) : Int If (t = null) Return -1 Else Return 1 + max(Height(t.left), Height(t.right))
17
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
18
Operations on AVL Trees Find, FindMin, FindMax Work as for normal BSTs O(log n) Insert, Delete Need extra steps Use rotations
19
Insert Example 8 7 1 1 8 7 1 Single rotation
20
Insert Example 8 6 7 7 8 7 6 Double rotation
21
Insert Example 6 7 9 9 9 7 6 Single rotation
22
Insert Example 5 9 8 8 9 8 5 Double rotation
23
Four Cases Left subtree of left child Right subtree of left child Right subtree of right child Left subtree of right child
24
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
25
Left-Left / Right-Right Case D B a6a6 c5c5 e5e5 B D e5e5 c5c5 a6a6
26
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
27
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
28
Deletion Delete old node Check each of its ancestors just like for insertion
29
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
30
Example 13 111 1525 6 8 17 22 27
31
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
32
Example 13 111 1525 6 8 17 22 27 14
33
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
34
Example 13 111 1525 6 8 17 22 27 5
35
5 Example 13 111 1525 6 8 17 22 27
36
5 Example 13 1 11 1525 6 8 17 22 27
37
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
38
Next Time Tree Sets B-Trees
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.