SlideShare une entreprise Scribd logo
1  sur  35
Data structure presentation
Leftist heap(min)
CSE 225
Leftist Heap
By
Shuvro Roy
EECS DEPARTMENT
NORTH SOUTH UNIVERSITY
A leftist tree or leftist heap is a priority queue implemented with a
variant of a binary heap.
The parent node is always less than or equals to the child node.
Hence , the minimum element is at the top.
 Every node in a Leftist Heap has a rank which is the
distance to the nearest leaf.
The Rank
The Leftist Heap Property: If the rank, (i.e.- the
distance between the nearest null leaf node and the
node) of the right sub tree is greater than the
rank of the left sub tree, the right sub tree is made
the left sub tree i.e.-they are swapped.
In contrast to a binary heap, a leftist tree attempts to be very
unbalanced.
Due to the Leftist Heap Property the Leftist heaps are
maintained so the right descendant of each node has lower
rank. Hence, the left sub tree is heavier than the right sub
tree.
Insertion
The rank of the right child <= the rank of the left child.
So we always go right.
If the element to be inserted is greater than the existing element in a
node, then we insert that element at the right node of the existing
node.
If the element to be inserted is less than the existing element in a
node, then we make the existing node the right node of that
element.
Insertion
After each insertion the ranks of all the parent nodes are updated
back to the root.
While updating the rank of a parent, we first compare the rank of the
left child and the rank of right child.
If the rank of the left child is less than that of the right, then we swap
the left child and the right child and update the rank of the parent.
O(log n)
Merge
While merging two trees, we first have to compare the two roots of
the two trees.
The node with the smaller element becomes the new root.
The root of the other tree replaces the right node of the root.
Then the replaced node is compared with the sub nodes for further
placement.
The ranks for each of the nodes are updated and the sub trees are
swapped if needed.
4
25
12
15
819
2027
43
6
78
14
Consider two leftist heaps …Consider two leftist heaps …
Task: merge them into a single leftist heapTask: merge them into a single leftist heap
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
First, instantiate a StackFirst, instantiate a Stack
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
First, instantiate a StackFirst, instantiate a Stack
44
Merging leftist heaps
x yy
4
25
12
15
819
2027
43
6
78
14
Remember smaller valueRemember smaller value
66
44
xx
yy
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
Repeat the process with the right child ofRepeat the process with the right child of
the smaller valuethe smaller value
66
44
x yy
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
Remember smaller valueRemember smaller value
77
66
44
xx yy
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
Repeat the process with the right child ofRepeat the process with the right child of
the smaller valuethe smaller value
77
66
44
xx
yy
nullnull
Merging leftist heaps
4
25
12
15
819
2027
43
6
78
14
Because one of the arguments is null,Because one of the arguments is null,
return the other argumentreturn the other argument
77
66
44
xx
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 8 the right child of 7Make 8 the right child of 7
77
66
44
x
88
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 7 leftist (by swapping children)Make 7 leftist (by swapping children)
77
66
44
88
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Return node 7Return node 7
66
44
77
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 7 the right child of 6 (whichMake 7 the right child of 6 (which
it already is)it already is)
66
44
77
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 6 leftist (it already is)Make 6 leftist (it already is)
66
44
77
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Return node 6Return node 6
44
6
8
7Refers to nodeRefers to node
88
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 6 the right child of 4Make 6 the right child of 4
44
66
8
7
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Make 4 leftist (it already is)Make 4 leftist (it already is)
44
66
8
7
Merging leftist heaps
4
25
12
15
19
2027
43
6
8
14
Return node 4Return node 4
44
8
7
O(log n)O(log n)
Final leftist heap
Deletion
To delete the minimum item we disconnect the root form its left and
right sub tree.
The left and right sub trees now has its own roots.
The roots of the two trees are compared and merged into one tree.
Deletion
Deletion
Right sub treeLeft sub tree
Deletion
Deletion
O(log n)
Time Complexity
Get min O(1)
Insert O(log n)
Delete O(log n)
Merge O(log n)
THANK YOU 

Contenu connexe

Tendances

B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 

Tendances (20)

Tree and graph
Tree and graphTree and graph
Tree and graph
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
File organization and indexing
File organization and indexingFile organization and indexing
File organization and indexing
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 
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)
 
Array data structure
Array data structureArray data structure
Array data structure
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Digital Search Tree
Digital Search TreeDigital Search Tree
Digital Search Tree
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 

En vedette (7)

Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Heap sort
Heap sortHeap sort
Heap sort
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Binomial Heap
Binomial HeapBinomial Heap
Binomial Heap
 

Similaire à Leftist heap

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
Getachew Ganfur
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Sheba41
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
leminhvuong
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
fredharris32
 

Similaire à Leftist heap (20)

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
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)
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
 
Lec24
Lec24Lec24
Lec24
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search 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
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Binary tree
Binary tree Binary tree
Binary tree
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Short dec
Short decShort dec
Short dec
 
Heapsort
HeapsortHeapsort
Heapsort
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 

Dernier

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
Epec Engineered Technologies
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Dernier (20)

Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
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...
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
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
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
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
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
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
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
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
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 

Leftist heap