SlideShare a Scribd company logo
1 of 27
Splay Trees and B-Trees
CSE 373
Data Structures
Lecture 9
1/31/03 Splay Trees and B-Trees - Lecture
9
2
Readings
• Reading
› Sections 4.5-4.7
1/31/03 Splay Trees and B-Trees - Lecture
9
3
Self adjusting Trees
• Ordinary binary search trees have no balance
conditions
› what you get from insertion order is it
• Balanced trees like AVL trees enforce a
balance condition when nodes change
› tree is always balanced after an insert or delete
• Self-adjusting trees get reorganized over time
as nodes are accessed
› Tree adjusts after insert, delete, or find
1/31/03 Splay Trees and B-Trees - Lecture
9
4
Splay Trees
• Splay trees are tree structures that:
› Are not perfectly balanced all the time
› Data most recently accessed is near the root.
(principle of locality; 80-20 “rule”)
• The procedure:
› After node X is accessed, perform “splaying”
operations to bring X to the root of the tree.
› Do this in a way that leaves the tree more
balanced as a whole
1/31/03 Splay Trees and B-Trees - Lecture
9
5
• 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
1/31/03 Splay Trees and B-Trees - Lecture
9
6
Zig-Zig and Zig-Zag
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.
1/31/03 Splay Trees and B-Trees - Lecture
9
7
1. Helpful if nodes contain a parent pointer.
2. When X is accessed, apply one of six rotation routines.
• Single Rotations (X has a P (the root) but no G)
ZigFromLeft, ZigFromRight
• Double Rotations (X has both a P and a G)
ZigZigFromLeft, ZigZigFromRight
ZigZagFromLeft, ZigZagFromRight
Splay Tree Operations
parent
right
left
element
1/31/03 Splay Trees and B-Trees - Lecture
9
8
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
ZigFromLeft
root
1/31/03 Splay Trees and B-Trees - Lecture
9
9
Zig at depth 1
• Suppose Q is now accessed using Find
• ZigFromRight moves Q back to the top
ZigFromRight
root
1/31/03 Splay Trees and B-Trees - Lecture
9
10
Zig-Zag operation
• “Zig-Zag” consists of two rotations of the
opposite direction (assume R is the node that
was accessed)
(ZigFromRight) (ZigFromLeft)
ZigZagFromLeft
1/31/03 Splay Trees and B-Trees - Lecture
9
11
Zig-Zig operation
• “Zig-Zig” consists of two single rotations
of the same direction (R is the node that
was accessed)
(ZigFromLeft) (ZigFromLeft)
ZigZigFromLeft
1/31/03 Splay Trees and B-Trees - Lecture
9
12
Decreasing depth -
"autobalance"
Find(T) Find(R)
1/31/03 Splay Trees and B-Trees - Lecture
9
13
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.
1/31/03 Splay Trees and B-Trees - Lecture
9
14
Example Insert
• Inserting in order 1,2,3,…,8
• Without self-adjustment
1
2
3
4
5
6
7
8
O(n2) time for n Insert
1/31/03 Splay Trees and B-Trees - Lecture
9
15
With Self-Adjustment
1
2
1 2
1
ZigFromRight
2
1 3
ZigFromRight
2
1
3
1
2
3
1/31/03 Splay Trees and B-Trees - Lecture
9
16
With Self-Adjustment
ZigFromRight
2
1
3
4
4
2
1
3
4
Each Insert takes O(1) time therefore O(n) time for n Insert!!
1/31/03 Splay Trees and B-Trees - Lecture
9
17
Example Deletion
10
15
5
20
13
8
2
9
6
10
15
5
20
13
8
2 9
6
splay
10
15
5
20
13
2 9
6
remove
10
15
5
20
13
2 9
6
Splay (zig)
attach
(Zig-Zag)
1/31/03 Splay Trees and B-Trees - Lecture
9
18
Analysis of Splay Trees
• Splay trees tend to be balanced
› M operations takes time O(M log N) for M > N
operations on N items. (proof is difficult)
› Amortized O(log n) time.
• Splay trees have good “locality” properties
› Recently accessed items are near the root of the
tree.
› Items near an accessed one are pulled toward the
root.
1/31/03 Splay Trees and B-Trees - Lecture
9
19
• Example: B-tree of order 3 has 2 or 3
children per node
• Search for 8
Beyond Binary Search Trees:
Multi-Way Trees
13:-
6:11
3 4 6 7 8 11 12 13 14 17 18
17:-
1/31/03 Splay Trees and B-Trees - Lecture
9
20
B-Trees are multi-way search trees commonly used in database
systems or other applications where data is stored externally on
disks and keeping the tree shallow is important.
A B-Tree of order M has the following properties:
1. The root is either a leaf or has between 2 and M children.
2. All nonleaf nodes (except the root) have between M/2
and M children.
3. All leaves are at the same depth.
All data records are stored at the leaves.
Internal nodes have “keys” guiding to the leaves.
Leaves store between M/2 and M data records.
B-Trees
1/31/03 Splay Trees and B-Trees - Lecture
9
21
B-Tree Details
Each (non-leaf) internal node of a B-tree has:
› Between M/2 and M children.
› up to M-1 keys k1 < k2 < ... < kM-1
Keys are ordered so that:
k1 < k2 < ... < kM-1
kM-1
. . . . . .
ki-1 ki
k1
1/31/03 Splay Trees and B-Trees - Lecture
9
22
Properties of B-Trees
Children of each internal node are "between" the items in that node.
Suppose subtree Ti is the ith child of the node:
all keys in Ti must be between keys ki-1 and ki
i.e. ki-1  Ti < ki
ki-1 is the smallest key in Ti
All keys in first subtree T1 < k1
All keys in last subtree TM  kM-1
k1
T
Ti
i
. . . . . .
k
ki-1 k
ki
i
T
TM
T
T1
1
k
kM-1
. . .
. . .
1/31/03 Splay Trees and B-Trees - Lecture
9
23
Example: Searching in B-trees
13:-
6:11
3 4 6 7 8 11 12 13 14 17 18
17:-
• B-tree of order 3: also known as 2-3 tree (2 to 3
children)
• Examples: Search for 9, 14, 12
• Note: If leaf nodes are connected as a Linked List, B-
tree is called a B+ tree – Allows sorted list to be
accessed easily
- means empty slot
1/31/03 Splay Trees and B-Trees - Lecture
9
24
Inserting into B-Trees
• Insert X: Do a Find on X and find appropriate leaf node
› If leaf node is not full, fill in empty slot with X
• E.g. Insert 5
› If leaf node is full, split leaf node and adjust parents up to root
node
• E.g. Insert 9
13:-
6:11
3 4 6 7 8 11 12 13 14 17 18
17:-
1/31/03 Splay Trees and B-Trees - Lecture
9
25
Deleting From B-Trees
• Delete X : Do a find and remove from leaf
› Leaf underflows – borrow from a neighbor
• E.g. 11
› Leaf underflows and can’t borrow – merge nodes, delete
parent
• E.g. 17 13:-
6:11
3 4 6 7 8 11 12 13 14 17 18
17:-
1/31/03 Splay Trees and B-Trees - Lecture
9
26
Run Time Analysis of B-Tree
Operations
• For a B-Tree of order M
› Each internal node has up to M-1 keys to search
› Each internal node has between M/2 and M children
› Depth of B-Tree storing N items is O(log M/2 N)
• Find: Run time is:
› O(log M) to binary search which branch to take at each
node. But M is small compared to N.
› Total time to find an item is O(depth*log M) = O(log N)
1/31/03 Splay Trees and B-Trees - Lecture
9
27
Summary of Search Trees
• Problem with Binary Search Trees: Must keep tree
balanced to allow fast access to stored items
• AVL trees: Insert/Delete operations keep tree balanced
• Splay trees: Repeated Find operations produce
balanced trees
• Multi-way search trees (e.g. B-Trees): More than two
children
› per node allows shallow trees; all leaves are at the
same depth
› keeping tree balanced at all times

