0% found this document useful (0 votes)
21 views17 pages

CHPT 3 Splay Trees

Splay trees are a type of self-balancing binary search tree where the recently accessed elements are moved closer to the root of the tree via rotations. Searches start like in a regular BST, but after finding the target node, it is "splayed" to the root through a series of rotations. There are different cases for the rotations depending on whether the target node is a left-left, right-right, left-right, or right-left grandchild of its parent. This process improves future access times by prioritizing frequently accessed elements at the top of the tree.

Uploaded by

ankitjain147
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views17 pages

CHPT 3 Splay Trees

Splay trees are a type of self-balancing binary search tree where the recently accessed elements are moved closer to the root of the tree via rotations. Searches start like in a regular BST, but after finding the target node, it is "splayed" to the root through a series of rotations. There are different cases for the rotations depending on whether the target node is a left-left, right-right, left-right, or right-left grandchild of its parent. This process improves future access times by prioritizing frequently accessed elements at the top of the tree.

Uploaded by

ankitjain147
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Splay Trees

6
v
3 8
z
4

Splay Trees 1
Splay Trees are
Binary Search Trees (20,Z) all the keys in the blue
note that two keys of region are  20
equal value may be well-
separated
(10,A) (35,R)
BST Rules:
 items stored only at internal (14,J)
(7,T) (21,O) (37,P)
nodes
 keys stored at nodes in the
left subtree of v are less (1,Q) (8,N) (36,L) (40,X)
than or equal to the key
stored at v
 keys stored at nodes in the (1,C) (5,H) (7,P) (10,U)
right subtree of v are
greater than or equal to the
key stored at v all the keys in the yellow
(2,R) (5,G)
region are  20
An inorder traversal will
return the keys in order (5,I) (6,Y)

Splay Trees 2
Searching in a Splay Tree:
Starts the Same as in a BST
(20,Z)
Search proceeds down
the tree to found item (10,A) (35,R)
or an external node.
Example: Search for (7,T)
(14,J)
(21,O) (37,P)
time with key 11.
(1,Q) (8,N) (36,L) (40,X)

(1,C) (5,H) (7,P) (10,U)

(2,R) (5,G)

(5,I) (6,Y)

Splay Trees 3
Example Searching in a BST,
continued
(20,Z)

search for key 8, ends at


an internal node. (10,A) (35,R)

(14,J)
(7,T) (21,O) (37,P)

(1,Q) (8,N) (36,L) (40,X)

(1,C) (5,H) (7,P) (10,U)

(2,R) (5,G)

(5,I) (6,Y)

Splay Trees 4
Splay Trees do Rotations after
Every Operation (Even Search)
new operation: splay
splaying moves a node to the root using rotations

 right rotation  left rotation


 makes the left child x of a node y into  makes the right child y of a node x
y’s parent; y becomes the right child into x’s parent; x becomes the left
of x child of y

y x
a right rotation about y a left rotation about x

x y
x y
T3 T1

y x
T1 T2 T2 T3
T1 T3

(structure of tree above y (structure of tree above x


is not modified) T2 T3 is not modified) T1 T2

Splay Trees 5
Splaying:  “x is a left-left grandchild” means x is a left child of its
parent, which is itself a left child of its parent
 p is x’s parent; g is p’s parent
start with
node x
is x a left-left
is x the yes
stop grandchild? zig-zig
root?
yes right-rotate about g,
no right-rotate about p
is x a right-right
is x a child of no grandchild? zig-zig
the root? left-rotate about g,
yes
left-rotate about p
yes
is x a right-left
grandchild? zig-zag
is x the left
no yes left-rotate about p,
child of the
right-rotate about g
root?
is x a left-right
yes zig zig grandchild? zig-zag
right-rotate left-rotate about yes right-rotate about p,
about the root the root left-rotate about g
Splay Trees 6
Visualizing the
Splaying Cases zig-zag x
z
z
y z y
y
T4 T1 x
x T4 T1 T2 T3 T4
T3
zig-zig T2 T3
T1 T2

x y
zig
x x
T1 y
T4
z w w y
T2
T3

T3 T4 T1 T2 T3 T4
T1 T2
Splay Trees 7
Splaying Example (10,A)
(20,Z)

(35,R)

let x = (8,N) g

 x is the right child of its parent, p (7,T)


(14,J)
(21,O) (37,P)
1.
(before
which is the left child of the
(1,Q) (8,N) rotating)
grandparent
(36,L) (40,X)

x
 left-rotate around p, then right-
(1,C)
rotate around g
(5,H) (7,P) (10,U)

(2,R) (5,G)
(20,Z)
(5,I) (6,Y)

(20,Z)
g (10,A) (35,R)

(14,J) (35,R)
x (8,N)
(21,O) (37,P)
x (8,N)

g
p (7,T) (10,U) (36,L) (40,X) p (7,T) (10,A) (21,O) (37,P)

