#Computer-Science#CS251#DataStructure 2-3 Trees are always balanced, solving our problem from above. There are two types of nodes in a 2-3 tree.
- A 2-node has one item, and two children.
- The item I is less than the right child and greater than the left child ()
- A 3-node has two items and three children.
- The left child is less than , and the right child is greater than . The middle child is in between and
Inserting on a 2-3 Tree
- Move down the tree going right or left or down the middle as needed until you reach a leaf.
- Transform that leaf
- A 2-node becomes a 3-node
- A 3-node becomes a 4-node
- A 4-node is split. The middle item get’s pushed up the tree.
Try inserting 1, 2, 3, 4, 5, 6, 7, 8 on a 2-3 tree.
It stays balanced, unlike the Binary Search Tree.
Height
- If a tree is made up of all 2-nodes, then the height of the tree
- If a tree is made up of all 3-nodes, then the height of the tree
Search
Searching on a 2-3 tree is basically the same as a binary search tree. On a 2-node you do the exact same thing. If less than the item go left, if greater than it go right. On a 3-node check if the item is less than , greater than , or if both of those are false, then go down the middle child.
Search is
For search trees in general, deleting is never very straightforward. There are often many cases to consider. Here’s an example of deleting a tree.
Whatever transformations are applied, you have to maintain a balanced and valid 2-3 tree.