3. Introduction
• Heap (MaxHeap): A complete binary tree H with
n elements is called a Heap if each node N of H has the
following property:
“ The value at N is greater than or equal to the value
at each of the children of N.”
• If the value at N is less than or equal to the value at each of the
children of N, then it is called MinHeap.
• Heaps are maintained in memory by using linear array TREE.
7. Insertion in a Heap
• INSERT_HEAP(TREE, N, ITEM)
1.Set N = N+1, and PTR = N.
2.Repeat Step 3 to 6 while PTR > 1
3. Set PAR =
4. If ITEM <= TREE [PAR], then:
Set TREE[PTR] = ITEM and Return.
5. Else: Set TREE[PTR] = TREE[PAR].
6. Set PTR = PAR.
7.Set TREE[1] = ITEM.
8.Return.
/ 2
PTR
9. Example
• Create a Heap from the following list of numbers:
40, 30, 50, 20, 60, 55, 70, 60, 65, 50
10. Deletion of Root of a Heap
•Can we directly delete any element from the heap?
11. • You can delete either from the root or the
rightmost element from last level.
•Assign Root R to some variable ITEM.
• Replace the deleted node R by the last node
L of Heap H so that H is still complete tree,
but not a Heap.
• Call MaxHeapify/MinHeapify to maintain
the heap property.
13. Consider the following keys
• 1,2,3,4,5,6,7
• Perform the deletion operation.
• Step 1-Delete from the root
• Step 2- Replace it with rightmost element
from the last level(Heapify)
17. 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.]
6. 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.]
7. Set LEFT=2*PTR and RIGHT=LEFT+1.
[End of Step 4 loop.]
8. If LEFT=N and if LAST < TREE[LEFT] set TREE[PTR]= TREE[LEFT]
Set PTR=LEFT.
9. Set TREE[PTR]= LAST.
10. Return.
22. Maintaining Heap Property
MAX_HEAPIFY(A, i)
1.Set: l = LEFT (i)
2.Set: r = RIGHT (i)
3.If l <= heapsize [A] and A[l] > A[i], then:
4. largest = l.
5.Else: largest = i.
6.If r <= heapsize [A] and A[r] > A[largest], then:
7. largest = r.
8.If largest != i, then:
9. Exchange A[i] A[largest].
10. MAX_HEAPIFY (A, largest).
23. Heap Sort
HEAP_SORT (A)
1.BUILD_MAXHEAP (A)
2.Repeat for i = length[A] to 2
3. Exchange A[1] A[i].
4. Heapsize[A] = Heapsize [A] ─ 1.
5. Call MAX_HEAPIFY(A, 1).
6.Exit.
24. Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
26. Review Questions
• What do you mean by Heap?
• What is the complexity of Heap sort?
• Calculate the complexity of Heap sort.
• How will you insert an element in a heap?
• What is the difference between Maxheap and Minheap?
Editor's Notes
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향
목차
자바의 생성배경
자바를 사용하는 이유
과거, 현재, 미래의 자바
자바의 발전과정
버전별 JDK에 대한 설명
자바와 C++의 차이점
자바의 성능
자바 관련 산업(?)의 경향