Other Strategies to Maintain trees of shallow depth:
To maintain a depth that is logarithmic in n;
--Adjust the tree to maintain.
AVL Trees are a good example.
--AVL Trees require no additional info, but
their algorithms are much more complex.
Red Black Trees also use this method
--Red Black Trees are easier to implement,
but require an extra bit of per node.
Some NON-Binary Tree types include:
2-3 Trees
2-3-4 Trees
These trees are named for the number of
children each node may have. See the Dinish
and Mheta book for examples and discussion.
ADT SPLAY TREES
With M operations, n of which are inserts the Time bounds are related to m lg n Time / Operation Amortized over the whole of operations.
Each Operation should make the tree stalkier on the whole.
1)
P Q
/ \ / \
A Q ==> P C
/ \ / \
B C A B
2)
P Q
/ \ / \
Q C ==> A P
/ \ / \
A B B C
3)
GP Q
/ \ / \
A P P D
/ \ ==> / \
B Q GP C
/ \ / \
C D A B
4)
GP Q
/ \ / \
P D A P
/ \ ==> / \
Q C B GP
/ \ / \
A B C D
5)
GP Q
/ \ / \
A P GP P
/ \ ==> / \ / \
Q D A B C D
/ \
B C
6)
GP Q
/ \ / \
P D P GP
/ \ ==> / \ / \
A Q A B C D
/ \
B C
Using these primitives, you now must follow these rules to keep the tree in good form:
The find operation is of key importance because it will promote speed based on the locality of our last find. Ie; if we are interested in a few elements more so than others, we would have beautiful performace with the splay tree. For example:
1)
P Q
/ \ / \
A Q ==> P C
/ \ / \
B C A B
P Q
/ \ / \
Q C ==> A P
/ \ / \
A B B C
3) GP Q
/ \ / \
A P P D
/ \ ==> / \
B Q GP C
/ \ / \
C D A B
4) GP Q
/ \ / \
P D A P
/ \ ==> / \
Q C B GP
/ \ / \
A B C D
5) GP Q
/ \ / \
A P GP P
/ \ ==> / \ / \
Q D A B C D
/ \
B C
6) GP Q
/ \ / \
P D P GP
/ \ ==> / \ / \
A Q A B C D
/ \
B C
Assume we will perform
ST.Insert(1);
The inserts, and subsequent splays will generate a long
skinny tree. Let's pickup at the insert of 3:
[ For clarity, the intermediate tree configurations
are shown as each element is inserted, then splayed ]
upon ST.Insert(3):
2
/ \
1 3
the insert subroutine to splay on 3 would generate:
3
/ \
2
/ \
1
upon ST.Insert(4):
3
/ \
2 4
/ \
1
the insert subroutine to splay on 4 would generate:
4
/ \
3
/ \
2
/ \
1
upon ST.Insert(5):
4
/ \
3 5
/ \
2
/ \
1
the insert subroutine to splay on 5 would generate:
5
/ \
4
/ \
3
/ \
2
/ \
1
upon ST.Insert(6):
5
/ \
4 6
/ \
3
/ \
2
/ \
1
the insert subroutine to splay on 6 would generate:
6
/ \
5
/ \
4
/ \
3
/ \
2
/ \
1
Then, if we were to execute ST.Find(1);
The tree would morph:
First;
6
/ \
5
/ \
4
/ \
1
/ \
2
/ \
3
Then;
6
/ \
1
/ \
4
/ \
2 5
/ \
3
Then finally as 1 makes its way to the top:
1
/ \
6
/ \
4
/ \
2 5
/ \
3
We have our final tree.Let F1, F2, F3,...,Fm be a sequence of find operations on a BST. Let T* be the time to perform F1,..,Fm on the optimal BST. There exists a constant C such that: This sequence of operations performing F1,...,F1 on a splay tree takes less than or equal to CT*.