More Related Content

Similar to lecture09.ppt zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Similar to lecture09.ppt zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz (17)

Splay tree
Splay treeSplay tree
Splay tree
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
 
Tutorial 3 (b tree min heap)
Tutorial 3 (b tree min heap)Tutorial 3 (b tree min heap)
Tutorial 3 (b tree min heap)
 
B+ tree.pptx
B+ tree.pptxB+ tree.pptx
B+ tree.pptx
 
B trees
B treesB trees
B trees
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
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 in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
4-b-tree.ppt
4-b-tree.ppt4-b-tree.ppt
4-b-tree.ppt
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Trees
TreesTrees
Trees
 
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
 
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
 

More from RAtna29

PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnPatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnRAtna29
 
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnNLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnRAtna29
 
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbb
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbbLexical-Semantics-NLP.pptx bbbbbbbbbbbbbbb
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbbRAtna29
 
btrees.ppt ttttttttttttttttttttttttttttt
btrees.ppt  tttttttttttttttttttttttttttttbtrees.ppt  ttttttttttttttttttttttttttttt
btrees.ppt tttttttttttttttttttttttttttttRAtna29
 
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmIndexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmRAtna29
 
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNN
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNNModel-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNN
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNNRAtna29
 
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyTF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyRAtna29
 
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhexing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhRAtna29
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxRAtna29
 
