Presentation is loading. Please wait.

Presentation is loading. Please wait.

Height Balanced Trees 2-3 Trees.

Similar presentations


Presentation on theme: "Height Balanced Trees 2-3 Trees."— Presentation transcript:

1 Height Balanced Trees 2-3 Trees

2 Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal nodes.

3 Extended Binary Tree external node internal node

4 2-3 Tree Definition Every internal node is either a 2-node or a 3-node. A 2-node has one key and 2 children/subtrees. All keys in left subtree are smaller than this key. All keys in right subtree are bigger than this key.

5 2-3 Tree Definition A 3-node has 2 keys and 3 children/subtrees; first key is smaller than second key. All keys in left subtree are smaller than first key. All keys in middle subtree are bigger than first key and smaller than second key. All keys in right subtree are bigger than second key. All external nodes are on the same level.

6 2-3 Tree Definition struct twoThreeNode {
int first, second; // Two data fields twoThreeNode *left, *mid, *right; // the three children int nodeType; // 2 or 3 node };

7 Only internal nodes are represented!
Node Structure No. of Children D1 D2 LC MC RC 2-node uses LC, D1, and MC. 3-node uses all fields. 2 27 3 16 25 LC = left child P1 = first pair (key, value) MC = middle child P2 = second pair RC = right child Only internal nodes are represented!

8 2-3 Tree class twoThreeTree { public: twoThreeTree(); // Constructor
~twoThreeTree(); // Destructor void add(int item); // Adds an item void delete(int item); // Adds an item twoThreeNode * search(int item); // Searches for an item private: twoThreeNode *root; // Pointer to root node // Private helper functions go here };

9 Example 2-3 Tree 2-nodes 3-nodes 15 20 8 4 1 3 5 6 30 40 9 17
8 4 1 3 5 6 9 17 2-nodes 3-nodes Leaf-pushed 2-3 tree … only leaves have pairs, other nodes have only keys. 2-node has max key in left subtree. 3-node has 2 keys; first is max key in eft subtree and second is max key in middle subtree.

10 Minimum # Of Pairs/Elements
Happens when all internal nodes are 2-nodes.

11 Minimum # Of Pairs/Elements
Number of nodes = 2h – 1, where h is tree height (excluding external nodes). Each node has 1 (key, value) pair. So, minimum # of pairs = 2h – 1

12 Maximum # Of Pairs/Elements
Happens when all internal nodes are 3-nodes. Full degree 3 tree. # of nodes = … + 3h-1 = (3h – 1)/2. Each node has 2 pairs. So, # of pairs = 3h – 1.

13 2-3 Tree Height Bounds 2h – 1 <= n <= 3h – 1.
log3(n+1) <= h <= log2(n+1).

14 Search 8 4 1 3 5 6 9 17 External nodes not shown.

15 Insert into a (leaf) 2-node
v < D1 v > D1 3 3 v D1 D1 v

16 Insert into a (leaf) 3-node
2 u w v Split 3 D1 D2 Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2)

17 Insert into a (leaf) 3-node
Insert into the parent of the original node 2 v 2 2 Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2) u w

18 Insert into a (leaf) 3-node (Parent is 2-node)
v < P 3 D1 D2 2 P 3 v P m m 2 2 u w

19 Insert into a (leaf) 3-node (Parent is 2-node)
Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2) v > P 2 3 P P v l l 3 2 2 D1 D2 u w

20 Insert into a (leaf) 3-node (Parent is 3-node)
Insert into the parent of the node Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2) 2 v < P P 3 2 2 P Q v Q m n 3 m n 2 2 D1 D2 u w

21 Insert into a (leaf) 3-node (Parent is 3-node)
Insert into the parent of the node Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2) P< v < Q 2 v 3 2 2 P Q P Q l n 3 l n 2 2 D1 D2 u w

22 Insert into a (leaf) 3-node (Parent is 3-node)
Insert into the parent of the node Insert X Let u = min(X, D1, D2) Let v = mid(X, D1, D2) Let w = max(X, D1, D2) 2 P< Q < v Q 3 2 2 P Q P v l m 3 l m 2 u 2 D1 D2 w

23 Insert 15 20 8 4 1 3 5 6 30 40 9 17 Insert pair with key = 16.
8 4 1 3 5 6 9 17 Insert pair with key = 16. Move D1 to D2. Insert as D1.

