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

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 RML  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 15 20 8 4 1 35 630 409 17 1 3 8 2-node 3-node

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

7 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

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

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

10 Node Structure 2-node uses LC, P1, and MC. 3-node uses all fields. May have optional parent field. Only internal nodes are represented! LCP1MCP2RC

11 Search 15 20 8 4 1 35 630 409 17 External nodes not shown.

12 Insert 15 20 8 4 1 35 630 409 17 Insert pair with key = 16.

13 Insert 15 20 8 4 1 35 630 409 Move P1 to P2. Insert as P1. Now insert a pair with key = 2. New pair goes into a 3-node. 16 17

14 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. 1 2 3 1 2 3 Insert second key and pointer to new 2-node into parent. 31 2

15 Insert 15 20 8 4 1 35 630 409 Insert a pair with key = 2. 16 17

16 Insert 15 20 8 4 5 630 409 16 17 3 1 2 Insert a pair with key = 2 plus a pointer into parent.

17 Insert Now, insert a pair with key = 18. 15 20 8 1 2 4 5 630 409 16 17 3

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 16 17 18 Insert second key and pointer to new 2-node into parent. 1816 17

19 Insert Insert a pair with key = 18. 15 20 8 1 2 4 5 630 409 16 17 3

20 Insert Insert a pair with key = 17 plus a pointer into parent. 15 20 8 1 2 4 5 630 4093 18 16 17

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 3 rd and 4 th pointers into a new 2-node. Insert second key and pointer to new 2-node into parent. 15 17 20 15 17 20 2015 17

22 Insert Insert a pair with key = 17 plus a pointer into parent. 15 20 8 1 2 4 5 630 4093 18 16 17

23 Insert Insert a pair with key = 17 plus a pointer into parent. 8 1 2 4 5 630 409316 17 15 18 20

24 Insert Now, insert a pair with key = 7. 1 2 4 5 630 409316 15 18 20 8 17

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 3 rd and 4 th pointers into a new 2-node. 5 6 7 5 6 7 Insert second key and pointer to new 2-node into parent. 75 6

26 Insert Now, insert a pair with key = 7. 1 2 4 5 630 409316 15 18 20 8 17

27 Insert Insert a pair with key = 6 plus a pointer into parent. 30 40 1 2 4 9316 15 18 20 8 17 5 7 6

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 3 rd and 4 th pointers into a new 2-node. 2 4 6 2 4 6 Insert second key and pointer to new 2-node into parent. 62 4

29 Insert Insert a pair with key = 6 plus a pointer into parent. 30 40 1 2 4 9316 15 18 20 8 17 5 7 6

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

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

32 Insert Height increases by 1. 30 40 1 9 3 16 15 18 20 6 2 57 4 17 8

33 Exercise—build a 2-3 tree Build a 2-3 tree from the following key values in sequence: 45, 28, 36, 22, 68, 50, 10, 98, 26, 15, 27, 78

34 Delete Delete the pair with key = 8. Transform deletion from interior into deletion from a leaf. Replace by largest in left subtree. 15 20 8 1 2 4 5 630 409 16 17 3

35 Delete From A Leaf Delete the pair with key = 16. 3-node becomes 2-node. 15 20 8 1 2 4 5 630 409 16 17 3

36 Delete Transform interior delete to leaf delete. Delete from a 3-node leaf reduces leaf degree. Delete from a 2-node leaf.  Check one sibling and determine if it is a 3-node.  If so, borrow a pair and a subtree via parent node.  If not, combine with sibling 2-node and in-between pair in parent. Continue up the tree if parent was a 2- node.

37 Delete From A Leaf 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. 15 20 8 1 2 4 5 630 4093 17

38 Delete From A Leaf 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. 15 30 8 1 2 4 5 693 20 40

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

40 Delete From A Leaf 8 1 2 4 5 693 15 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.

41 Delete From A Leaf 8 1 2 5 94 15 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. 6

42 Delete From A Leaf 8 1 4 59 15 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. 2

43 Delete From A Leaf 8 1 4 5 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. 2 9 15

44 Delete From A Leaf 1 4 5 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. 9 15 2 8

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


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