btree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggbtree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggRAtna29
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
avltrees.ppt unit 4 ppts for the university exam
avltrees.ppt unit 4 ppts for the university examavltrees.ppt unit 4 ppts for the university exam
avltrees.ppt unit 4 ppts for the university examRAtna29
 
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuu
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuuavltreesUNIT4.ppt ggggggggggggggguuuuuuuuu
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuuRAtna29
 
nlp semantic for parsing ... mmmmmmmmmmmm
nlp semantic for parsing ... mmmmmmmmmmmmnlp semantic for parsing ... mmmmmmmmmmmm
nlp semantic for parsing ... mmmmmmmmmmmmRAtna29
 

More from RAtna29 (15)

PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnPatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnNLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
NLP-ppt.pptx nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbb
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbbLexical-Semantics-NLP.pptx bbbbbbbbbbbbbbb
Lexical-Semantics-NLP.pptx bbbbbbbbbbbbbbb
 
btrees.ppt ttttttttttttttttttttttttttttt
btrees.ppt  tttttttttttttttttttttttttttttbtrees.ppt  ttttttttttttttttttttttttttttt
btrees.ppt ttttttttttttttttttttttttttttt
 
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmIndexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
 
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNN
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNNModel-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNN
Model-Theoretic Semantics.pdf NNNNNNNNNNNNNNNNNNNNN
 
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyTF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
TF-IDF.pdf yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
 
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhexing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
exing.ppt hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
btree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggbtree.ppt ggggggggggggggggggggggggggggggg
btree.ppt ggggggggggggggggggggggggggggggg
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
avltrees.ppt unit 4 ppts for the university exam
avltrees.ppt unit 4 ppts for the university examavltrees.ppt unit 4 ppts for the university exam
avltrees.ppt unit 4 ppts for the university exam
 
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuu
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuuavltreesUNIT4.ppt ggggggggggggggguuuuuuuuu
avltreesUNIT4.ppt ggggggggggggggguuuuuuuuu
 
nlp semantic for parsing ... mmmmmmmmmmmm
nlp semantic for parsing ... mmmmmmmmmmmmnlp semantic for parsing ... mmmmmmmmmmmm
nlp semantic for parsing ... mmmmmmmmmmmm
 

Recently uploaded

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf203318pmpc
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 

