SlideShare a Scribd company logo
SPLAY TREE
By,
Ambooj Yadav, Anam Iqbal & Hina Firdaus
M.Tech CSE Ist year, Ist Semester
Advance Datastructure Algorithm and Analysis
Jamia Hamdard University, New Delhi-62
Introduction
Definition:-
• Splay trees another variation of the binary search tree, invented by Robert
Tarjan and Daniel Sleator.
• Splay means “to rearrange, so that the desired element is placed at the
root"
• Splay trees support all the operations of binary trees.
• A splay tree is a self-adjusting binary search tree
Novel characteristics
• It performs basic operations such as insertion, look-up and removal.
• Does not require any accounting information (color, level, height, etc.)
• Worst case for any single operation: O(N)
• Average case for all operation: O(log N)
• Frequently-accessed keys are moved up so that they are near the root.
• Easier to perform comparison with 2-3-4 trees.
General question
1. What is difference between AVL trees and
splay trees?
2. On what basis do we select these tress?
3. What are positive's and negative's of these
trees?
4. What are the performances of these trees in
terms of big O notation?
1. What is difference between AVL
trees and splay trees?
Splay trees always try to be
balanced after every operation.
Because of this rest operation
take less time to perform
They are cool…
2. On what basis do we select these
tress?
• Splay trees are always better than binary search trees
when, your application deals with a lot of data in the
tree but, will need access to a subset of the data very
frequently than others.
• In this case the data you access frequently will come
near the root as a result of the splay.
• Because of this, any node can then be accessed with
less time than before.
3. What are positive's and negative's of
these trees?
Positive Negative
• Positives for both is that you
get around log(n) in both these
data structures theoretically.
• Splay trees have average log(n)
over a number of operations.
• Getting n times complexity for
an operation in a set, but it can
be compensated while
accessing frequent items.
• In binary search tree, you
need to be lucky to have log(n)
always.
• If the keys are not random,
then the tree will reduce to a
list like form with only one
side.
4. What are the performances of these
trees in terms of big O notation?
• AVL tree insertion, deletion, and lookups take
O(log n) time for each.
• Splay tree do take same time in amortized sense.
• Any long sequence of operations will take at
most O(n log n) time, but individual operations
might take as much as O(n) time in splay trees.
Splay tree
• Two varieties to approach splay trees are :-
▫ Bottom up : first search the tree and rotate at same
iteration.
▫ Top down: first search the tree and rotate in another
iteration
• There are three case :-
1) ZIG
2) ZIG-ZAG (Bottom-up approach)
3) ZIG-ZIG (Top-down approach)
Case 1: ZIG
• When p is the root.
• The tree is rotated on the edge between x and p.
• We rotate x over y, making x’s children be the node y and one
of x’s former children u, so as to maintain the relative inorder
relationships of the nodes in tree T.
Case 2: Zig-Zag
• Left-right or right-left
• The order of rotations: bottom-up approach, which
guarantees that the tree height is reduced by one at each zig-zag
rotation.
• The tree is rotated on the edge between p and x, and then rotated
on the resulting edge between x and g.
Case 3: Zig-Zig
• Left–left or right–right
• The order of rotations: top-down, which guarantees that the
distance to every node encountered (except the root) is reduced to
half.
• When p is not the root and x and p are either both right children or
are both left children.
• The tree is rotated on the edge joining p with its parent g, then
rotated on the edge joining x with p.
Insertion
• To insert a value x into a splay tree:
▫ Insert x as with a normal binary search tree.
▫ when an item is inserted, a splay is performed.
▫ As a result, the newly inserted node x becomes the root of the tree.
• Algorithm:
insert(node) {
1. Insert the new node.
2. Let n be the length of the path traversed for the insertion. Then,
a. If n = 2m (i.e., even number) then perform m double rotations.
b. If n = 2m+1 (i.e., odd number) then perform one single rotation followed
by m double rotations.
}
Let’s solve it!
• Insert
5, 10, 15, 6, 9,7,11,4,12
Deletion
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.
Delete(x, T):
∗ Splay(x, T) and remove root → tree falls into T1 and T2.
∗ Splay(x, T1)
∗ Make T2 right son of new root of T1 after splay
find(x)
L R
x
L R
> x< x
delete x
Deletion (cont.)
Join
Join(L, R): given two trees such that L < R, merge them
Splay on the maximum element in L, then attach R
L R R
splay
T
find(x)
L R
x
delete x
L R
> x< x
Join(L,R)
T - x
Deletion (cont.)
Delete Example
91
6
4 7
2
Delete(4)
find(4)
9
6
7
1
4
2
1
2 6
7
1
2
9
6
7
9
Split
tree
Join
Tree
Try your own!
• Delete object(11) from the tree
SEARCH
• The search operation in splay trees does the standard BINARY
SEARCH TREE SEARCH.
• In addition to that it SPLAYS.
• Splay trees are very effective search trees relatively simple:
1. No extra fields required
2. Excellent locality properties:
• frequently accessed keys are cheap to find (near top of tree)
• infrequently accessed keys stay out of the way (near
bottom of tree)
Rules of splaying: Search
• If the search is successful, then the node that is found is splayed and becomes
the new root.
• If the search is unsuccessful, the last node accessed prior to reaching the
NULL pointer is splayed and becomes the new root.
Search (i, t)
If item i is in tree t, return a pointer to the node containing i; otherwise return a
pointer to the null node.
• Search down the root of t, looking for i.
• If the search is successful and we reach a node x containing i, we complete
the search by splaying at x and returning a pointer to x.
• If the search is unsuccessful, i.e., we reach the null node, we splay at the last
non-null node reached during the search and return a pointer to null.
• If the tree is empty, we omit any splaying operation.
1
2
3
4
5
6
Q. Search(6)
Now parent and grand parent are
both on the same direction
GRAND PARENT
PARENT
ZIG-ZIG 1
2
3
4
5
6
1
2
3
4
5
6
For 6 parent and
grand-parent are in
same direction
ZIG-ZIG
1
2
3
4
5
6
STILL
SPLAYING…
.
1
2
3
4
5
6
6 SPLAYED
OUT….
6’s PARENT IS
THE ROOT
ZIG
1
2
3
4
5
6
1
2
3
4
5
6
Search(4)
Zig-Zag Zig-Zag
1
3
4
2
5
6
4
1
5
2
3
6
HANDLING AN UNSUCCESSFUL
SEARCH
50
30
30 40
60
20
15
90
70 100
Search(80)
Not Found
Last accessed node is 70
Splay 70
50
30
30 40
60
20
15
90
70 100
50
30
30 40
60
20
15
70
100
90
AMORTIZED ANALYSIS
• Amortized analysis is a technique for analyzing an algorithm's running time.
• It is often appropriate when one is interested in understanding asymptotic behavior
over sequences of operations.
• For example, one might be interested in reasoning about the running time for an ar
bitrary operation to insert an item into a binary search tree structure. In cases such
as this, it might be straightforward to come up with an upper bound by say,
finding the worst-possible time required for any operation, them multiplying this by
the number of operations in the sequence.
• However, many real data structures, such as splay trees, have the property that it is
impossible for every operation in a sequence to take the worst case time, so
this approach can result in a horribly pessimistic bound!
• Amortized analysis allows a tighter bound that better reflects performance.
• It does not say anything about the cost of a specific operation in that sequence.
• Amortized analysis is concerned with the overall cost of arbitrary sequences.
• An amortized bound will hold regardless of the specific sequence; for example, if th
e amortized cost of insertion is O(log n), it is so regardless of whether you insert
the sequence '10,' '160,' '2' or the sequence '2', '160', '10,' '399', etc.
• An amortized bound says nothing about the cost of individual operations, it may
be possible that one operation in the sequence requires a huge cost.
• Practical systems in which it is important that all operations have low and/or
comparable costs may require an algorithm with a worse amortized cost but a better
worst case per operation bound.
• Amortized analysis can be understood to take advantage of the fact that some
expensive operations may “pay” for future operations by somehow limiting the
number or cost of expensive operations that can happen in the near future.
• If good amortized cost is a goal, an algorithm may be designed to explicitly perform
this “clean up” during expensive operations!
• The key to performing amortized analysis is picking a good “credit” or “potential
function” that captures how operations with different actual costs affect a data
structure and allows the desired bound to be shown.
AMORTISED ANALYSIS OF
SPLAY TREES
• Let S(x) denote subtree of S
rooted at x.
• |S| = number of nodes in tree S.
• µ(S) = rank = floor(log |S|).
• P(i) = ∑tree node x rank(x).
• P(i) is potential after i’th
operation.
• rank(x) is computed after i’th
operation.
• P(0) = 0
• Potential=10
2
1 8
4
3 6
5 7
10
9
|S| = 10
µ(2) = 3
µ(8) = 3
µ(4) = 2
µ(6) = 1
µ(5) = 0
CASE 1: If q = null or q is the root
do nothing (splay is over).
∆P = 0
amortized cost = actual cost + ∆P = 0.
CASE 2: If q is at level 2
do a one-level move and terminate the splay
operation.
r(x) = rank of x before splay step.
r'(x) = rank of x after splay step.
∆P = r'(p) + r'(q) − r(p) − r(q) ≤ r'(q) − r(q)
amortized cost = actual cost + ∆P ≤ 1+ r'(q) − r(q)
p
q
ba
c
q
cb
a p
Potential
s
p
q
d
a b
c
q
a
s
p
c d
b
r'(q) = r(s)
r'(s) ≤ r’(q)
r’(p) ≤ r’(q)
r(q) ≤ r(p)
∆P = r’(s) +r’(p) +r’(q)−r(s)−r(p)−r(q) ≤ r'(q) + r'(q) − r(q) − r(q) =
2(r'(q)−r(q))
2(r'(q)−r(q)) ≤ 3(r'(q)−r(q))−1
amortized cost = actual cost + ∆P ≤ 1+3(r'(q)−r(q))−1 = 3(r'(q)−r(q))
CASE 3: If q is at level 3
Putting it together
 We repeat the steps until X replaces the root R
• zig only happens once, so the 1 is only added once/
• Each time, the last Rf(X) is cancelled by the next –Ri(X)
 The only terms left are: AT(total) <= 1 + 3*[Rroot(X) – Rinitial(X)]
 Rinitial(X) could be as low as 0, Rroot(X) as high as log N •
 Thus, total budget for whole sequence is O(log N)
OPERATION COST
AT(zig) < = 1+ ∆R(X) <= 1 + 3 [Rf(X) –
Ri(X)]
AT(zig-zag) <= 2 ∆R(X) <= 3 [Rf(X) –
Ri(X)]
AT(zig-zig) <= 3 ∆R(X) <= 3 [Rf(X) –
Ri(X)]
Splay tree

More Related Content

What's hot

Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Pankaj Thakur
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
Prof. Dr. K. Adisesha
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
Digvijay Singh Karakoti
 
Splay Tree Presentation Slides
Splay Tree Presentation SlidesSplay Tree Presentation Slides
Splay Tree Presentation Slides
Muhammad Shahbaz
 
Tower Of Hanoi
Tower Of HanoiTower Of Hanoi
Tower Of Hanoi
Vinit Dantkale
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
Ghaffar Khan
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
Core Condor
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Lecture 14 splay tree
Lecture 14 splay treeLecture 14 splay tree
Lecture 14 splay tree
Abirami A
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
State Space Search in ai
State Space Search in aiState Space Search in ai
State Space Search in ai
vikas dhakane
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 

What's hot (20)

Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Splay Tree Presentation Slides
Splay Tree Presentation SlidesSplay Tree Presentation Slides
Splay Tree Presentation Slides
 
Tower Of Hanoi
Tower Of HanoiTower Of Hanoi
Tower Of Hanoi
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Time complexity
Time complexityTime complexity
Time complexity
 
Lecture 14 splay tree
Lecture 14 splay treeLecture 14 splay tree
Lecture 14 splay tree
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
State Space Search in ai
State Space Search in aiState Space Search in ai
State Space Search in ai
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 

Similar to Splay tree

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
ashish bansal
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
Dr.Umadevi V
 
sorting
sortingsorting
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
Arumugam90
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
Tekle12
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
rupanaveen24
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
Gelo Maribbay
 
Splay tree
Splay treeSplay tree
Splay tree
Rajendran
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
Tree
TreeTree
Tree
sbkbca
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
(5) collections algorithms
(5) collections algorithms(5) collections algorithms
(5) collections algorithms
Nico Ludwig
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
Shahzad Ali
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
Dr.Umadevi V
 
Phases of distributed query processing
Phases of distributed query processingPhases of distributed query processing
Phases of distributed query processing
Nevil Dsouza
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Binary Search Tree
Binary Search TreeBinary Search Tree

Similar to Splay tree (20)

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
sorting
sortingsorting
sorting
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
 
Splay tree
Splay treeSplay tree
Splay tree
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Tree
TreeTree
Tree
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
(5) collections algorithms
(5) collections algorithms(5) collections algorithms
(5) collections algorithms
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Phases of distributed query processing
Phases of distributed query processingPhases of distributed query processing
Phases of distributed query processing
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Recently uploaded

Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 

Recently uploaded (20)

Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 

Splay tree

  • 1. SPLAY TREE By, Ambooj Yadav, Anam Iqbal & Hina Firdaus M.Tech CSE Ist year, Ist Semester Advance Datastructure Algorithm and Analysis Jamia Hamdard University, New Delhi-62
  • 2. Introduction Definition:- • Splay trees another variation of the binary search tree, invented by Robert Tarjan and Daniel Sleator. • Splay means “to rearrange, so that the desired element is placed at the root" • Splay trees support all the operations of binary trees. • A splay tree is a self-adjusting binary search tree Novel characteristics • It performs basic operations such as insertion, look-up and removal. • Does not require any accounting information (color, level, height, etc.) • Worst case for any single operation: O(N) • Average case for all operation: O(log N) • Frequently-accessed keys are moved up so that they are near the root. • Easier to perform comparison with 2-3-4 trees.
  • 3.
  • 4. General question 1. What is difference between AVL trees and splay trees? 2. On what basis do we select these tress? 3. What are positive's and negative's of these trees? 4. What are the performances of these trees in terms of big O notation?
  • 5. 1. What is difference between AVL trees and splay trees? Splay trees always try to be balanced after every operation. Because of this rest operation take less time to perform They are cool…
  • 6. 2. On what basis do we select these tress? • Splay trees are always better than binary search trees when, your application deals with a lot of data in the tree but, will need access to a subset of the data very frequently than others. • In this case the data you access frequently will come near the root as a result of the splay. • Because of this, any node can then be accessed with less time than before.
  • 7. 3. What are positive's and negative's of these trees? Positive Negative • Positives for both is that you get around log(n) in both these data structures theoretically. • Splay trees have average log(n) over a number of operations. • Getting n times complexity for an operation in a set, but it can be compensated while accessing frequent items. • In binary search tree, you need to be lucky to have log(n) always. • If the keys are not random, then the tree will reduce to a list like form with only one side.
  • 8. 4. What are the performances of these trees in terms of big O notation? • AVL tree insertion, deletion, and lookups take O(log n) time for each. • Splay tree do take same time in amortized sense. • Any long sequence of operations will take at most O(n log n) time, but individual operations might take as much as O(n) time in splay trees.
  • 9. Splay tree • Two varieties to approach splay trees are :- ▫ Bottom up : first search the tree and rotate at same iteration. ▫ Top down: first search the tree and rotate in another iteration • There are three case :- 1) ZIG 2) ZIG-ZAG (Bottom-up approach) 3) ZIG-ZIG (Top-down approach)
  • 10. Case 1: ZIG • When p is the root. • The tree is rotated on the edge between x and p. • We rotate x over y, making x’s children be the node y and one of x’s former children u, so as to maintain the relative inorder relationships of the nodes in tree T.
  • 11. Case 2: Zig-Zag • Left-right or right-left • The order of rotations: bottom-up approach, which guarantees that the tree height is reduced by one at each zig-zag rotation. • The tree is rotated on the edge between p and x, and then rotated on the resulting edge between x and g.
  • 12. Case 3: Zig-Zig • Left–left or right–right • The order of rotations: top-down, which guarantees that the distance to every node encountered (except the root) is reduced to half. • When p is not the root and x and p are either both right children or are both left children. • The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p.
  • 13. Insertion • To insert a value x into a splay tree: ▫ Insert x as with a normal binary search tree. ▫ when an item is inserted, a splay is performed. ▫ As a result, the newly inserted node x becomes the root of the tree. • Algorithm: insert(node) { 1. Insert the new node. 2. Let n be the length of the path traversed for the insertion. Then, a. If n = 2m (i.e., even number) then perform m double rotations. b. If n = 2m+1 (i.e., odd number) then perform one single rotation followed by m double rotations. }
  • 14. Let’s solve it! • Insert 5, 10, 15, 6, 9,7,11,4,12
  • 15. Deletion 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. Delete(x, T): ∗ Splay(x, T) and remove root → tree falls into T1 and T2. ∗ Splay(x, T1) ∗ Make T2 right son of new root of T1 after splay
  • 16. find(x) L R x L R > x< x delete x Deletion (cont.)
  • 17. Join Join(L, R): given two trees such that L < R, merge them Splay on the maximum element in L, then attach R L R R splay
  • 18. T find(x) L R x delete x L R > x< x Join(L,R) T - x Deletion (cont.)
  • 19. Delete Example 91 6 4 7 2 Delete(4) find(4) 9 6 7 1 4 2 1 2 6 7 1 2 9 6 7 9 Split tree Join Tree
  • 20. Try your own! • Delete object(11) from the tree
  • 21. SEARCH • The search operation in splay trees does the standard BINARY SEARCH TREE SEARCH. • In addition to that it SPLAYS. • Splay trees are very effective search trees relatively simple: 1. No extra fields required 2. Excellent locality properties: • frequently accessed keys are cheap to find (near top of tree) • infrequently accessed keys stay out of the way (near bottom of tree)
  • 22. Rules of splaying: Search • If the search is successful, then the node that is found is splayed and becomes the new root. • If the search is unsuccessful, the last node accessed prior to reaching the NULL pointer is splayed and becomes the new root. Search (i, t) If item i is in tree t, return a pointer to the node containing i; otherwise return a pointer to the null node. • Search down the root of t, looking for i. • If the search is successful and we reach a node x containing i, we complete the search by splaying at x and returning a pointer to x. • If the search is unsuccessful, i.e., we reach the null node, we splay at the last non-null node reached during the search and return a pointer to null. • If the tree is empty, we omit any splaying operation.
  • 23. 1 2 3 4 5 6 Q. Search(6) Now parent and grand parent are both on the same direction GRAND PARENT PARENT ZIG-ZIG 1 2 3 4 5 6
  • 24. 1 2 3 4 5 6 For 6 parent and grand-parent are in same direction ZIG-ZIG 1 2 3 4 5 6 STILL SPLAYING… .
  • 25. 1 2 3 4 5 6 6 SPLAYED OUT…. 6’s PARENT IS THE ROOT ZIG 1 2 3 4 5 6
  • 27. HANDLING AN UNSUCCESSFUL SEARCH 50 30 30 40 60 20 15 90 70 100 Search(80) Not Found Last accessed node is 70 Splay 70
  • 30. AMORTIZED ANALYSIS • Amortized analysis is a technique for analyzing an algorithm's running time. • It is often appropriate when one is interested in understanding asymptotic behavior over sequences of operations. • For example, one might be interested in reasoning about the running time for an ar bitrary operation to insert an item into a binary search tree structure. In cases such as this, it might be straightforward to come up with an upper bound by say, finding the worst-possible time required for any operation, them multiplying this by the number of operations in the sequence. • However, many real data structures, such as splay trees, have the property that it is impossible for every operation in a sequence to take the worst case time, so this approach can result in a horribly pessimistic bound!
  • 31. • Amortized analysis allows a tighter bound that better reflects performance. • It does not say anything about the cost of a specific operation in that sequence. • Amortized analysis is concerned with the overall cost of arbitrary sequences. • An amortized bound will hold regardless of the specific sequence; for example, if th e amortized cost of insertion is O(log n), it is so regardless of whether you insert the sequence '10,' '160,' '2' or the sequence '2', '160', '10,' '399', etc. • An amortized bound says nothing about the cost of individual operations, it may be possible that one operation in the sequence requires a huge cost. • Practical systems in which it is important that all operations have low and/or comparable costs may require an algorithm with a worse amortized cost but a better worst case per operation bound.
  • 32. • Amortized analysis can be understood to take advantage of the fact that some expensive operations may “pay” for future operations by somehow limiting the number or cost of expensive operations that can happen in the near future. • If good amortized cost is a goal, an algorithm may be designed to explicitly perform this “clean up” during expensive operations! • The key to performing amortized analysis is picking a good “credit” or “potential function” that captures how operations with different actual costs affect a data structure and allows the desired bound to be shown.
  • 33. AMORTISED ANALYSIS OF SPLAY TREES • Let S(x) denote subtree of S rooted at x. • |S| = number of nodes in tree S. • µ(S) = rank = floor(log |S|). • P(i) = ∑tree node x rank(x). • P(i) is potential after i’th operation. • rank(x) is computed after i’th operation. • P(0) = 0 • Potential=10 2 1 8 4 3 6 5 7 10 9 |S| = 10 µ(2) = 3 µ(8) = 3 µ(4) = 2 µ(6) = 1 µ(5) = 0
  • 34. CASE 1: If q = null or q is the root do nothing (splay is over). ∆P = 0 amortized cost = actual cost + ∆P = 0. CASE 2: If q is at level 2 do a one-level move and terminate the splay operation. r(x) = rank of x before splay step. r'(x) = rank of x after splay step.
  • 35. ∆P = r'(p) + r'(q) − r(p) − r(q) ≤ r'(q) − r(q) amortized cost = actual cost + ∆P ≤ 1+ r'(q) − r(q) p q ba c q cb a p Potential
  • 36. s p q d a b c q a s p c d b r'(q) = r(s) r'(s) ≤ r’(q) r’(p) ≤ r’(q) r(q) ≤ r(p) ∆P = r’(s) +r’(p) +r’(q)−r(s)−r(p)−r(q) ≤ r'(q) + r'(q) − r(q) − r(q) = 2(r'(q)−r(q)) 2(r'(q)−r(q)) ≤ 3(r'(q)−r(q))−1 amortized cost = actual cost + ∆P ≤ 1+3(r'(q)−r(q))−1 = 3(r'(q)−r(q)) CASE 3: If q is at level 3
  • 37. Putting it together  We repeat the steps until X replaces the root R • zig only happens once, so the 1 is only added once/ • Each time, the last Rf(X) is cancelled by the next –Ri(X)  The only terms left are: AT(total) <= 1 + 3*[Rroot(X) – Rinitial(X)]  Rinitial(X) could be as low as 0, Rroot(X) as high as log N •  Thus, total budget for whole sequence is O(log N) OPERATION COST AT(zig) < = 1+ ∆R(X) <= 1 + 3 [Rf(X) – Ri(X)] AT(zig-zag) <= 2 ∆R(X) <= 3 [Rf(X) – Ri(X)] AT(zig-zig) <= 3 ∆R(X) <= 3 [Rf(X) – Ri(X)]