2 3 Trees Extended tree Tree in which

  • Slides: 43
Download presentation
2 -3 Trees • Extended tree. § Tree in which all empty subtrees are

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

Extended Binary Tree external node internal node

2 -3 Tree Definition • Every internal node is either a 2 -node or

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 L 2 -node R § 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;

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

Example 2 -3 Tree 8 4 1 3 8 15 20 5 6 2

Example 2 -3 Tree 8 4 1 3 8 15 20 5 6 2 -node 9 17 1 3 30 40 3 -node

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

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

Minimum # Of Pairs/Elements • Number of nodes = 2 h – 1, where

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

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

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

Node Structure LC • • P 1 MC P 2 RC 2 -node uses

Node Structure LC • • P 1 MC P 2 RC 2 -node uses LC, P 1, and MC. 3 -node uses all fields. May have optional parent field. Only internal nodes are represented!

Search 8 4 1 3 15 20 5 6 9 17 External nodes not

Search 8 4 1 3 15 20 5 6 9 17 External nodes not shown. 30 40

Insert 8 4 1 3 15 20 5 6 Insert pair with key =

Insert 8 4 1 3 15 20 5 6 Insert pair with key = 16. 9 17 30 40

Insert 8 4 1 3 15 20 5 6 • Move P 1 to

Insert 8 4 1 3 15 20 5 6 • Move P 1 to P 2. • Insert as P 1. • 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

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

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

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

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

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

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

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

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

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

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 •

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

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 •

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

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

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

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

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

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

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

Delete 8 2 4 1 3 15 20 5 6 9 16 17 30

Delete 8 2 4 1 3 15 20 5 6 9 16 17 30 40 • Delete the pair with key = 8. • Transform deletion from interior into deletion from a leaf. • Replace by largest in left subtree.

Delete From A Leaf 8 2 4 1 3 15 20 5 6 •

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

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

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

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

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

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

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

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