Definition b 2 3 4 Tree is a
Definition b 2 -3 -4 Tree is a multiway tree. Each node can have up to 4 children and 3 data items b All external nodes are at the same level b It is less space efficient than a Red-Black tree
Why is this called a 2 -3 -4 tree? b Nodes with a single link are not allowed in 2 -3 -4 trees b A non-leaf node with one data item must have two links ( children) b A non-leaf node with two data items must have three children b A non-leaf node with three data items must have four children
Structure of 2 -3 -4 Trees 60 85 95 Values less than 60 goes here Values between 60 and 85 goes here Values between 85 and 95 goes here Values greater than 95 goes here
Example of 2 -3 -4 Tree 50 10 5 7 9 70 80 30 40 60 75 85 90 92
Terminology 1 Node with 2 links called 2 -node 55 40 60 65 70
Terminology 2 data item with 3 links called 3 -node 15 30 10 12 17 21 25 35
Terminology 3 data item with 4 links called 4 -node 60 85 95 56 59 65 90 105 125
Operations for 2 -3 -4 Tree b Insert--always made into a leaf node b Deletion b Search b Split the node b Split the root Splitting keeps the tree balanced
Splitting Process b In 2 -3 -4 trees, splitting is on the way down or top-down b There are two possible splitting process • Splitting the node • Splitting the root
Node Split Process b When node splits, a new node is created b Middle value goes into the parent of the node being split -- because we split on the way down, we know there is room in the parent for the middle value b First value remains at the same node b Third value moves into the new node that is created Let’s see the node splitting process in action
Node Split Process 50 40 45 Assume this is the parent and we want to enter 100 60 855 85 95 951 • New node is created • 60 stays where it is • 85 goes to the parent • 95 goes to the new node • Now we can insert 100 after 95
Node Split Process 50 85 85 40 45 Assume this is the parent and we want to enter 100 60 85 95 95 95 100 • New node is created • 60 stays where it is • 85 goes to the parent • 95 goes to the new node • Now we can insert 100 after 95
Root Split Process b When the root is full • A new node is created • This newly created node becomes the root • A second new node is created and it becomes a sibling of the node being split Let’s see the root splitting process in action
Root Split Process 50 85 209 Assume this is the full root and we want to insert 100 • New node is created & 85 goes to the new root • The second node is created & 209 goes to the new node • Now we can insert 100 before 209
Root Split Process 85 85 50 85 209 209 100 209 Assume this is the full root and we want to insert 100 • New node is created & 85 goes to the new root • The second node is created & 209 goes to the new node • Now we can insert 100 before 209
Top-down Insertion b There are three different cases to consider for a 4 -node • • • It is the root of the 2 -3 -4 tree Its parent is a 2 -node Its parent is a 3 -node
Top-down Insertion- Case 1 b It is the root of the 2 -3 -4 tree X Y Z a b Y After insertion c d X Z a b c d
Top-down Insertion- Case 2 a b Its parent is a 2 -node After insertion Z Left child e e W X Y a b X Z c d W Y a b c d
Top-down Insertion- Case 2 b b Its parent is a 2 -node After insertion W Right child a X Y Z b c d e W Y a X Z b c d e
Top-down Insertion- Case 3 a b Its parent is a 3 -node After insertion Y Z Left child e V W X a b c d W Y Z e f V X a b c d f
Top-down Insertion- Case 3 b b Its parent is a 3 -node After insertion V Z f a W X Y b c d e Middle child V X Z a f W Y b c d e
Insertion Process b Search the place to insert b If the node is full split the node before the insertion Let’s see insertion and splitting process in action
Insertion Process Let’s insert some items. Assuming the tree is empty Insert 776 Insert 234 Insert 345 Insert 400 Insert 560 Insert 450 345 776 560 345 560 234 345 560 400 776 450 400400400 DONE! This is the basic process Root is full. Split the root • Create a new node and make it root. Move 560 to the root • Create second node (sibling) and move 776 to the new node • Insert the 234 776 Node is full. Split the node • Move 345 to the parent • Create a new node an move 400 • Insert 450 after 400
Deletion Transformation-Left child of a 3 -node b When nearest sibling is a 3 -node W Z X Z f V a b After deletion V W X Y Nearest Sibling c d f e a b c Y d e
Deletion Transformation-Left child of a 4 -node b When nearest sibling is a 3 -node V Y Z After deletion f U a b g f U V W X Nearest Sibling c d W Y Z e a b X c d e g
- Slides: 29