SlideShare a Scribd company logo
Splay Trees
https://docs.google.com/presentation/d/1J8sIJDKi9DfnZJ5-VMeGtPqQ8nFfJ_mQVxz4oPEj-KA/edit#slide=id.p
Fundamental Concept
Carefully rearrange (using tree rotation
operation) the tree so that the recently
accessed elements are brought to the top and
at the same time, the tree is also
approximately balanced.
Splay Tree
Splay is a binary search tree in which recently accessed
keys are quick to access again.
When an element is accessed, it is moved to the root using
series of rotation operations so that it is quick to access
again.
The tree is also approximately balanced while rearranging.
The process of rearranging is called splaying.
Operations
• Splay tree includes all the operations of BST along with splay
operation.
• Splay operation rearranges the tree so that the element is placed
at the top of the tree.
• Let x be the accessed node and let p be the parent node of x.
Three types of splay steps:
• Zig step (one left rotation or one right rotation)
o carried out when p is the root (last step in the splay operation)
• Zig-Zig step (left-left rotation or right-right rotation)
o carried out when p is not the root and both x,p are either right
or left children.
• Zig-Zag step (left-right rotation or right-left rotation)
o carried out when p is not the root and x is left and p is right or
vice versa
Operations (contd)
Insertion
•Insert the node using the normal BST insert procedure
•splay the newly inserted node to the root
Deletion
•delete using normal BST delete procedure
•splay the parent of removed node to the root
Search
•Search using normal BST search procedure
•splay the accessed item to the root
Splay Operation - Pseudo code
void splay(struct node *x)
{
struct node *p,*g;
/*check if node x is the root node*/
if (x->parent == NULL) { root = x; return; }
/*Performs Zig step*/
else if ( x->parent == root)
{
if(x == x->parent->left) rightrotation(root);
else
leftrotation(root);
}
else
{
p = x->parent; /*now points to parent of x*/
g = p->parent; /*now points to grand parent of x*/
/*when x is left and x's parent is left*/
if(x==p->left and p==g->left) //Zig-zig
{ rightrotation(g); rightrotation(p); }
/*step when x is right and x's parent is right*/
else if (x==p->right and p==g->right) //Zig-zig
{ leftrotation(g); leftrotation(p); }
/*when x's is right and x's parent is left*/
else if (x==p->right and p==g->left) // Zig-zag
{ leftrotation(p); rightrotation(g); }
/*when x's is left and x's parent is right*/
else if (x==p->left and p==g->right) //Zig-zag
{ rightrotation(p); leftrotation(g); }
splay(x);
}
Splay Trees and B-Trees - Lecture 9
8
• Let X be a non-root node with ≥ 2 ancestors.
• P is its parent node.
• G is its grandparent node.
P
G
X
G
P
X
G
P
X
G
P
X
Splay Tree Terminology
Zig-Zig and Zig-Zag
Splay Trees and B-Trees - Lecture 9
9
4
G 5
1 P
Zig-zag
G
P 5
X 2
Zig-zig
X
Parent and grandparent
in same direction.
Parent and grandparent
in different directions.
Zig at depth 1 (root)
“Zig” is just a single rotation, as in an AVL tree
Let R be the node that was accessed (e.g. using
Find)
ZigFromLeft moves R to the top →faster access
next time
Splay Trees and B-Trees - Lecture 9
10
ZigFromLeft
root
Zig at depth 1
Suppose Q is now accessed using Find
ZigFromRight moves Q back to the top
Splay Trees and B-Trees - Lecture 9
11
ZigFromRight
root
Zig-Zag operation
“Zig-Zag” consists of two rotations of the opposite
direction (assume R is the node that was accessed)
Splay Trees and B-Trees - Lecture 9
12
(ZigFromRight) (ZigFromLeft)
ZigZagFromLeft
Zig-Zig operation
 “Zig-Zig” consists of two single rotations of the same direction (R
is the node that was accessed)
Splay Trees and B-Trees - Lecture 9
13
(ZigFromLeft) (ZigFromLeft)
ZigZigFromLeft
Decreasing depth - "autobalance"
Splay Trees and B-Trees - Lecture 9
14
Find(T) Find(R)
Splay Tree Insert and Delete
Insert x
Insert x as normal then splay x to root.
Delete x
Splay x to root and remove it. (note: the node
does not have to be a leaf or single child node
like in BST delete.) Two trees remain, right
subtree and left subtree.
Splay the max in the left subtree to the root
Attach the right subtree to the new root of the left
subtree.
Splay Trees and B-Trees - Lecture 9
15
Example Insert
 Inserting in order 1,2,3,…,8
 Without self-adjustment
Splay Trees and B-Trees - Lecture 9
16
1
2
3
4
5
6
7
8
O(n2
) time for n Insert
With Self-Adjustment
Splay Trees and B-Trees - Lecture 9
17
1
2
1 2
1
ZigFromRight
2
1 3
ZigFromRight
2
1
3
1
2
3
With Self-Adjustment
Splay Trees and B-Trees - Lecture 9
18
ZigFromRight
2
1
34
4
2
1
3
4
Each Insert takes O(1) time therefore O(n) time for n Insert!!
Example Deletion
Splay Trees and B-Trees - Lecture 9
19
10
155
201382
96
10
15
5
2013
8
2 96
splay
10
15
5
2013
2 96
remove
10
15
5
2013
2 9
6
Splay (zig)
attach
(Zig-Zag)
Advantages
 Simple implementation (self optimizing)
 Average case performance is as efficient as other trees
 No need to store any bookkeeping data
 Allows access to both the previous and new versions after an
update(Persistent data structure)
 Stable sorting
How many squares can you create in this figure by connecting any 4 dots (the corners of a
square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell
triangles, and 1 sixteen-cell triangle.
GUIDED READING
1.8 splay tree
ASSESSMENT
After inserting 56,87,56,43,98 and 77 the
element 22 is inserted. Then the root node of
the resultant splay tree is
1. 87
2. 56
3. 22
4. 77
CONTD..
The Zig-zig corresponds to
1.left rotation followed by right rotation
2.right rotation followed by left rotation
3.left rotation followed by left rotation
4.only left rotation.
CONTD..
After inserting, 7,6,5,4,3,2 and 1 into a normal
binary search tree, a splay operation is
performed at node 1. Then, the left and right
child of the root node of the resultant tree are
1.6 and null respectively
2.null and 6 respectively
3.5 and null respectively
4.null and 5 respectively
CONTD..
Let x be the accessed node and p be the parent
node of x. Then Zig-Zag operation is performed
When
1.p is the root
2.p is not the root and both x,p are left child nodes
3.p is not the root and both x,p are right child nodes
4.p is not the root and x is left and p is right or vice versa
CONTD..
After inserting, 7,6,5,4,3,2 and 1 into splay tree,
a search operation is performed at
node1. Then, the left and right child of the root
node of the resultant tree are
1. null and 3 respectively
2. null and 4 respectively
3. null and 6 respectively
4. 2 and 5 respectively

More Related Content

PPTX
Binary Tree in Data Structure
PPTX
single linked list
PPTX
Threaded Binary Tree.pptx
PPT
Binary search tree(bst)
PDF
Binary search tree operations
PPTX
Graph traversals in Data Structures
PPTX
Trees (data structure)
PPTX
Binary Search Tree in Data Structure
Binary Tree in Data Structure
single linked list
Threaded Binary Tree.pptx
Binary search tree(bst)
Binary search tree operations
Graph traversals in Data Structures
Trees (data structure)
Binary Search Tree in Data Structure

What's hot (20)

PPTX
Binary Search Tree
PPT
Data Structure and Algorithms Binary Search Tree
PPT
Queue Data Structure
PPTX
Trees in data structures
PPTX
trees in data structure
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Doubly Linked List
PPTX
Logical and shift micro operations
PPT
Binary search trees
PPTX
Digital Search Tree
PPTX
Arrays in Data Structure and Algorithm
PPTX
Binary Search Tree
PPTX
Infix to postfix conversion
PPTX
sorting and its types
PDF
Queue as data_structure
PPTX
Linked List
PPTX
Doubly linked list
PPTX
Threaded Binary Tree
PPTX
Splay tree
Binary Search Tree
Data Structure and Algorithms Binary Search Tree
Queue Data Structure
Trees in data structures
trees in data structure
BINARY TREE REPRESENTATION.ppt
Doubly Linked List
Logical and shift micro operations
Binary search trees
Digital Search Tree
Arrays in Data Structure and Algorithm
Binary Search Tree
Infix to postfix conversion
sorting and its types
Queue as data_structure
Linked List
Doubly linked list
Threaded Binary Tree
Splay tree
Ad

Viewers also liked (20)

PPT
Splay trees
PPT
Splay tree
PPTX
B tree &
PPT
stack and queue array implementation in java.
PPT
B trees and_b__trees
PPTX
B tree long
PPT
B tree
PPSX
Data Structure (Dynamic Array and Linked List)
PPTX
stack & queue
PPT
1.9 b trees 02
PPT
4.2 bst 02
PPT
4.1 webminig
PPT
1.9 b trees eg 03
PPT
2.4 mst prim &kruskal demo
PPT
5.2 divede and conquer 03
DOCX
nhận thiết kế clip quảng cáo giá tốt
PPT
5.4 randamized algorithm
PPT
4.4 hashing02
PDF
문제는 한글이 잘 구현되는가?
PPT
Top Forex Brokers
Splay trees
Splay tree
B tree &
stack and queue array implementation in java.
B trees and_b__trees
B tree long
B tree
Data Structure (Dynamic Array and Linked List)
stack & queue
1.9 b trees 02
4.2 bst 02
4.1 webminig
1.9 b trees eg 03
2.4 mst prim &kruskal demo
5.2 divede and conquer 03
nhận thiết kế clip quảng cáo giá tốt
5.4 randamized algorithm
4.4 hashing02
문제는 한글이 잘 구현되는가?
Top Forex Brokers
Ad

Similar to 1.8 splay tree (20)

DOCX
5220191CS146 Data Structures and AlgorithmsC.docx
PPTX
Lecture_10 - Revised.pptx
PPT
Binary Search Tree
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
PPTX
Trees in data structure
PDF
Skiena algorithm 2007 lecture05 dictionary data structure trees
PPTX
Balanced Tree(AVL Tree,Red Black Tree)
PPTX
Lecture 14 splay tree
PPTX
Binary tree operations in data structures
PPTX
Splay trees by NIKHIL ARORA (www.internetnotes.in)
PPT
Chapter 9 ds
PPTX
Splay Tree Presentation Slides
PDF
Leftlist Heap-1.pdf
PPTX
Splay Trees Data Structure and Algorithm (DSA).pptx
PPTX
Splay Trees Data Structure and Algorithm (DSA).pptx
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
PPT
UNIT II.ppt
5220191CS146 Data Structures and AlgorithmsC.docx
Lecture_10 - Revised.pptx
Binary Search Tree
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
Trees in data structure
Skiena algorithm 2007 lecture05 dictionary data structure trees
Balanced Tree(AVL Tree,Red Black Tree)
Lecture 14 splay tree
Binary tree operations in data structures
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Chapter 9 ds
Splay Tree Presentation Slides
Leftlist Heap-1.pdf
Splay Trees Data Structure and Algorithm (DSA).pptx
Splay Trees Data Structure and Algorithm (DSA).pptx
Balanced Tree (AVL Tree & Red-Black Tree)
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
UNIT II.ppt

More from Krish_ver2 (20)

PPT
5.5 back tracking
PPT
5.5 back track
PPT
5.5 back tracking 02
PPT
5.4 randomized datastructures
PPT
5.4 randomized datastructures
PPT
5.3 dynamic programming 03
PPT
5.3 dynamic programming
PPT
5.3 dyn algo-i
PPT
5.2 divede and conquer 03
PPT
5.2 divide and conquer
PPT
5.1 greedyyy 02
PPT
5.1 greedy
PPT
5.1 greedy 03
PPT
4.4 hashing
PPT
4.4 hashing ext
PPT
4.4 external hashing
PPT
4.2 bst
PPT
4.2 bst 03
PPT
4.1 sequentioal search
PPT
3.9 external sorting
5.5 back tracking
5.5 back track
5.5 back tracking 02
5.4 randomized datastructures
5.4 randomized datastructures
5.3 dynamic programming 03
5.3 dynamic programming
5.3 dyn algo-i
5.2 divede and conquer 03
5.2 divide and conquer
5.1 greedyyy 02
5.1 greedy
5.1 greedy 03
4.4 hashing
4.4 hashing ext
4.4 external hashing
4.2 bst
4.2 bst 03
4.1 sequentioal search
3.9 external sorting

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Insiders guide to clinical Medicine.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Lesson notes of climatology university.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
master seminar digital applications in india
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
Pre independence Education in Inndia.pdf
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Lesson notes of climatology university.
Renaissance Architecture: A Journey from Faith to Humanism
master seminar digital applications in india
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

1.8 splay tree

  • 2. Fundamental Concept Carefully rearrange (using tree rotation operation) the tree so that the recently accessed elements are brought to the top and at the same time, the tree is also approximately balanced.
  • 3. Splay Tree Splay is a binary search tree in which recently accessed keys are quick to access again. When an element is accessed, it is moved to the root using series of rotation operations so that it is quick to access again. The tree is also approximately balanced while rearranging. The process of rearranging is called splaying.
  • 4. Operations • Splay tree includes all the operations of BST along with splay operation. • Splay operation rearranges the tree so that the element is placed at the top of the tree. • Let x be the accessed node and let p be the parent node of x. Three types of splay steps: • Zig step (one left rotation or one right rotation) o carried out when p is the root (last step in the splay operation) • Zig-Zig step (left-left rotation or right-right rotation) o carried out when p is not the root and both x,p are either right or left children. • Zig-Zag step (left-right rotation or right-left rotation) o carried out when p is not the root and x is left and p is right or vice versa
  • 5. Operations (contd) Insertion •Insert the node using the normal BST insert procedure •splay the newly inserted node to the root Deletion •delete using normal BST delete procedure •splay the parent of removed node to the root Search •Search using normal BST search procedure •splay the accessed item to the root
  • 6. Splay Operation - Pseudo code void splay(struct node *x) { struct node *p,*g; /*check if node x is the root node*/ if (x->parent == NULL) { root = x; return; } /*Performs Zig step*/ else if ( x->parent == root) { if(x == x->parent->left) rightrotation(root); else leftrotation(root); }
  • 7. else { p = x->parent; /*now points to parent of x*/ g = p->parent; /*now points to grand parent of x*/ /*when x is left and x's parent is left*/ if(x==p->left and p==g->left) //Zig-zig { rightrotation(g); rightrotation(p); } /*step when x is right and x's parent is right*/ else if (x==p->right and p==g->right) //Zig-zig { leftrotation(g); leftrotation(p); } /*when x's is right and x's parent is left*/ else if (x==p->right and p==g->left) // Zig-zag { leftrotation(p); rightrotation(g); } /*when x's is left and x's parent is right*/ else if (x==p->left and p==g->right) //Zig-zag { rightrotation(p); leftrotation(g); } splay(x); }
  • 8. Splay Trees and B-Trees - Lecture 9 8 • Let X be a non-root node with ≥ 2 ancestors. • P is its parent node. • G is its grandparent node. P G X G P X G P X G P X Splay Tree Terminology
  • 9. Zig-Zig and Zig-Zag Splay Trees and B-Trees - Lecture 9 9 4 G 5 1 P Zig-zag G P 5 X 2 Zig-zig X Parent and grandparent in same direction. Parent and grandparent in different directions.
  • 10. Zig at depth 1 (root) “Zig” is just a single rotation, as in an AVL tree Let R be the node that was accessed (e.g. using Find) ZigFromLeft moves R to the top →faster access next time Splay Trees and B-Trees - Lecture 9 10 ZigFromLeft root
  • 11. Zig at depth 1 Suppose Q is now accessed using Find ZigFromRight moves Q back to the top Splay Trees and B-Trees - Lecture 9 11 ZigFromRight root
  • 12. Zig-Zag operation “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed) Splay Trees and B-Trees - Lecture 9 12 (ZigFromRight) (ZigFromLeft) ZigZagFromLeft
  • 13. Zig-Zig operation  “Zig-Zig” consists of two single rotations of the same direction (R is the node that was accessed) Splay Trees and B-Trees - Lecture 9 13 (ZigFromLeft) (ZigFromLeft) ZigZigFromLeft
  • 14. Decreasing depth - "autobalance" Splay Trees and B-Trees - Lecture 9 14 Find(T) Find(R)
  • 15. Splay Tree Insert and Delete Insert x Insert x as normal then splay x to root. Delete x Splay x to root and remove it. (note: the node does not have to be a leaf or single child node like in BST delete.) Two trees remain, right subtree and left subtree. Splay the max in the left subtree to the root Attach the right subtree to the new root of the left subtree. Splay Trees and B-Trees - Lecture 9 15
  • 16. Example Insert  Inserting in order 1,2,3,…,8  Without self-adjustment Splay Trees and B-Trees - Lecture 9 16 1 2 3 4 5 6 7 8 O(n2 ) time for n Insert
  • 17. With Self-Adjustment Splay Trees and B-Trees - Lecture 9 17 1 2 1 2 1 ZigFromRight 2 1 3 ZigFromRight 2 1 3 1 2 3
  • 18. With Self-Adjustment Splay Trees and B-Trees - Lecture 9 18 ZigFromRight 2 1 34 4 2 1 3 4 Each Insert takes O(1) time therefore O(n) time for n Insert!!
  • 19. Example Deletion Splay Trees and B-Trees - Lecture 9 19 10 155 201382 96 10 15 5 2013 8 2 96 splay 10 15 5 2013 2 96 remove 10 15 5 2013 2 9 6 Splay (zig) attach (Zig-Zag)
  • 20. Advantages  Simple implementation (self optimizing)  Average case performance is as efficient as other trees  No need to store any bookkeeping data  Allows access to both the previous and new versions after an update(Persistent data structure)  Stable sorting
  • 21. How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot? TRIANGLES: How many triangles are located in the image below?
  • 22. There are 11 squares total; 5 small, 4 medium, and 2 large. 27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.
  • 25. ASSESSMENT After inserting 56,87,56,43,98 and 77 the element 22 is inserted. Then the root node of the resultant splay tree is 1. 87 2. 56 3. 22 4. 77
  • 26. CONTD.. The Zig-zig corresponds to 1.left rotation followed by right rotation 2.right rotation followed by left rotation 3.left rotation followed by left rotation 4.only left rotation.
  • 27. CONTD.. After inserting, 7,6,5,4,3,2 and 1 into a normal binary search tree, a splay operation is performed at node 1. Then, the left and right child of the root node of the resultant tree are 1.6 and null respectively 2.null and 6 respectively 3.5 and null respectively 4.null and 5 respectively
  • 28. CONTD.. Let x be the accessed node and p be the parent node of x. Then Zig-Zag operation is performed When 1.p is the root 2.p is not the root and both x,p are left child nodes 3.p is not the root and both x,p are right child nodes 4.p is not the root and x is left and p is right or vice versa
  • 29. CONTD.. After inserting, 7,6,5,4,3,2 and 1 into splay tree, a search operation is performed at node1. Then, the left and right child of the root node of the resultant tree are 1. null and 3 respectively 2. null and 4 respectively 3. null and 6 respectively 4. 2 and 5 respectively