Download presentation
Presentation is loading. Please wait.
1
Week 7 - Friday CS221
2
Last time What did we talk about last time? BST deletion 2-3 trees
3
Questions?
4
Project 2
5
2-3 tree practice Add the following keys to a 2-3 tree: 62 11 32 7 45
24 88 25 28 90
6
Red-black trees Student Lecture
7
Red-black trees
8
An interlude in a formicary
One hundred ants are walking along a meter long stick Each ant is traveling either to the left or the right with a constant speed of 1 meter per minute When two ants meet, they bounce off each other and reverse direction When an ant reaches an end of the stick, it falls off Will all the ants fall off? What is the longest amount of time that you would need to wait to guarantee that all ants have fallen off?
9
"As if" On the previous slide, we can look at the problem "as if" ants were passing through each other with no effect This idea of looking at a problem as if it is something else can make solving it easier Coding up a 2-3 tree is annoying (but possible) By creating a data structure that is (somehow) equivalent, we can get the job done in an easier way Sometimes the way we implement an algorithm and the way we analyze it are different
10
Red-black trees A red-black tree is a form of binary search tree
Each node looks like a regular BST node, with one additional piece of information: color A node can either be red or black Null values are considered black The color allows us to simulate a 2-3 tree We can think of a red node is actually part of a 3 node with its parent
11
Red-black tree definition
A red-black tree is a BST with red and black nodes and the following properties: Red nodes lean left from their parents No node has two red children The tree has perfect black balance In other words, every path from the root to a null has the same number of black nodes on the way The length of this path is called the black height of the tree The book describes the link as having a color (which is probably easier to think about), but the color has to be stored in the node
12
2-3 tree implementation How do we implement a 2-3 tree?
Answer: We don't. It is (of course) possible, but it involves having weird 2-nodes and 3-nodes that inherit from some abstract node class or interface It's a huge pain Note that 2-3 trees are essentially a special case of a B-tree, and someone does have to implement those Instead, we use red-black trees which are structurally the same as 2-3 trees (if you squint)
13
2-3 tree mapping to a red-black tree
J A C H L R P S X M E J A C H L R P S X
14
Building the tree We can do an insertion with a red-black tree using a series of rotations and recolors We do a regular BST insert Then, we work back up the tree as the recursion unwinds If the right child is red, we rotate the current node left If the left child is red and the left child of the left child is red, we rotate the current node right If both children are red, we recolor them black and the current node red You have to do all these checks, in order! Multiple rotations can happen It doesn't make sense to have a red root, so we always color the root black after the insert
15
We perform a left rotation when the right child is red
Y X B A C Current Y X B A C Current We perform a left rotation when the right child is red
16
Right rotation Z Y B A D Current X C Z Y B A D Current X C We perform a right rotation when the left child is red and its left child is red
17
Recolor Y X B A D Current Z C Y X B A D Current Z C We recolor both children and the current node when both children are red
18
Red-black tree practice
Add the following keys to a red-black tree: 62 11 32 7 45 24 88 25 28 90
19
Analysis of red-black trees
The height of a red-black tree is no more than 2 log n Find is Θ(height), so find is Θ(log n) Since we only have to go down that path and back up to insert, insert is Θ(log n) Delete in red-black trees is messy, but it is also actually Θ(log n)
20
AVL Trees
21
AVL trees Another approach to balancing is the AVL tree
Named for its inventors G.M. Adelson-Velskii and E.M. Landis Invented in 1962 An AVL tree is better balanced than a red-black tree, but it takes more work to keep such a good balance It's faster for finds (because of the better balance) It's slower for inserts and deletes Like a red-black tree, all operations are Θ(log n)
22
AVL definition An AVL tree is a binary search tree where
The left and right subtrees of the root have heights that differ by at most one The left and right subtrees are also AVL trees
23
AVL tree? 10 6 14 1 9 17 2 7 15
24
Balance factor The balance factor of a tree (or subtree) is the height of its right subtree minus the height of its left In an AVL tree, the balance factor of every subtree is -1, 0, or +1 What’s the balance factor of this tree? 6 1 9 2
25
How do we build an AVL tree?
Carefully. Every time we do an add, we have to make sure that the tree is still balanced There are 4 cases Left Left Right Right Left Right Right Left
26
Left Left 3 2 1 B A C D 3 2 1 B A C D Right Rotation
27
Right Right 2 1 B A C D 3 3 2 1 B A C D Left Rotation
28
Left Rotation + Right Rotation
Left Right 3 2 1 B A C D 3 2 1 B A C D Left Rotation + Right Rotation
29
Right Rotation + Left Rotation
Right Left 2 1 B A C D 3 3 2 1 B A C D Right Rotation + Left Rotation
30
Let’s make an AVL tree! Let’s just add these numbers in order: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 What about these? 7, 24, 92, 32, 2, 57, 67, 84, 66, 75
31
Balancing a Tree by Construction
32
How to make a balanced tree
What if we knew ahead of time which numbers we were going to put into a tree? How could we make sure the tree is balanced? Answer: Sort the numbers Recursively add the numbers such that the tree stays balanced
33
Balanced insertion Write a recursive method that adds a sorted array of data such that the tree stays balanced Assume that an add(int key) method exists Use the usual convention that start is the beginning of a range and end is the location after the last legal element in the range public void balance(int[] data, int start, int end )
34
DSW algorithm Takes a potentially unbalanced tree and turns it into a balanced tree Step 1 Turn the tree into a degenerate tree (backbone or vine) by right rotating any nodes with left children Step 2 Turn the degenerate tree into a balanced tree by doing sequences of left rotations starting at the root The analysis is not obvious, but it takes O(n) total rotations
35
Which method is best? How much time does it take to insert n items with: Red-black or AVL tree Balanced insertion method Unbalanced insertion + DSW rebalance How much space does it take to insert n items with:
36
Self-Adjusting Trees
37
Self-Adjusting Trees Balanced binary trees ensure that accessing any element takes O(log n) time But, maybe only a few elements are accessed repeatedly In this case, it may make more sense to restructure the tree so that these elements are closer to the root
38
Restructuring strategies
Single rotation Rotate a child about its parent if an element in a child is accessed, unless it's the root Moving to the root Repeat the child-parent rotation until the element being accessed is the root
39
Splaying A concept called splaying takes the rotate to the root idea further, doing special rotations depending on the relationship of the child R with its parent Q and grandparent P Cases: Node R's parent is the root Do a single rotation to make R the root Node R is the left child of Q and Q is the left child of P (respectively right and right) Rotate Q around P Rotate R around Q Node R is the left child of Q and Q is the right child of P (respectively right and left) Rotate R around P
40
Danger, Will Robinson! We are not going deeply into self-organizing trees It's an interesting concept, and rigorous mathematical analysis shows that splay trees should perform well, in terms of Big O bounds In practice, however, they usually do not An AVL tree or a red-black tree is generally the better choice If you come upon some special problem where you repeatedly access a particular element most of the time, only then should you consider splay trees
41
Mid-semester Evaluations
42
Upcoming
43
Next time… Hash tables Hashing functions
44
Reminders Read section 3.4 Finish Project 2 Due tonight by midnight
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.