14heap
14heap
Sungmin Cha
New York University
10.23.2024
Outline
• Notice
• Heap
– Priority queue
– Operations of heap
– Heap Sort
2
Outline
• Notice
• Heap
– Priority queue
– Operations of heap
– Heap Sort
3
Notice
• Midterm exam
– 12:30 PM on Monday, October 28th
– Duration: 1 hour 15mins
– Location: 60 Fifth Ave, 110
– The Midterm exam covers Lecture 1-14
– Offline exam and closed book (no cheat sheets allowed)
– Include True/False and multiple-choice questions, subjective
questions, and filling in appropriate pseudo code for blanks
All test questions will be created based on lecture slides and HW1-2
(Labs are not considered)
There will be 1-2 challenging questions
In total, there will be 10-12 questions (excluding T/F questions)
4
Outline
• Notice
• Heap
– Priority queue
– Operations of heap
– Heap Sort
5
Quick Sort
• Quick sort
– A divide-and-conquer algorithm devised by Charles Antony
Richard Hoare
6
Quick Sort
• Pseudo code of quick sort
7
Quick Sort
• Time complexity of quick sort (Big-O, worst case)
– When is the worst case of quick sort?
In case of each subarray being unevenly divided
(when the array is already sorted)
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
…
1 2
1 2 8
Quick Sort
• Time complexity of quick sort (Big-O, best case)
– When is the best case of quick sort?
In case of each subarray being evenly divided
n/2 n/2
1 1 1 1 1 1 1 1
9
Quick Sort
• Pros and Cons of quick sort
– Pros
Fastest sorting algorithm on average (empirically)
Do not require additional memory
– Cons
time complexity of O(n^2) in case of the worst case
10
Quick Sort
• Merge vs Quick sort
– They are based on divide-and-conquer but
Merge sort: the given array is divided into smaller units until it cannot
be divided further, and sorting is performed during the merging process.
Quick sort: sorting is performed during the process of dividing the
given array.
– Additional memory
Merge sort requires an additional memory but quick sort is not
11
Tree
• What is Tree?
– A non-linear hierarchical data structure where nodes are
connected like branches of a tree
12
Tree
• Usage of Tree
1. It can be used to implement a Head (priority queue)
Sorting an array using Heap => Heap sort
2. It can be used to store a dataset in a tree-structured format
Example: directory structure and organizational chart
13
Tree
• Binary tree
– A tree where every node has at most two child nodes
• Full binary tree
– A binary tree that has exactly two child nodes and is fully
occupied
– Example: 9
8 8
4 7 3 5
2 1 3 4 9 1 2 7
8 8
4 7 3 5
2 1 3
15
Outline
• Notice
• Heap
– Priority queue
– Operations of heap
– Heap Sort
16
Heap
• Priority queue
– The data structure that returns and deletes the data with the
highest priority, regardless of the order of data addition.
– Example: managing messages with priority
8 8
4 7 3 5
2 1 3 18
Heap
• Heap and its corresponding array
– Heap is generally implemented using an array
Because we can easily get the index of a parent and child node of a node
9
The child nodes of A[k]
are A[2k+1] and A[2k+2]
8 A[1] 8
A[3] 4 7 A[4] 3 5
2 1 3
9 8 8 4 7 3 5 2 1 3
19
Heap
• Heap and its corresponding array
– Heap is generally implemented using an array
Because we can easily get the index of a parent and child node of a node
𝑘−1
9 The parent node of A[k] is A[⌊ ⌋]
2
8 8 A[2]
4 7 3 5 A[6]
2 1 3
9 8 8 4 7 3 5 2 1 3
20
Heap
• ADT of Heap
7 8
4 3 3 5
2 1
A[0]
9 7 8 4 3 3 5 2 1
22
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
1. insert ‘8’ to the end of A[0, …, n-1]
7 8
4 3 3 5
2 1 8
A[0]
9 7 8 4 3 3 5 2 1 8
23
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
2. compare A[9]=8 with its parent node (A[4] = 3)
7 8
4 3 3 5
24
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
2. compare A[9]=8 with its parent node (A[4] = 3)
7 8
4 8 3 5
25
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
2. compare A[4]=8 with its parent node (A[1] = 7)
7 8
4 8 3 5
2 1 3
A[0] A[1] A[4]
9 7 8 4 8 3 5 2 1 3
26
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
2. compare A[4]=8 with its parent node (A[1] = 8)
4 8 3 5
2 1 3
A[0] A[1] A[4]
9 7 8 4 8 3 5 2 1 3
27
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
2. compare A[4]=8 with its parent node (A[1] = 8)
8 8
4 7 3 5
28
Insert Operation of Heap
• Implementation of insert(x) function
– Example: Insert ‘8’ to the heap A[0, …, n-1] (insert(8))
3. compare A[1]=8 with its parent node (A[0] = 9)
9
A[1]=8 < A[0]=9
: satisfy the property
8 8
4 7 3 5
2 1 3
A[0] A[1]
9 8 8 4 7 3 5 2 1 3
29
Insert Operation of Heap
• Implementation of insert(x) function
– PercolateUp operation
1. Compare node A[i] with its parent’s value and
swap them if they break the heap property
2. Compare the changed parent with its parent again and
if it doesn’t satisfy the heap property
3. Continue this process until reaching
o the first point where the heap property is satisfied
o or until it cannot go further up
30
Insert Operation of Heap
• Pseudo algorithm of insert(x) function
8 8
4 7 3 5
2 1 3
A[0]
9 8 8 4 7 3 5 2 1 3
32
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
1. delete and return the value of the root node
8 8
4 7 3 5
2 1 3
A[0]
9 8 8 4 7 3 5 2 1 3
33
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
1. delete and return the value of the root node
4 7 3 5
2 1 3
A[0]
8 8 4 7 3 5 2 1 3
34
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
2. Move the last node to the root node
8 8
4 7 3 5
2 1 3
A[0]
8 8 4 7 3 5 2 1 3
35
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
2. Move the last node to the root node
8 8
4 7 3 5
2 1
A[0]
3 8 8 4 7 3 5 2 1
36
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
3. compare A[0]=3 with its largest child node (A[1] = 7)
3
A[0]=3 < A[1]=8
8 8
4 7 3 5
2 1
A[0] A[1]
3 8 8 4 7 3 5 2 1
37
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
3. compare A[0]=3 with its largest child node (A[1] = 7)
3 8
4 7 3 5
A[0] A[1]
8 3 8 4 7 3 5 2 1
38
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
4. compare A[1]=3 with its largest child node (A[1] = 7)
3 8
4 7 3 5
A[1] A[4]
8 3 8 4 7 3 5 2 1
39
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
4. compare A[1]=3 with its largest child node (A[1] = 7)
7 8
4 3 3 5
A[1] A[4]
8 7 8 4 3 3 5 2 1
40
Delete Operation of Heap
• Implementation of deleteMax() function
– Example: deleteMax()
5. A[4]=3 is the leaf node
7 8
4 3 3 5
A[1] A[4]
8 7 8 4 3 3 5 2 1
41
Delete Operation of Heap
• Implementation of deleteMax() function
– PercolateDown operation
1. Return the value of the root node
and move the last node to the root node position
2. Compare the changed root node with its child nodes
and swap if it doesn’t satisfy the heap property
3. Compare the changed node with its child nodes
and swap if it doesn’t satisfy the heap property
4. Repeat step 3 until the heap property is satisfied
or until reaching a leaf node
42
Delete Operation of Heap
• Pseudo algorithm of deleteMax() function
A[0]
4 1 8 7 3 3 5 2 9
Heap
44
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
1. get an array and interpret it as a complete binary tree
1 8
7 3 3 5
2 9
A[0]
4 1 8 7 3 3 5 2 9
45
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
1. get an array and interpret it as a complete binary tree
(4) Sequentially make each subtree
satisfy the heap property
4 starting from the smallest one
1 8
7 3 3 5
(1) (2)
2 9 (3)
A[0]
4 1 8 7 3 3 5 2 9
46
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
2. compare A[3]=7 with its largest child node (A[8] = 9)
1 8
7 3 3 5
A[3]=7 < A[8]=9
2 9
1 8
9 3 3 5
1 8
A[2]=8 > A[6]=5
9 3 3 5
2 7
1 8
A[1]=1 < A[3]=9
9 3 3 5
2 7
9 8
1 3 3 5
9 8
1 3 3 5
A[3]=1 < A[8]=7
2 7
9 8
7 3 3 5
9 8
7 3 3 5
2 1
A[0] A[1]
4 9 8 7 3 3 5 2 1
54
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
5. compare A[0]=4 with its largest child node (A[1] = 9)
4 8
7 3 3 5
2 1
4 8
A[1]=4 < A[3]=7
7 3 3 5
2 1
A[1] A[3]
9 4 8 7 3 3 5 2 1
56
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
6. compare A[1]=4 with its largest child node (A[3] = 7)
7 8
3 3 3 5
2 1
7 8
3 3 3 5
A[3]=4 > A[7]=2
2 1
A[3] A[7]
9 7 8 3 3 3 5 2 1
58
Build Operation of Heap
• Implementation of buildHeap() function
– Example: buildHeap()
8. Checked all subtrees!
7 8
3 3 3 5
2 1
A[3] A[7]
9 7 8 3 3 3 5 2 1
59
Build Operation of Heap
• Implementation of buildHeap() function
– The overall idea of buildHeap()
Sequential access to subtrees
9 A[0]
A[1] 7 8 A[2]
A[3] 3 3 3 5
2 1
9 A[0]
A[1] 7 8 A[2]
A[3] 3 3 3 5
2 1
63
Review for the midterm exam
• What we have learned so far
– JAVA’s variables and memory allocation of them
– Recursion and time complexity
– Array and Linked List
– Stack and Queue
Implemented by an array and linked list
– Sorting algorithm
Bubble, selection, insertion
Merge, quick
64
Review for the midterm exam
• JAVA’s variables and memory allocation
– Primitive and reference variable
– Memory allocation in Stack and Heap memory
• Sorting
– Why do we need to sort the data?
– How to implement sorting algorithms?
– Time complexity of them?
What is the time complexity in the best and worst cases?
66
Review for the midterm exam
• Tree and Heap
– Terminology
– Binary tree
– Heap and its property and operations
– Heap sort
67
Thank you!
E-mail: [email protected]
68