Recently uploaded (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

lecture09.ppt zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

  • 1. Splay Trees and B-Trees CSE 373 Data Structures Lecture 9
  • 2. 1/31/03 Splay Trees and B-Trees - Lecture 9 2 Readings • Reading › Sections 4.5-4.7
  • 3. 1/31/03 Splay Trees and B-Trees - Lecture 9 3 Self adjusting Trees • Ordinary binary search trees have no balance conditions › what you get from insertion order is it • Balanced trees like AVL trees enforce a balance condition when nodes change › tree is always balanced after an insert or delete • Self-adjusting trees get reorganized over time as nodes are accessed › Tree adjusts after insert, delete, or find
  • 4. 1/31/03 Splay Trees and B-Trees - Lecture 9 4 Splay Trees • Splay trees are tree structures that: › Are not perfectly balanced all the time › Data most recently accessed is near the root. (principle of locality; 80-20 “rule”) • The procedure: › After node X is accessed, perform “splaying” operations to bring X to the root of the tree. › Do this in a way that leaves the tree more balanced as a whole
  • 5. 1/31/03 Splay Trees and B-Trees - Lecture 9 5 • 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
  • 6. 1/31/03 Splay Trees and B-Trees - Lecture 9 6 Zig-Zig and Zig-Zag 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.
  • 7. 1/31/03 Splay Trees and B-Trees - Lecture 9 7 1. Helpful if nodes contain a parent pointer. 2. When X is accessed, apply one of six rotation routines. • Single Rotations (X has a P (the root) but no G) ZigFromLeft, ZigFromRight • Double Rotations (X has both a P and a G) ZigZigFromLeft, ZigZigFromRight ZigZagFromLeft, ZigZagFromRight Splay Tree Operations parent right left element
  • 8. 1/31/03 Splay Trees and B-Trees - Lecture 9 8 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 ZigFromLeft root
  • 9. 1/31/03 Splay Trees and B-Trees - Lecture 9 9 Zig at depth 1 • Suppose Q is now accessed using Find • ZigFromRight moves Q back to the top ZigFromRight root
  • 10. 1/31/03 Splay Trees and B-Trees - Lecture 9 10 Zig-Zag operation • “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed) (ZigFromRight) (ZigFromLeft) ZigZagFromLeft
  • 11. 1/31/03 Splay Trees and B-Trees - Lecture 9 11 Zig-Zig operation • “Zig-Zig” consists of two single rotations of the same direction (R is the node that was accessed) (ZigFromLeft) (ZigFromLeft) ZigZigFromLeft
  • 12. 1/31/03 Splay Trees and B-Trees - Lecture 9 12 Decreasing depth - "autobalance" Find(T) Find(R)
  • 13. 1/31/03 Splay Trees and B-Trees - Lecture 9 13 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.
  • 14. 1/31/03 Splay Trees and B-Trees - Lecture 9 14 Example Insert • Inserting in order 1,2,3,…,8 • Without self-adjustment 1 2 3 4 5 6 7 8 O(n2) time for n Insert
  • 15. 1/31/03 Splay Trees and B-Trees - Lecture 9 15 With Self-Adjustment 1 2 1 2 1 ZigFromRight 2 1 3 ZigFromRight 2 1 3 1 2 3
  • 16. 1/31/03 Splay Trees and B-Trees - Lecture 9 16 With Self-Adjustment ZigFromRight 2 1 3 4 4 2 1 3 4 Each Insert takes O(1) time therefore O(n) time for n Insert!!
  • 17. 1/31/03 Splay Trees and B-Trees - Lecture 9 17 Example Deletion 10 15 5 20 13 8 2 9 6 10 15 5 20 13 8 2 9 6 splay 10 15 5 20 13 2 9 6 remove 10 15 5 20 13 2 9 6 Splay (zig) attach (Zig-Zag)
  • 18. 1/31/03 Splay Trees and B-Trees - Lecture 9 18 Analysis of Splay Trees • Splay trees tend to be balanced › M operations takes time O(M log N) for M > N operations on N items. (proof is difficult) › Amortized O(log n) time. • Splay trees have good “locality” properties › Recently accessed items are near the root of the tree. › Items near an accessed one are pulled toward the root.
  • 19. 1/31/03 Splay Trees and B-Trees - Lecture 9 19 • Example: B-tree of order 3 has 2 or 3 children per node • Search for 8 Beyond Binary Search Trees: Multi-Way Trees 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:-
  • 20. 1/31/03 Splay Trees and B-Trees - Lecture 9 20 B-Trees are multi-way search trees commonly used in database systems or other applications where data is stored externally on disks and keeping the tree shallow is important. A B-Tree of order M has the following properties: 1. The root is either a leaf or has between 2 and M children. 2. All nonleaf nodes (except the root) have between M/2 and M children. 3. All leaves are at the same depth. All data records are stored at the leaves. Internal nodes have “keys” guiding to the leaves. Leaves store between M/2 and M data records. B-Trees
  • 21. 1/31/03 Splay Trees and B-Trees - Lecture 9 21 B-Tree Details Each (non-leaf) internal node of a B-tree has: › Between M/2 and M children. › up to M-1 keys k1 < k2 < ... < kM-1 Keys are ordered so that: k1 < k2 < ... < kM-1 kM-1 . . . . . . ki-1 ki k1
  • 22. 1/31/03 Splay Trees and B-Trees - Lecture 9 22 Properties of B-Trees Children of each internal node are "between" the items in that node. Suppose subtree Ti is the ith child of the node: all keys in Ti must be between keys ki-1 and ki i.e. ki-1  Ti < ki ki-1 is the smallest key in Ti All keys in first subtree T1 < k1 All keys in last subtree TM  kM-1 k1 T Ti i . . . . . . k ki-1 k ki i T TM T T1 1 k kM-1 . . . . . .
  • 23. 1/31/03 Splay Trees and B-Trees - Lecture 9 23 Example: Searching in B-trees 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:- • B-tree of order 3: also known as 2-3 tree (2 to 3 children) • Examples: Search for 9, 14, 12 • Note: If leaf nodes are connected as a Linked List, B- tree is called a B+ tree – Allows sorted list to be accessed easily - means empty slot
  • 24. 1/31/03 Splay Trees and B-Trees - Lecture 9 24 Inserting into B-Trees • Insert X: Do a Find on X and find appropriate leaf node › If leaf node is not full, fill in empty slot with X • E.g. Insert 5 › If leaf node is full, split leaf node and adjust parents up to root node • E.g. Insert 9 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:-
  • 25. 1/31/03 Splay Trees and B-Trees - Lecture 9 25 Deleting From B-Trees • Delete X : Do a find and remove from leaf › Leaf underflows – borrow from a neighbor • E.g. 11 › Leaf underflows and can’t borrow – merge nodes, delete parent • E.g. 17 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:-
  • 26. 1/31/03 Splay Trees and B-Trees - Lecture 9 26 Run Time Analysis of B-Tree Operations • For a B-Tree of order M › Each internal node has up to M-1 keys to search › Each internal node has between M/2 and M children › Depth of B-Tree storing N items is O(log M/2 N) • Find: Run time is: › O(log M) to binary search which branch to take at each node. But M is small compared to N. › Total time to find an item is O(depth*log M) = O(log N)
  • 27. 1/31/03 Splay Trees and B-Trees - Lecture 9 27 Summary of Search Trees • Problem with Binary Search Trees: Must keep tree balanced to allow fast access to stored items • AVL trees: Insert/Delete operations keep tree balanced • Splay trees: Repeated Find operations produce balanced trees • Multi-way search trees (e.g. B-Trees): More than two children › per node allows shallow trees; all leaves are at the same depth › keeping tree balanced at all times