SlideShare a Scribd company logo
1 of 38
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
9
1
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
b
a
c
q
c
b
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)]
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN

More Related Content

Similar to splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN

Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge SortGelo Maribbay
 
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-11sumitbardhan
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
(5) collections algorithms
(5) collections algorithms(5) collections algorithms
(5) collections algorithmsNico Ludwig
 
4a searching-more
4a searching-more4a searching-more
4a searching-moreShahzad Ali
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Phases of distributed query processing
Phases of distributed query processingPhases of distributed query processing
Phases of distributed query processingNevil Dsouza
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphRai University
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080Aravind NC
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksHemanth Kumar
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graphRai University
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 

Similar to splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN (20)

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
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
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
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_Stacks
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN

  • 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 9 1 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 b a c q c b 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)]