Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 Extended Binary Tree external node internal node

3 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. 8 2-node L R All keys in left subtree are smaller than this key. All keys in right subtree are bigger than this key.

4 2-3 Tree Definition A 3-node has 2 keys and 3 children/subtrees; first key is smaller than second key. 1 3 3-node R M L 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.

5 Example 2-3 Tree 2-node 3-node 15 20 8 4 1 3 5 6 30 40 9 17
8 4 1 3 5 6 9 17 2-node 3-node 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 left subtree and second is max key in middle subtree.

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

7 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

8 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.

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

10 Node Structure 2-node uses LC, P1, and MC. 3-node uses all fields.
RC 2-node uses LC, P1, and MC. 3-node uses all fields. May have optional parent field. Only internal nodes are represented! LC = left child P1 = first pair (key, value) MC = middle child P2 = second pair RC = right child

11 Search External nodes not shown. 15 20 8 4 1 3 5 6 30 40 9 17
8 4 1 3 5 6 9 17 External nodes not shown. Height of best AVL tree equals that of worst 2-3 tree. #cache misses when searching a 2-3 tree or AVL tree is at most tree height. Search in each node of 2-3 tree takes little more time. So, depending on cache miss penalty, 2-3 may yield faster search.

12 Insert 8 4 1 3 5 6 9 17 Insert pair with key = 16.

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

14 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. The code will do all 3 steps as one. 3 1 2

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

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

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

18 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

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

20 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.

21 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

22 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.

23 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.

24 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.

25 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

26 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.

27 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.

28 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

29 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.

30 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.

31 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.

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

33 Delete Delete the pair with key = 8.
8 1 2 4 5 6 9 3 Delete the pair with key = 8. Transform deletion from interior into deletion from a leaf. Replace by largest in left subtree.

34 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.

35 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.

36 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.

37 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.

38 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.

39 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.

40 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.

41 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.

42 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.

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


Download ppt "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."

Similar presentations


Ads by Google