24 Insert 8 4 15 20 1 3 5 6 9 30 40 16 17 Now insert a pair with key = 2.
1 3 5 6 9 Now insert a pair with key = 2. New pair goes into a 3-node.

25 Insert Into A Leaf 3-node
Insert new pair so that the 3 keys are in ascending order. 1 2 3 Move third key into a new 2-node. 1 2 3 Insert second key and pointer to new 2-node into parent. 3 1 2

26 Insert 8 4 1 3 5 6 9 Insert a pair with key = 2.

27 Insert Insert a pair with key = 2 plus a pointer into parent. 8 4 2
3 1 5 6 9 Insert a pair with key = 2 plus a pointer into parent.

28 Insert 8 1 2 4 5 6 9 3 Now, insert a pair with key = 18.

29 Insert Into A Leaf 3-node
Insert new pair so that the 3 keys are in ascending order. Move third key into a new 2-node. 16 17 18 Insert second key and pointer to new 2-node into parent. 18 16 17

30 Insert 8 1 2 4 5 6 9 3 Insert a pair with key = 18.

31 Insert Insert a pair with key = 17 plus a pointer into parent. 8 17
2 4 18 1 3 5 6 9 16 Insert a pair with key = 17 plus a pointer into parent.

32 Insert Into A Nonleaf 3-node
Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3rd and 4th pointers into a new 2-node. 15 17 20 Insert second key and pointer to new 2-node into parent. 20 15 17

33 Insert Insert a pair with key = 17 plus a pointer into parent. 8 17
2 4 18 1 3 5 6 9 16 Insert a pair with key = 17 plus a pointer into parent.

34 Insert Insert a pair with key = 17 plus a pointer into parent. 17 8
2 4 15 20 1 3 5 6 9 16 18 Insert a pair with key = 17 plus a pointer into parent.

35 Insert Now, insert a pair with key = 7. 1 2 4 5 6 30 40 9 3 16 15 18
2 4 5 6 9 3 16 15 18 20 8 17 Now, insert a pair with key = 7.

36 Insert Into A Nonleaf 3-node
Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3rd and 4th pointers into a new 2-node. 5 6 7 Insert second key and pointer to new 2-node into parent. 7 5 6

37 Insert Now, insert a pair with key = 7. 1 2 4 5 6 30 40 9 3 16 15 18
2 4 5 6 9 3 16 15 18 20 8 17 Now, insert a pair with key = 7.

38 Insert Insert a pair with key = 6 plus a pointer into parent. 8 17 6
8 17 6 2 4 15 20 7 1 3 5 9 16 18 Insert a pair with key = 6 plus a pointer into parent.

39 Insert Into A Nonleaf 3-node
Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3rd and 4th pointers into a new 2-node. 2 4 6 Insert second key and pointer to new 2-node into parent. 6 2 4

40 Insert Insert a pair with key = 6 plus a pointer into parent. 8 17 6
8 17 6 2 4 15 20 7 1 3 5 9 16 18 Insert a pair with key = 6 plus a pointer into parent.

41 Insert Insert a pair with key = 4 plus a pointer into parent. 8 17 4 6
8 17 4 6 2 15 20 5 7 9 16 18 1 3 Insert a pair with key = 4 plus a pointer into parent.

42 Insert Insert a pair with key = 8 plus a pointer into parent.
4 17 6 2 15 20 1 3 5 7 9 16 18 Insert a pair with key = 8 plus a pointer into parent. There is no parent. So, create a new root.

43 Insert 8 4 17 6 2 15 20 9 16 18 1 3 5 7 Height increases by 1.

44 Delete from a (leaf) 3-node
v == D2 v == D1 2 2 D1 D2

45 Delete from a (leaf) 2-node
Propagate the hole up until it can be eliminated 2 v

46 Delete from a (leaf) 2-node Case 1: The hole has a 2 node as a parent and a 2-node as sibling
3 P Q l m n 2 P Q 2 2 Q P l m n l m n Merge parent and the sibling Move the hole up

47 Rotate in the direction of the hole
Delete from a (leaf) 2-node Case 2: The hole has a 2 node as a parent and a 3-node as sibling 2 R Q P l m n o 2 2 P R 3 3 Q R P Q l m n o l m n o Rotate in the direction of the hole

