Height Balanced Trees 2 3 Trees Extended tree
Height Balanced Trees 2 -3 Trees
Extended tree. § Tree in which all empty subtrees are replaced by new nodes that are called external nodes. § Original nodes are called internal nodes.
Extended Binary Tree external node internal node
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.
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.
2 -3 Tree Definition struct two. Three. Node { int first, second; // Two data fields two. Three. Node *left, *mid, *right; // the three children int node. Type; // 2 or 3 node };
Node Structure No. of Children D 1 LC MC D 2 RC • 2 -node uses LC, D 1, and MC. • 3 -node uses all fields. 2 27 3 16 25 Only internal nodes are represented!
2 -3 Tree class two. Three. Tree { public: two. Three. Tree(); // Constructor ~two. Three. Tree(); // Destructor void add(int item); // Adds an item void delete(int item); // Adds an item two. Three. Node * search(int item); // Searches for an item private: two. Three. Node *root; // Pointer to root node // Private helper functions go here };
Example 2 -3 Tree 8 4 1 3 15 20 5 6 2 -nodes 9 17 30 40 3 -nodes
Minimum # Of Pairs/Elements • Happens when all internal nodes are 2 -nodes.
Minimum # Of Pairs/Elements • Number of nodes = 2 h – 1, where h is tree height (excluding external nodes). • Each node has 1 (key, value) pair. • So, minimum # of pairs = 2 h – 1
Maximum # Of Pairs/Elements • • • Happens when all internal nodes are 3 -nodes. Full degree 3 tree. # of nodes = 1 + 32 + 33 + … + 3 h-1 = (3 h – 1)/2. Each node has 2 pairs. So, # of pairs = 3 h – 1.
2 -3 Tree Height Bounds • 2 h – 1 <= n <= 3 h – 1. • log 3(n+1) <= h <= log 2(n+1).
Search 8 4 1 3 15 20 5 6 9 17 External nodes not shown. 30 40
Insert into a (leaf) 2 -node 2 D 1 v < D 1 v > D 1 3 3 v D 1 v
Insert into a (leaf) 3 -node D 1 3 Split D 2 2 v 2 2 u Insert X w Let u = min(X, D 1, D 2) Let v = mid(X, D 1, D 2) Let w = max(X, D 1, D 2)
Insert into a (leaf) 3 -node Insert into the parent of the original node 2 v 2 2 u w Insert X Let u = min(X, D 1, D 2) Let v = mid(X, D 1, D 2) Let w = max(X, D 1, D 2)
Insert into a (leaf) 3 -node (Parent is 2 -node) v<P P 2 v D 1 3 m D 2 u 2 w 3 P 2 m
Insert into a (leaf) 3 -node (Parent is 2 -node) Insert X v>P Let u = min(X, D 1, D 2) Let v = mid(X, D 1, D 2) P Let w = max(X, D 1, D 2) l D 1 2 3 P l D 2 u 3 v 2 w 2
Insert into a (leaf) 3 -node (Parent is 3 -node) Insert X Insert into the parent of the node Let u = min(X, D 1, D 2) Let v = mid(X, D 1, D 2) v<P P Let w = max(X, D 1, D 2) P D 1 3 m D 2 3 Q v n u 2 w 2 2 2 Q m 2 n
Insert into a (leaf) 3 -node (Parent is 3 -node) Insert into the parent of the node Insert X Let u = min(X, D 1, D 2) P< v < Q Let v = mid(X, D 1, D 2) v 2 Let w = max(X, D 1, D 2) P l D 1 3 3 Q D 2 P n l u 2 2 Q w 2 2 n
Insert into a (leaf) 3 -node (Parent is 3 -node) Insert X Insert into the parent of the node Let u = min(X, D 1, D 2) Let v = mid(X, D 1, D 2) P< Q < v Q 2 Let w = max(X, D 1, D 2) P l 3 Q m P D 1 3 l D 2 2 m v u 2 2 w 2
Insert 8 4 1 3 15 20 5 6 Insert pair with key = 16. • Move D 1 to D 2. • Insert as D 1. 9 17 30 40
Insert 8 4 1 3 15 20 5 6 • Now insert a pair with key = 2. • New pair goes into a 3 -node. 9 16 17 30 40
Insert Into A Leaf 3 -node • Insert new pair so that the 3 keys are in ascending order. 123 • Move third key into a new 2 -node. 3 12 • Insert second key and pointer to new 2 -node into parent. 2 1 3
Insert 8 4 1 3 15 20 5 6 • Insert a pair with key = 2. 9 16 17 30 40
Insert 8 4 2 15 20 3 1 5 6 9 16 17 30 40 • Insert a pair with key = 2 plus a pointer into parent.
Insert 8 2 4 1 3 15 20 5 6 9 • Now, insert a pair with key = 18. 16 17 30 40
Insert Into A Leaf 3 -node • Insert new pair so that the 3 keys are in ascending order. 16 17 18 • Move third key into a new 2 -node. 18 16 17 • Insert second key and pointer to new 2 -node into parent. 17 16 18
Insert 8 2 4 1 3 15 20 5 6 • Insert a pair with key = 18. 9 16 17 30 40
Insert 8 17 2 4 1 3 15 20 5 6 9 16 18 30 40 • Insert a pair with key = 17 plus a pointer into parent.
Insert Into A Nonleaf 3 -node • Insert new pair and pointer so that the 3 keys are in ascending order. 15 17 20 • Move third key and 3 rd and 4 th pointers into a new 2 -node. 20 15 17 • Insert second key and pointer to new 2 -node into parent. 17 15 20
Insert 8 17 2 4 1 3 15 20 5 6 9 16 18 30 40 • Insert a pair with key = 17 plus a pointer into parent.
Insert 17 8 2 4 1 3 15 5 6 9 20 16 18 30 40 • Insert a pair with key = 17 plus a pointer into parent.
Insert 8 17 2 4 1 3 15 5 6 9 20 16 • Now, insert a pair with key = 7. 18 30 40
Insert Into A Nonleaf 3 -node • Insert new pair and pointer so that the 3 keys are in ascending order. 5 6 7 • Move third key and 3 rd and 4 th pointers into a new 2 -node. 7 5 6 • Insert second key and pointer to new 2 -node into parent. 6 5 7
Insert 8 17 2 4 1 3 15 5 6 9 20 16 • Now, insert a pair with key = 7. 18 30 40
Insert 8 17 6 2 4 7 1 3 15 5 9 20 16 18 30 40 • Insert a pair with key = 6 plus a pointer into parent.
Insert Into A Nonleaf 3 -node • Insert new pair and pointer so that the 3 keys are in ascending order. 2 4 6 • Move third key and 3 rd and 4 th pointers into a new 2 -node. 6 2 4 • Insert second key and pointer to new 2 -node into parent. 4 2 6
Insert 8 17 6 2 4 7 1 3 15 5 9 20 16 18 30 40 • Insert a pair with key = 6 plus a pointer into parent.
Insert 8 17 4 6 2 1 15 3 9 20 16 18 30 40 5 • Insert a pair with key = 4 plus a pointer into parent. 7
Insert 8 4 17 6 2 1 15 3 5 7 9 20 16 18 30 40 • Insert a pair with key = 8 plus a pointer into parent. • There is no parent. So, create a new root.
Insert 8 4 17 6 2 1 3 5 15 7 • Height increases by 1. 9 20 16 18 30 40
Delete from a (leaf) 3 -node 3 D 1 D 2 v == D 2 v == D 1 2 2 D 1 D 2
Delete from a (leaf) 2 -node 2 v Propagate the hole up until it can be eliminated
Delete from a (leaf) 2 -node Case 1: The hole has a 2 node as a parent and a 2 -node as sibling P Q l m 2 Q 2 n P l 3 Q m P n l 2 m 1. Merge parent and the sibling 2. Move the hole up 2 n
Delete from a (leaf) 2 -node Case 2: The hole has a 2 node as a parent and a 3 -node as sibling 2 P Q l m Q 3 n P R o l 2 m 2 R R n 2 P o l 3 2 Q m n o Rotate in the direction of the hole
Delete from a (leaf) 2 -node Case 3: The hole has a 3 node as a parent and a 3 -node as sibling Q R 3 3 l m Q l R m T S n 3 S p Q l o T 3 3 R p Q no Q S m 2 n 3 o l T p S R 3 T 2 p S 2 m n 3 o m n l R l op T R m Q S n 3 3 T o 2 p
Delete from a (leaf) 2 -node Case 4: The hole has a 3 node as a parent and a 2 -node as sibling P Q l m 3 P R 2 l o Q m n Q P l 3 R 2 n R o R 2 m 3 P o n l 3 m 2 o Q n
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 3 a: 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 3 b: 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.
Delete 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 Ø else – stop
Delete from a non-leaf node Exchange with its predecessor or successor and then delete
Delete From A Leaf 8 2 4 1 3 15 20 5 6 • Delete the pair with key = 16. • 3 -node becomes 2 -node. 9 16 17 30 40
Delete From A Leaf 8 2 4 1 3 15 20 5 6 9 17 30 40 • 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.
Delete From A Leaf 8 2 4 1 3 15 30 5 6 9 20 40 • 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.
Delete From A Leaf 8 2 4 1 3 15 5 6 • Delete the pair with key = 30. • Deletion from a 3 -node. • 3 -node becomes 2 -node. 9 30 40
Delete From A Leaf 8 2 4 1 3 15 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.
Delete From A Leaf 8 2 5 1 4 15 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.
Delete From A Leaf 8 2 1 15 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.
Delete From A Leaf 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.
Delete From A Leaf 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.
Delete From A Leaf 2 8 1 • Height reduces by 1. 4 5 9 15
Delete From An Intermediate Node 8 2 4 1 3 15 20 5 6 9 16 17 30 40 • Replace with in-order predecessor or successor and delete
Delete From An Intermediate Node 8 2 4 1 3 15 20 5 6 9 16 17 30 40 • Replace with in-order predecessor or successor and delete
Delete From An Intermediate Node 6 2 4 1 3 15 20 5 8 9 16 17 30 40 • Replace with in-order predecessor or successor and delete
- Slides: 65