SlideShare une entreprise Scribd logo
1  sur  10
insertion,
deletion,
sorting
2
Heap
 A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
 A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
 Operations on heaps
 creation of an empty heap
 insertion of a new element into the heap;
 deletion of the largest element from the heap
Figure : Sample max heaps
[4]
14
12 7
810 6
9
6 3
5
30
25
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
Property:
The root of max heap (min heap) contains
the largest (smallest).
2
7 4
810 6
10
20 83
50
11
21
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
[4]
Figure : Sample min heaps
5
Example of Insertion to Max Heap
20
15 2
14 10
initial location of new node
21
15 20
14 10 2
insert 21 into heap
20
15 5
14 10 2
insert 5 into heap
Insertion into a Max Heap
INSHEAP(TREE, N, ITEM)
1. Set N=N+1 and PTR=N.
2. Repeat Steps 3 to 6 while PTR>1.
3. Set PAR= └PTR/2┘
4. If ITEM<= TREE[PAR], then:
Set TREE[PTR]=ITEM, and Return.
[End of If structure]
5. Set TREE[PTR]=TREE[PAR]
6. Set PTR=PAR.
[End of step 2]
7. Set TREE[1]=ITEM
8. Return.
7
Example of Deletion from Max Heap
20
remove
15 2
14 10
10
15 2
14
15
14 2
10
Deletion from a Max Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM= TREE[1].
2. Set LAST= TREE[N] and N= N-1.
3. Set PTR=1, LEFT=2 and RIGHT=3.
4. Repeat Steps 5 to 7 while RIGHT<=N:
5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then:
Set TREE[PTR]=LAST and Return.
[End of If structure.]
1. If TREE[RIGHT]<= TREE[LEFT], then:
Set TREE[PTR]= TREE[LEFT] and PTR=LEFT.
Else:
Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT.
[End of If structure.]
1. Set LEFT=2*PTR and RIGHT=LEFT+1.
[End of Step 4 loop.]
8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT]
then: Set PTR=LEFT.
9. Set TREE[PTR]= LAST.
10. Return.
HeapSort
HEAPSORT(A, N)
1. Repeat for J=1 to N-1:
Call INSHEAP(A, J, A[J+1]).
[End of loop.]
1. Repeat while N>1:
a) Call DELHEAP(A, N, ITEM).
b) A[N+1]=ITEM.
[End of loop.]
1. Exit.
Data Structure and Algorithms Heaps and Trees

Contenu connexe

Tendances

Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
Sajid Marwat
 

Tendances (20)

Heap sort
Heap sortHeap sort
Heap sort
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
Shell sort
Shell sortShell sort
Shell sort
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Queues
QueuesQueues
Queues
 
Data structures
Data structuresData structures
Data structures
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 

Similaire à Data Structure and Algorithms Heaps and Trees

Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 

Similaire à Data Structure and Algorithms Heaps and Trees (20)

heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
Sorting
SortingSorting
Sorting
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 

Plus de ManishPrajapati78

Plus de ManishPrajapati78 (15)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 

Dernier

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Dernier (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

Data Structure and Algorithms Heaps and Trees

  • 2. 2 Heap  A max tree is a tree in which the key value in each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree.  A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree.  Operations on heaps  creation of an empty heap  insertion of a new element into the heap;  deletion of the largest element from the heap
  • 3. Figure : Sample max heaps [4] 14 12 7 810 6 9 6 3 5 30 25 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] Property: The root of max heap (min heap) contains the largest (smallest).
  • 4. 2 7 4 810 6 10 20 83 50 11 21 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] [4] Figure : Sample min heaps
  • 5. 5 Example of Insertion to Max Heap 20 15 2 14 10 initial location of new node 21 15 20 14 10 2 insert 21 into heap 20 15 5 14 10 2 insert 5 into heap
  • 6. Insertion into a Max Heap INSHEAP(TREE, N, ITEM) 1. Set N=N+1 and PTR=N. 2. Repeat Steps 3 to 6 while PTR>1. 3. Set PAR= └PTR/2┘ 4. If ITEM<= TREE[PAR], then: Set TREE[PTR]=ITEM, and Return. [End of If structure] 5. Set TREE[PTR]=TREE[PAR] 6. Set PTR=PAR. [End of step 2] 7. Set TREE[1]=ITEM 8. Return.
  • 7. 7 Example of Deletion from Max Heap 20 remove 15 2 14 10 10 15 2 14 15 14 2 10
  • 8. Deletion from a Max Heap DELHEAP(TREE, N, ITEM) 1. Set ITEM= TREE[1]. 2. Set LAST= TREE[N] and N= N-1. 3. Set PTR=1, LEFT=2 and RIGHT=3. 4. Repeat Steps 5 to 7 while RIGHT<=N: 5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then: Set TREE[PTR]=LAST and Return. [End of If structure.] 1. If TREE[RIGHT]<= TREE[LEFT], then: Set TREE[PTR]= TREE[LEFT] and PTR=LEFT. Else: Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT. [End of If structure.] 1. Set LEFT=2*PTR and RIGHT=LEFT+1. [End of Step 4 loop.] 8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT] then: Set PTR=LEFT. 9. Set TREE[PTR]= LAST. 10. Return.
  • 9. HeapSort HEAPSORT(A, N) 1. Repeat for J=1 to N-1: Call INSHEAP(A, J, A[J+1]). [End of loop.] 1. Repeat while N>1: a) Call DELHEAP(A, N, ITEM). b) A[N+1]=ITEM. [End of loop.] 1. Exit.