Balanced Trees
Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes Need a structure that tends to maintain balance – How? Grow in ‘width’ first, then height Accommodate horizontal growth More data at each level Nodes of two forms –One data member and two children (“Two node”) –Two data members and three children (“Three node”)
2-3 Tree Nodes > L< S S > S< S >S <L S L
BST Insertion
2-3 Tree Insertion (39)
2-3 Tree Insertion (38)
2-3 Tree Insertion (37)
2-3 Tree Insertion (36)
2-3 Tree Insertion (36)
2-3-4 Trees, Red-Black Trees
2-3 Tree Insertion (38 – Leaf Node)
Generalized Insertion – Leaf Node P S M Le M P SeL
2-3 Tree Insertion (36 Internal Node)
2-3 Tree Insertion (36)
Generalized Insertion, Internal Node P S M Le cbad e cbad LS M P
2-3-4 Trees If allowing – 2 values/node – 3 children/node is better… What about – 3 values/node – 4 children/node ??? Definition S M L > L< S>S <M>M <L >L<S>S<L S L S >S<S – See 2-3 Tree “3-Node” …AND…
Insertion in Tree Same scheme as 2-3 Tree – Traverse down tree – If node is 4-node Split tree Insert into new tree – This means what in terms of… Growth in Height versus Width? How far ‘up’ will insertion cascade?
Insertion in Tree (20)
Insertion in Tree (70)
Generalized Insertion in Tree P S M Le cbad e cbad LS M P Splitting a 4-Node with a 2-Node Parent P S M Le cbad
Generalized Insertion in Tree Splitting a 4-Node with a 3-Node Parent f cbad P Q S M Lee cbad M P Q LSff cbad P Q S M Le
Binary Search Tree Representation Color-code node-type information – 2-Node Ordinary binary search tree node – 4-Node with values A, B, and C Center B is Black Children A and C are Red – 3-Node with values A, and B (two choices) A is Black parent, B is Red right child –or – B is Black parent, A is Red left child – Ignore color-code Structure of binary search tree Two ways to change tree – Color change – Rotations
B AC STUV Red-Black Tree Representation A B C STUV 4-Node
Red-Black Tree Representation A B STU B AU ST A SB TU 3-Node
Red-Black Tree Translation
Red-Black Tree Insertion B AC STUV A B C STUV Parent (root) is 4-Node B AC STUV B AC STUV Representation Red-Black Representation
Red-Black Tree Insertion Parent is 2-Node Representation Red-Black Representation P S M Le cbad e cbad LS M P M SL ABCD P EM SL ABCD P E
Red-Black Tree Insertion Parent is 3-Node (version 1 of 2) Representation Red-Black Representation e cbad M P Q LSf f cbad P Q S M Le M SL ABCD P Q FE M SL ABCD P Q FE
Red-Black Tree Insertion Parent is 3-Node (version 2 of 2) - Rotation M SL ABCD P Q FE M SL ABCD P Q F E f cbad P Q S M Le
Adelson-Velskii and Landis (AVL)
AVL Tree Binary tree For every node x, define its balance factor balance factor of x = height of left subtree of x - height of right subtree of x Balance factor of every node x is - 1, 0, or 1 The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). The height of every n node binary tree is at least log 2 (n+1)
AVL Search Tree
insert(9)
insert(29) RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)
insert(29) RR rotation
AVL Rotations Single Rotation – RR – LL Double Rotation – RL new node in left subtree of right subtree Go right down and then go left down – LR
LL Rotation Algorithm BinaryNode LL_Rotate ( BinaryNode k2) { BinaryNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }
RR Rotation Algorithm BinaryNode RR_Rotate ( BinaryNode k1 ) { BinaryNode k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; }
Single Rotation Running Example Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially
Single Rotation (2) Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially
Why Double Rotation? Single Rotation cannot solve LR or RL
LR Double Rotation BinaryNode LR_doubleRotate ( BinaryNode k3 ){ k3.left = RR_Rotate ( k3.left ); return LL_Rotate ( k3 ); }
LR Double Rotation Example
RL Double Rotation BinaryNode RL_doubleRotate ( BinaryNode k1 ) { k1.right = LL_Rotate ( k1.right ); return RR_Rotate ( k1 ); }
More Double Rotation Example Consider the insertion of 15, 14, 13 into the AVL tree built by inserting 1,...,7 sequentially
Double Rotation Example (2) Consider the insertion of 15,..., 10, 9, 8, and 8.5 into the AVL tree built by inserting 1,...,7 sequentially
Single Left Rotation
Double Rotation (Step One)
Double Rotation (Step Two)
B-Trees Binary tree is still not efficient enough for searching A perfectly balanced binary tree has 5 levels for 25 nodes, while a 5-ary tree has only 3 levels Especially useful for external usage
B-tree A B-tree of order m has the following properties: The data items are stored at the leaves in order The root is either a leaf or has 2 to m children All non-leaf nodes (except the root) have m/2 to m children Each non-leaf node has at most m-1 keys: key i is the data value in subtree i+1 All leaves are at the same depth and have L/2 to L items
B-tree Insertion - Split L and M are determined by the sizes of a node, the size of an item and the size of a key Requiring nodes to be half full guarantees that the B-tree will be pretty balanced and will not degenerate into a simple binary tree, or even into a linked list A B-tree of order 3 is also known as a 2-3 tree Insertion of 55 in the example B-tree causes a split into two leaves
B-tree Insertion - Cascaded Split Further insertion of 40 in the B-tree causes a split into two leaves and then a split of the parent node
B-tree Deletion Deletion is done similarly If the number of items in a leaf falls below the minimum, adopt an item from a neighboring leaf If the number of items in the neighboring leaves are also minimum, combine two leaves. Their parent will then lose a child and it may need to be combined with its neighbor This combination process should be recursively executed up the tree until: – Getting to the root – A parent has more than the minimum number of children