(1,Q) (7,P) (1,Q) (7,P) (14,J) (36,L) (40,X)

(1,C)
2. (1,C) (10,U)
3.
(5,H) (5,H)
(after first rotation)
(after second
(2,R) (5,G) (2,R) (5,G)
rotation)
(5,I) (6,Y) (5,I) (6,Y) x is not yet the root, so
Splay Trees we splay again 8
Splaying Example, Continued
now x is the left child of the root
 right-rotate around root
(20,Z)

(35,R)
x (8,N)

(7,T) (10,A) (21,O) (37,P)

(1,Q) (7,P) (14,J) (36,L) (40,X)


x (8,N)
2.
(1,C) (5,H) (10,U)
(7,T) (20,Z) (after rotation)
1.
(2,R) (5,G) (before applying (1,Q) (7,P)
(10,A) (35,R)

rotation)
(5,I) (6,Y)
(1,C) (5,H) (14,J)
(21,O) (37,P)

(10,U) (36,L) (40,X)


(2,R) (5,G)

(5,I) (6,Y)

x is the root, so stop


Splay Trees 9
Example Result
(20,Z)

(10,A) (35,R)

of Splaying before
(7,T)
(14,J)
(21,O) (37,P)

tree might not be more balanced (1,Q) (8,N) (36,L) (40,X)


e.g. splay (40,X)
 before, the depth of the shallowest leaf is(1,C) (5,H) (10,U)
(7,P)
3 and the deepest is 7
 after, the depth of shallowest leaf is 1 (2,R) (5,G)
and deepest is 8 (40,X)
(5,I) (6,Y)
(20,Z)
(20,Z)

(10,A) (37,P)
(10,A) (40,X)

(14,J) (35,R)
(14,J) (37,P) (7,T)
(7,T)

(1,Q) (8,N)
(1,Q) (8,N) (35,R) (21,O) (36,L)

(1,C) (5,H) (7,P) (10,U)


(1,C) (5,H) (7,P) (10,U) (21,O) (36,L)

(2,R) (5,G)
(2,R) (5,G)
after first splay after second
(6,Y)
(5,I) (6,Y) splay
(5,I)
Splay Trees 10
Splay Tree Definition
a splay tree is a binary search tree where a
node is splayed after it is accessed (for a
search or update)
 deepest internal node accessed is splayed
 splaying costs O(h), where h is height of the tree
– which is still O(n) worst-case
 O(h) rotations, each of which is O(1)

Splay Trees 11
Splay Trees & Ordered
Dictionaries
which nodes are splayed after each operation?

method splay node

if key found, use that node


findElement
if key not found, use parent of ending external node

insertElement use the new node containing the item inserted

use the parent of the internal node that was actually


removed from the tree (the parent of the node that the
removeElement
removed item was swapped with)

Splay Trees 12
Amortized Analysis of
Splay Trees
Running time of each operation is proportional to time
for splaying.
Define rank(v) as the logarithm (base 2) of the number
of nodes in subtree rooted at v.
Costs: zig = $1, zig-zig = $2, zig-zag = $2.
Thus, cost for playing a node at depth d = $d.
Imagine that we store rank(v) cyber-dollars at each
node v of the splay tree (just for the sake of analysis).

Splay Trees 13
Cost per zig
y zig

x x
T4
w w y
T3

T1 T2 T3 T4
T1 T2

Doing a zig at x costs at most rank’(x) - rank(x):


 cost = rank’(x) + rank’(y) - rank(y) - rank(x)

< rank’(x) - rank(x).

Splay Trees 14
Cost per zig-zig and zig-zag
z x
y
T4
zig-zig T1 y
x z
T3 T2

T1 T2 T3 T4

Doing a zig-zig or zig-zag at x costs at most


3(rank’(x) - rank(x)) - 2.
 Proof: See Theorem 3.9, Page 192.

z zig-zag x

y z y
T1 x
T4 T1 T2 T3 T4

T2 T3

Splay Trees 15
Cost of Splaying
Cost of splaying a node x at depth d of a tree
rooted at r:
 at most 3(rank(r) - rank(x)) - d + 2:
 Proof: Splaying x takes d/2 splaying substeps:
d /2
cost   cost i
i 1
d /2
  (3( rank i ( x )  rank i 1 ( x ))  2)  2
i 1

 3( rank ( r )  rank 0 ( x ))  2( d / d )  2
 3( rank ( r )  rank ( x ))  d  2.
Splay Trees 16
Performance of
Splay Trees
Recall: rank of a node is logarithm of its size.
Thus, amortized cost of any splay operation is
O(log n).
In fact, the analysis goes through for any
reasonable definition of rank(x).
This implies that splay trees can actually adapt
to perform searches on frequently-requested
items much faster than O(log n) in some
cases. (See Theorems 3.10 and 3.11.)

Splay Trees 17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy