540 likes | 634 Views
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
E N D
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”)
S < S > S 2-3 Tree Nodes S L < S >S <L > L
60 60 60 30 30 90 90 30 90 10 10 50 50 80 80 100 100 10 50 80 100 20 20 40 40 70 70 20 40 70 39 39 BST Insertion 39
50 30 70 90 50 10 20 40 60 80 100 30 70 90 10 20 39 40 60 80 100 2-3 Tree Insertion (39)
30 30 10 20 39 40 10 20 38 39 40 30 39 10 20 38 40 2-3 Tree Insertion (38)
50 30 39 70 90 50 10 20 38 40 60 80 100 30 39 70 90 10 20 37 38 40 60 80 100 2-3 Tree Insertion (37)
50 70 90 60 80 100 30 39 10 20 37 38 40 2-3 Tree Insertion (36)
30 39 10 20 36 37 38 40 37 50 50 30 39 30 37 39 30 39 70 90 10 20 37 38 40 10 20 36 38 40 10 20 36 38 40 60 80 100 2-3 Tree Insertion (36)
30 30 10 20 39 40 10 20 38 39 40 30 39 10 20 38 40 2-3 Tree Insertion (38 – Leaf Node)
P M P S M L e S L e Generalized Insertion – Leaf Node
50 70 90 60 80 100 30 39 10 20 37 38 40 2-3 Tree Insertion (36 Internal Node)
30 39 10 20 36 37 38 40 37 50 50 30 39 30 37 39 30 39 70 90 10 20 37 38 40 10 20 36 38 40 10 20 36 38 40 60 80 100 2-3 Tree Insertion (36)
P M P S M L e S L e a a b b c c d d Generalized Insertion, Internal Node
S <S >S<L >L S L <S >S S M L < S >S <M >M <L > L 2-3-4 Trees • If allowing • 2 values/node • 3 children/node is better… • What about • 3 values/node • 4 children/node ??? • Definition • See 2-3 Tree • …AND… • “3-Node”
Insertion in 2-3-4 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?
30 30 10 20 60 10 60 Insertion in 2-3-4 Tree (20) 20 10 30 60
30 30 50 10 20 40 50 60 10 20 40 60 30 50 10 20 40 60 70 Insertion in 2-3-4 Tree (70) 70
P M P S M L e S L e a a a b b b c c c d d d P S M L e Generalized Insertion in 2-3-4 Tree Splitting a 4-Node with a 2-Node Parent
P Q M P Q S M L e f S L e f a a a b b b c c c d d d P Q S M L e f Generalized Insertion in 2-3-4 Tree Splitting a 4-Node with a 3-Node Parent
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 A B C A C S T U V S T U V Red-Black Tree Representation 4-Node
A A B S B S T U T U B A U S T Red-Black Tree Representation 3-Node
10 8 10 20 8 20 3 9 12 40 1 3 4 9 12 15 30 40 1 4 15 30 Red-Black Tree Translation
B A B C B A C S T U V A C S T U V S T U B V A C S T U V Red-Black Tree Insertion Parent (root) is 4-Node 2-3-4 Representation Red-Black Representation
P M P S M L e S L e a a b b c c d d P P M E M E S L S L A B C D A B C D Red-Black Tree Insertion Parent is 2-Node 2-3-4 Representation Red-Black Representation
M P Q S L e f a a b b c c d d P Q P P M Q M Q S M L e f S L S L E F E F A B C D A B C D Red-Black Tree Insertion Parent is 3-Node (version 1 of 2) 2-3-4 Representation Red-Black Representation
Q F P a b c d E M P Q S L P A B C D M Q S M L e f S L E F A B C D Red-Black Tree Insertion Parent is 3-Node (version 2 of 2) - Rotation
30 20 20 20 10 30 10 30 10 40 Adelson-Velskii and Landis (AVL)
AVL Tree -1 • 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 xis -1, 0, or 1 • The height of an AVL tree that has nnodes is at most1.44 log2 (n+2). • The height of every nnode binarytree is at leastlog2 (n+1). 1 1 -1 0 1 0 0 -1 0 0 0 0
-1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 -1 0 0 0 60 35 1 20 5 0 25 AVL Search Tree
insert(9) -1 10 0 1 1 7 40 -1 0 1 -1 0 45 3 8 30 0 -1 0 0 0 0 60 35 1 9 20 5 0 25
insert(29) -1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 -1 0 0 0 -2 60 35 1 20 5 0 -1 RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2) 25 0 29
insert(29) -1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 0 0 0 0 60 35 1 25 5 0 0 20 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 0 -2 1 1 2 1 -1 0 0 2 2 3 1 2 0 2 3 3 1 4 4 1 5 3 5
Single Rotation (2) Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially 4 2 -2 2 5 4 1 1 6 3 5 3 6
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 ); }
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 4 4 2 2 6 6 1 7 1 14 3 5 3 5 7 15 15 14
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 4 4 6 7 2 2 14 5 14 6 1 3 1 3 7 15 13 15 5 13
20 40 10 40 20 50 30 50 10 30 60 60 Single Left Rotation
40 40 30 50 20 50 20 35 60 10 30 60 10 25 25 35 22 22 Double Rotation (Step One)
40 30 30 50 20 40 20 35 60 10 25 35 50 10 25 22 60 22 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