Title: Balanced Trees Abs(depth(leftChild)
1Balanced TreesAbs(depth(leftChild)
depth(rightChild)) lt 1Depth of a tree is its
longest path length
- Red-black trees Restructure the tree when rules
among nodes of the tree are violated as we follow
the path from root to the insertion point. - AVL Trees Maintain a three way flag at each
node (-1,0,1) determining whether the left
sub-tree is longer, shorter or the same length.
Restructure the tree when the flag would go to 2
or 2. - Splay Trees Dont require complete balance.
However, N inserts and deletes can be done in
NlgN time. Rotates are done to move accessed
nodes to the top of the tree.
2RotatesAnalyze possible tree depths after rotates
- LL, RR Rotates
- Child node is raised one level
- RL, LR Rotates
- Child node is raised two levels in two steps
- Splay Tree Rotates
- Outer nodes of grandparent nodes are raised two
levels.
3Outer RR and LL rotates
X
Y
C
X
Y
A
A
B
C
B
Note The squares are subtrees, the circles are
nodes
4Inner LR and RL rotates
X
Z
X
Y
Y
A
RL
A
B
Z
D
D
C
B
C
Note LR is the mirror image
5Outer Splay Rotates
Z
X
Y
D
Y
A
C
X
B
Z
A
B
C
D
Note There is also a rotate for the mirror image
6AVL and Splay TreesAdelson-Velskii and Landis
- AVL Insertion Algorithm
- Insert using the same algorithm as the binary
search - Traverse back up the tree and for each node on
the path to the root - Update the balance weight
- IF weight becomes zero, break out of traversal
- IF weight becomes 2 or -2, perform an
appropriate rotation break - AVL Removal Algorithm
- Add the following logic to a standard binary
search tree deletionAfter the deleting node
having less than two children, traverse up the
treeWHILE traversing IF the path to the left
was reduced in length, add 1 to the weight IF
the path to the right was reduced in length,
subtract 1 from the weight IF the tree did
not shrink (new weight 1) THEN BREAK IF the
weight becomes 2 or -2 Find a path in the
sibling direction that we can rotate Perform an
appropriate rotation and adjust weights
7Splay Tree Algorithm
- Splaying a node (bringing it up to the top)
- Find the node and double rotate it to the top.
- A single rotation might be needed at the last
step - Splay Tree Insertion Algorithm
- Perform a normal binary search tree insertion
and then splay - Deletion Algorithm
- Find and Splay node, n, to delete
- Remove n leaving two trees (one with nodes with
smaller keys) - Splay the smallest node in the tree with larger
nodes - Link the root of the tree with smaller nodes to
the root of the tree with the larger nodes
8Red Black Trees
- Red-black trees are tress that conform to the
following rules - Every node is colored (either red or black)
- The root is always black
- If a node is red, its children must be black
- Every path from the root to leaf, or to a null
child, must contain the same number of black
nodes. - During insertions and deletions, these rules must
be maintained
9Red-black Insertion Algorithm
- current node root node
- parent grandParent null
- While current ltgt null
- If current is black, and currents children
are red - Change current to red (If currentltgtroot)
and currents children to black - Call rotateTree()
- grandParent parent
- parent current
- current is set to the child node in the
binary search sequence - If parent is null
- root node to insert color it black
- Else Connect the node to insert to the leaf node
it was instantiated to red - Call rotateTree()
- Link the new node to the parent (or create new
root if there is no parent)
10Rotate Tree() Algorithm
- If current ltgt root and Parent is red
- If current is an outer child of the
grandParent node - Set color of grandParent node to red
- Set color of parent node to black
- Raise current by rotating parent with
grandParent - If current is an inner child of the
grandParent node - Set color of grandParent node to red
- Set color of current to black
- Raise current by rotating current
with parent - Raise current by rotating current
with grandParent node
11Red Black Example
Before adding 99
After adding 99
Note Color change at 92 led to an outer rotation
involving 52, 67, 92
12Red Black Deletion
- A standard Binary Search Tree removal reduces to
removing a node with less than two children - A node with a single child must be black, with a
red child (otherwise there would be either a
black imbalance or two consecutive red nodes). To
delete, simply connect the child to the parent
and change its color black. - Leafs can be red or black. If red, simply remove
it. If black, removal causes an imbalance of
black nodes, which must be restored. - Weapons at our disposal
- Color flips
- Traversal upward
- Rotations
13Red Black Deletion (cont.)
- X, Y, Z represent sub-trees
- Path from P to the left has K black nodes the
path to the right has K1 black nodes - P is the parent node, S is the sibling node (A
sibling must exist, because of step 2) - To restore balance
- IF Head of X is red turn it black
- IFS black with red children rotate
- IF S black with no red children
- IF P red, set P black and S red
- ELSE color S red restoring balance. Traversal up
because both paths now have only K black nodes - IF S red, perform one rotate, two if one or three
of S's grandchildren are red.
14Example
- Case B (Balance Restored)
Note Pparent, Ssibling, G grandchild, Green
node can be either black or red
15Case D Examples
- Case D (An S grandchild is red)
- Case D (An S grandchild is red)
Note These examples require another rotation
because a double red occurs
16Another Case D Example
- Case D (All of S's Grandchildren are Red)
- Balance has been restored
P
S
X
G
G
A
B
C
D
17Which algorithm is best?
- Advantages
- AVL relatively easy to program. Insert requires
only one rotation. - Splay No extra storage, high frequency nodes
near the top - RedBlack Fastest in practice, no traversal back
up the tree on insert - Disadvantages
- AVL Repeated rotations are needed on deletion,
must traverse back up the tree. - SPLAY Can occasionally have O(N) finds, multiple
rotates on every search - RedBlack Multiple rotates on insertion, delete
algorithm difficult to understand and program