Two-three trees have the following two invariants:
The method for inserting is as follows:
Here's an example consisting of instructors of introductory courses, cs advisors, and staff:
carl ; doug
/ | \
avrim danny mark;presley
/ \ / \ / | \
allan bruce cathy dave maria phil terri
Now let's insert a few items.
doug
/ \
carl mark
/ \ / \
avrim danny katheryn presley
/ \ / \ / \ / \
allan bruce cathy dave emelio maria phil terri
Insert a few more:
doug
/ \
carl mark
/ \ / \
avrim danny katheryn mo;presley
/ \ / \ / \ / | \
allan bruce cathy dave emelio maria mary phil terri
Idea is that in a binary search tree, can go back and forth between these two:
x y
/ \ / \
T1 y <------> x T3
/ \ / \
T2 T3 T1 T2
Draw a binary search tree, and see what would happen if
you did a rotation in some places. For example:
1 3
/ \ / \
0 3 left rotation at node 1 1 5
/ \ ------------------> / \ / \
2 5 0 2 4 6
/ \
4 6
Notice that this rotation transformed a tree with depth 3 into a tree
with depth 2, thus improving its balance.
Let's write the code to perform a left rotation:
struct tree_node {
int value;
tree_node *left;
tree_node *right;
};
tree_node*
rotate_left(tree_node *y) {
tree_node *x = y->right;
y->right = x->left;
x->left = y;
return x;
}
Here's the general picture. We start with:
o y
/ \
o o x
/_\ / \
A o o
/_\ /_\
B C
After x = y->right and y->right = x->left, we have:
o y o x
/ \ \
o o o
/_\ /_\ /_\
A B C
After x->left = y we have:
o x
/ \
o y o
/ \ /_\
o o C
/_\ /_\
A B