48 Delete from a (leaf) 2-node Case 3: The hole has a 3 node as a parent and a 3-node as sibling
Q T R S l m n p o 3 S T Q R o l m p n 3 R T 2 Q l m n p S o 3 Q T R S p m n l o 3 Q R S T o l m p n 3 Q S 2 T l p m o R n

49 Delete from a (leaf) 2-node Case 4: The hole has a 3 node as a parent and a 2-node as sibling
Q o m n l 3 P R 2 Q l m n o 2 R 3 P Q l m n o 3 Q R 2 P n l m o

50 Delete Case 1: The leaf node is a 3-node
Convert the node into a 2-node by deleting the key and adjust the other data stored in the node. Case 2: The leaf node is a 2-node and the parent and the sibling are also 2-nodes Merge the parent and the sibling and propagate the hole to the parent of parent. Case 3: The leaf node is a 2-node and left or right sibling is a 3-node Apply rotation from the 3-node sibling to the node from which the data is being deleted. Case 4: The leaf node and its adjacent sibling are 2-nodes and the parent is a 3-node Case 3a: the right sibling is a 2-node Convert the parent into a 2-node and the sibling into a 3-node by moving the larger of the two values in the parent to the sibling. Adjust the pointers accordingly. Case 3b: the left sibling is a 2-node Convert the parent into a 2-node and the sibling into a 3-node by moving the smaller of the two values in the parent to the sibling. Adjust the pointers accordingly.

51 Delete Adjusting the hole: If the leaf node is a 3-node
Convert the node into a 2-node by deleting the key and adjust the other data stored in the node. stop. Else if the leaf node is a 2-node, create a hole by deleting the data in the node. Adjust the hole Adjusting the hole: If the adjacent sibling of the hole is a 3-node rotate in the direction of the hole. stop else make the sibling a 3-node by borrowing one from the parent. If the parent has become a hole (i.e. parent was previously a 2-node) adjust this new hole by repeating the process

52 Delete from a non-leaf node
Exchange with its predecessor or successor and then delete

53 Delete From A Leaf Delete the pair with key = 16.
8 1 2 4 5 6 9 3 Delete the pair with key = 16. 3-node becomes 2-node.

54 Delete From A Leaf Delete the pair with key = 17.
8 2 4 1 3 5 6 9 17 Delete the pair with key = 17. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If so borrow a pair and a subtree via parent node.

55 Delete From A Leaf Delete the pair with key = 20.
8 2 4 1 3 5 6 9 40 20 Delete the pair with key = 20. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair.

56 Delete From A Leaf Delete the pair with key = 30.
8 2 4 15 1 3 5 6 9 Delete the pair with key = 30. Deletion from a 3-node. 3-node becomes 2-node.

57 Delete From A Leaf Delete the pair with key = 3.
8 2 4 15 1 3 5 6 9 40 Delete the pair with key = 3. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If so borrow a pair and a subtree via parent node.

58 Delete From A Leaf Delete the pair with key = 6.
8 2 5 15 1 4 6 9 40 Delete the pair with key = 6. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair.

59 Delete From A Leaf Delete the pair with key = 40.
8 2 15 1 4 5 9 40 Delete the pair with key = 40. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair.

60 Delete From A Leaf Parent pair was from a 2-node.
8 2 1 4 5 9 15 Parent pair was from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair.

61 Delete From A Leaf Parent pair was from a 2-node.
2 8 1 4 5 9 15 Parent pair was from a 2-node. Check one sibling and determine if it is a 3-node. No sibling, so must be the root. Discard root. Left child becomes new root.

62 Delete From A Leaf 2 8 1 4 5 9 15 Height reduces by 1.

63 Delete From An Intermediate Node
8 1 2 4 5 6 9 3 Replace with in-order predecessor or successor and delete

64 Delete From An Intermediate Node
8 1 2 4 5 6 9 3 Replace with in-order predecessor or successor and delete

65 Delete From An Intermediate Node
6 1 2 4 5 8 9 3 Replace with in-order predecessor or successor and delete


Download ppt "Height Balanced Trees 2-3 Trees."

Similar presentations


Ads by Google