Splay Trees

COMP2521 20T2 ♢ Splay Trees [0/14]
❖ Splay Trees

Splay tree = one style of "self-balancing" tree ...

Splay tree insertion modifies insertion-at-root method:

The idea: appropriate double-rotations improve tree balance.

Splay tree implementations also do rotation-in-search:

COMP2521 20T2 ♢ Splay Trees [1/14]
❖ ... Splay Trees

Cases for splay tree double-rotations:

[Diagram:Pics/splay-cases.png]

COMP2521 20T2 ♢ Splay Trees [2/14]
❖ ... Splay Trees

Actions for splay tree double-rotations:

COMP2521 20T2 ♢ Splay Trees [3/14]
❖ ... Splay Trees

Example: double-rotation case for left-child of left-child:

COMP2521 20T2 ♢ Splay Trees [4/14]
❖ ... Splay Trees

Example: double-rotation case for right-child of left-child:

COMP2521 20T2 ♢ Splay Trees [5/14]
❖ Splay Tree Insertion Algorithm


In describing splay trees, it is convenient to use abbreviations

tl  = tr->left = left(tree)
tr  = tree->right = right(tree)
tll = tree->left->left
tlr = tree->left->right
trr = tree->right->right 
trl = tree->right->left

These could be implemented using #define in C, e.g.

#define  tll  t->left->left

COMP2521 20T2 ♢ Splay Trees [6/14]
❖ ... Splay Tree Insertion Algorithm

Algorithm for splay tree insertion:

insertSplay(tree,item):
|  Input  tree, item
|  Output tree with item splay-inserted
|
|  if tree is empty then return new node containing item
|  else if item=data(tree) then return tree
|  else if item < data(tree) then
|  |  if left(tree) is empty then
|  |     left(tree) = new node containing item
|  |  else if item < data(left(tree)) then
|  |        // Case 1: left-child of left-child
|  |     tll = insertSplay(tll),item)
|  |     tree = rotateRight(tree)
|  |  else  // Case 2: right-child of left-child
|  |     tlr = insertSplay(tlr,item)
|  |     left(tree) = rotateLeft(left(tree))
|  |  end if
|  |  return rotateRight(tree)
|  else if item > data(tree) then
|  |  if right(tree) is empty then
|  |     right(tree) = new node containing item
|  |  else if item < data(right(tree)) then
|  |        // Case 3: left-child of right-child
|  |     trl = insertSplay(trl,item)
|  |     right(tree) = rotateRight(right(tree))
|  |  else  // Case 4: right-child of right-child
|  |     trr = insertSplay(trr,item)
|  |     tree = rotateLeft(tree)
|  |  end if
|  |  return rotateLeft(tree)
|  end if
COMP2521 20T2 ♢ Splay Trees [7/14]
❖ Insertion into Splay Trees


Example: insert 18 into this splay tree:

[Diagram:Pics/tree1.png]

COMP2521 20T2 ♢ Splay Trees [8/14]
❖ ... Insertion into Splay Trees


New node is moved to root via right then left rotation

[Diagram:Pics/tree5.png]

COMP2521 20T2 ♢ Splay Trees [9/14]
❖ Searching in Splay Trees

Searching in splay trees:

searchSplay(tree,item):
|  Input  tree, item
|  Output address of item if found in tree
|         NULL otherwise
|
|  if tree=NULL then
|     return NULL
|  else
|  |  tree = splay(tree,item)
|  |  if data(tree)=item then
|  |     return tree
|  |  else
|  |     return NULL
|  |  end if
|  end if

splay() is similar to insertSplay(), but doesn't add a node
moves item to root if found, moves nearest node to root if not found

COMP2521 20T2 ♢ Splay Trees [10/14]
❖ ... Searching in Splay Trees


Example: search for 22 in the splay tree

[Diagram:Pics/tree1.png]

How does this affect the tree?

COMP2521 20T2 ♢ Splay Trees [11/14]
❖ ... Searching in Splay Trees


Found node is moved to root via right then left rotations

[Diagram:Pics/tree3.png]

COMP2521 20T2 ♢ Splay Trees [12/14]
❖ Splay Tree Performance


Analysis of splay tree performance:

Derivation of the above beyond the scope of this course.
COMP2521 20T2 ♢ Splay Trees [13/14]
❖ ... Splay Tree Performance

Implications of performance analysis

i.e. gives good overall (amortized) cost.

But still has worst-case search cost O(n)
COMP2521 20T2 ♢ Splay Trees [14/14]


Produced: 15 Jun 2020