Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Similar presentations


Presentation on theme: "Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes."— Presentation transcript:

1 Balanced Trees

2 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”)

3 2-3 Tree Nodes > L< S S > S< S >S <L S L

4 BST Insertion 60 30 5010 90 10080 704020 39 60 30 5010 90 10080 704020 39 60 30 5010 90 10080 704020

5 2-3 Tree Insertion (39) 70 90 6010080 30 10 20 50 40 70 90 6010080 30 10 20 50 39 40

6 2-3 Tree Insertion (38) 30 10 2039 40 30 10 2038 39 403810 20 30 39 40

7 2-3 Tree Insertion (37) 70 90 6010080 50 3810 20 30 39 4010 20 30 39 4037 38 50 70 90 6010080

8 2-3 Tree Insertion (36) 10 20 30 39 4037 38 70 90 6010080 50

9 2-3 Tree Insertion (36) 10 20 30 39 4037 3810 20 30 39 4036 37 38 10 20 30 37 39 403836 5037 50 10 20 30 36 39 4038 70 90 6010080

10 2-3-4 Trees, Red-Black Trees

11 2-3 Tree Insertion (38 – Leaf Node) 30 10 2039 40 30 10 2038 39 403810 20 30 39 40

12 Generalized Insertion – Leaf Node P S M Le M P SeL

13 2-3 Tree Insertion (36 Internal Node) 10 20 30 39 4037 38 70 90 6010080 50

14 2-3 Tree Insertion (36) 10 20 30 39 4037 3810 20 30 39 4036 37 38 10 20 30 37 39 403836 5037 50 10 20 30 36 39 4038 70 90 6010080

15 Generalized Insertion, Internal Node P S M Le cbad e cbad LS M P

16 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…

17 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?

18 Insertion in 2-3-4 Tree (20) 10 30 60 20 10 30 6010 20 30 60

19 Insertion in 2-3-4 Tree (70) 10 20 30 40 50 60 10 20 30 50 406010 20 30 50 4060 70 70

20 Generalized Insertion in 2-3-4 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

21 Generalized Insertion in 2-3-4 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

22 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

23 B AC STUV Red-Black Tree Representation A B C STUV 4-Node

24 Red-Black Tree Representation A B STU B AU ST A SB TU 3-Node

25 Red-Black Tree Translation 10 820 1240 301541 39 8 10 20 1 3 412 1530 409

26 Red-Black Tree Insertion B AC STUV A B C STUV Parent (root) is 4-Node B AC STUV B AC STUV 2-3-4 Representation Red-Black Representation

27 Red-Black Tree Insertion Parent is 2-Node 2-3-4 Representation Red-Black Representation P S M Le cbad e cbad LS M P M SL ABCD P EM SL ABCD P E

28 Red-Black Tree Insertion Parent is 3-Node (version 1 of 2) 2-3-4 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

29 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

30 Adelson-Velskii and Landis (AVL) 30 10 20 1030 20 40 1030 20

31 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). 00 0 0 1 0 0 1 0 1

32 AVL Search Tree 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 20 25 35 45 60

33 insert(9) 00 0 0 1 0 0 1 0 1 9 0 0 10 7 83 1 5 30 40 20 25 35 45 60

34 insert(29) 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 20 25 35 45 60 29 0 -2 RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

35 insert(29) 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 25 35 45 60 0 RR rotation. 20 0 29

36 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

37 LL Rotation Algorithm BinaryNode LL_Rotate ( BinaryNode k2) { BinaryNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

38 RR Rotation Algorithm BinaryNode RR_Rotate ( BinaryNode k1 ) { BinaryNode k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; }

39 Single Rotation Running Example Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially 1 0 -2 1 2 3 1 2 0 2 1 3 0 2 1 3 0 4 5 2 1 4 5 3

40 Single Rotation (2) Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially 2 1 4 5 3 6 2 1 4 5 3 6 -2

41 Why Double Rotation? Single Rotation cannot solve LR or RL

42 LR Double Rotation BinaryNode LR_doubleRotate ( BinaryNode k3 ){ k3.left = RR_Rotate ( k3.left ); return LL_Rotate ( k3 ); }

43 LR Double Rotation Example

44 RL Double Rotation BinaryNode RL_doubleRotate ( BinaryNode k1 ) { k1.right = LL_Rotate ( k1.right ); return RR_Rotate ( k1 ); }

45 More Double Rotation Example Consider the insertion of 15, 14, 13 into the AVL tree built by inserting 1,...,7 sequentially 2 1 4 6 3 7 5 15 14 2 1 4 6 3 5 157

46 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 2 1 4 6 3 14 5 157 13 2 1 4 7 3 14 6 1513 5

47 Single Left Rotation 50 1040 20 60 30 50 10 40 20 6030

48 Double Rotation (Step One) 50 10 40 20 6030 3525 22 50 20 40 30 6035 2510 22

49 Double Rotation (Step Two) 50 20 40 30 6035 2510 22 40 10 30 20 503525 2260

50 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

51 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

52 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

53 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

54 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


Download ppt "Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes."

Similar presentations


Ads by Google