2. Splay trees are self-adjusting binary search
trees that reduces the number of operations
required to access recently accessed nodes. It
achieves this property by bringing recently
accessed nodes closer to the root of the tree.
The Splay tree algorithm was invented by
Daniel D. Sleator and Robert E. Tarjan in 1985
3. A network router receives network packets at a
high rate from incoming connections and must
quickly decide on which outgoing wire to send
each packet, based on the IP address in the
packet. The router needs a big table (a map) that
can be used to look up an IP address and find out
which outgoing connection to use. If an IP
address has been used once, it is likely to be
used again, perhaps many times. Splay trees can
provide good performance in this situation.
Importantly, splay trees offer
amortized O(lg n) performance; a sequence
of M operations on an n-node splay tree
takes O(M lg n) time.
4. The surgery on the tree is done
using rotations, also called as splaying steps.
There are six different splaying steps.
1.Zig Rotation (Right Rotation)
2.Zag Rotation (Left Rotation)
3.Zig-Zag (Zig followed by Zag)
4.Zag-Zig (Zag followed by Zig)
5.Zig-Zig
6.Zag-Zag
5. The decision to choose one of the above
rotations depends on three things:
1. Does the node we are trying to rotate have a
grand-parent?
2. Is the node left or right child of the parent?
3. Is the parent left or right child of the grand-
parent?
6. If the node does not have a grand-parent, we
carry out a left rotation if it is the right child
of the parent; otherwise, we carry out a right
rotation.
7. If node is left of parent and parent is left of
grand-parent, we do a zig-zig right-
right rotation.
If node is left of parent but parent is right of
grand-parent, we do a zig-zag right-
left rotation.
If node is right of parent and parent is right of
grand-parent, we do a zig-zig left-left rotation.
Finally, if node is right of parent but parent is left
or grand-parent, we do a zig-zag left-
right rotation.
8. Step 1: Check whether tree is Empty.
Step 2: If tree is Empty then insert
the newNode as Root node and exit from the
operation.
step 3: If tree is not Empty then insert the
newNode as a leaf node using Binary Search
tree insertion logic.
Step 4: After insertion, Splay the newNode
9. a zig rotation is the same as a right rotation whereas the zag
step is the left rotation.
10. same as a double rotation in an AVL tree. Note that the target
element is lifted up by two levels.
11. This is also the same as a double rotation in an AVL tree.
Here again, the target element is lifted up by two levels.
12. The target element is lifted up by two levels in each case.
Zig-Zig is different from two successive right rotations;
zag-zag is different from two successive left rotations.
13.
14. There are two main ways of representing a
binary tree.
The first is using node objects that have
references to their children.
The second is using a regular array and
manipulating the index of the node to find its
children. The index of the left child of a node
is 2i + 12i+1 and the index of the right is 2i
+ 22i+2 where ii is the index of the parent.