Post on 13-Dec-2015
Chapter 9. Heaps
[INA240] Data Structures and PracticeYoun-Hee Han
http://link.kut.ac.kr
1. Basic ConceptsWhere We Are?
Tree Binary Tree Binary Search Tree AVL Search Tree
Heap
힙 (Heap): 수북하게 쌓아
올린 더미
Splay Tree
1. Basic ConceptsHeap: a binary tree structure with the properties …
The tree is complete or nearly complete The key value of each node is greater than or equal to the key
value in each of its descendents Root 의 키가 왼쪽자식과 오른쪽 자식의 키보다 크거나 같다 .
The subtrees are in turn heaps
주의할 특징 왼쪽 자식과 오른쪽 자식 사이에는 어느 쪽 키가 크던 상관이 없다 .
1. Basic ConceptsHeap 의 속성
A heap is a special kind of tree. It has two properties that are not generally true for other trees:[Completeness] - The tree is (nearly) complete, which means that nodes are added from top to bottom, left to right, without leaving any spaces. [Heapness] - The item in the tree with the highest priority is at the top of the tree, and the same is true for every subtree.
(nearly)
Data Structure5
1. Basic Concepts
Max Heap ( 일반적 의미의 Heap) 키 값이 큰 레코드가 우선순위가 높은 것으로 간주 루트노드의 키가 가장 크다 .
Min Heap Max Heap 과 정 반대 키 값이 작을수록 우선순위가 높다 . 예 ] 내신등급
1. Basic ConceptsExamples of Heaps
Invalid heaps (Why?)
Data Structure7
1. Basic Concepts
정렬 관점에서… BST 는 약한 의미로 정렬 .
왼쪽 자식 키보다 오른쪽 자식 키가 크다 . Heap 도 약한 의미로 정렬 .
부모의 키가 자식의 키보다 우선 순위가 높다 왼쪽 자식 키와 오른쪽 자식 키는 무관하다 .
Heap Binary search tree
Data Structure8
2. Maintenance Operations (p. 391)
Although Heap is a tree, it is meaningless to traverse it, search it, or print it out.
Important operations Insertion Deletion
Problem: preserving conditions of max heap 1st condition: Completeness 2nd condition: Heapness
Data Structure9
2. Maintenance Operations
Inserting a node into heap1. Insert the node to heap
To preserve 1st condition, the new node should be added at the last leaf level at the first empty position
2nd condition could be broken
2. Repair the structure so that 2nd condition is satisfied Reheap Up
Data Structure10
2. Maintenance Operations
Inserting a node into heap 효율
O(lgN) 삭제보다 비교 횟수가 적음
[ 힙의 삽입 ]“ 신입사원이 오면 일단 말단 자리에 앉힌 다음 , 능력껏 위로 진급시키는 것 ( 進級 , Reheap Up, Promotion)”
Data Structure11
2. Maintenance Operations
Deleting a node from heap1. Deletion always occurs at the root
To preserve 1st condition, the last node should move to the root
2nd condition could be broken
2. Repair the structure so that 2nd condition is satisfied Reheap Down
[ 힙의 삭제 ]“ 사장자리가 비면 말단 사원을 그 자리에 앉힌 다음 , 바로 아래 부하직원보다는 능력이 좋다고 판단될 때까지 강등시키는 것 ( 降等 , Reheap Down, Demotion)”
Data Structure12
2. Maintenance Operations
Deleting a node from heap 우선순위가 가장 큰
루트노드를 삭제 Completeness 를 유지
하기 위해서는 루트의 삭제와 동시에 마지막 요소를 루트노드 위치로 옮김 .
Reheap Down 힙 모습의 복원 (Heap Rebuilding) Reheap ! 루트로부터 시작해서 제자리를 찾기까지 굴러 떨어짐 왼쪽 , 오른쪽 자식 모두를 비교해서 더 큰 것과 스와핑 (Swaping)
Data Structure13
2. Maintenance Operations
Deleting a node from heap 효율
마지막 원소를 루트로 복사하는 데 O(1) 최악의 경우 루트로부터 리프까지 굴러 떨어짐 .. 비교의 횟수는 총 2lgN 이 된다 . 삭제 효율은 O(1) + O(2lgN) ≈ O(lgN)
Data Structure14
2. Maintenance Operations
BST vs. Heap 삽입 , 삭제 , 탐색의 관점에서 Heap 이 BST 보다 더 좋은
구조임 . BST 는 최악의 경우 연결 리스트와 유사 O(N) 의 효율
BST 는 Skewed Binary Tree ( 편향된 이진 트리 ) 가 가능 힙은 완전 (Complete or Nearly Complete) 이진트리로서 균형
트리 균형 트리의 높이는 항상 lg(N) 에 가까움
탐색은 가장 큰 (or 작은 ) 값의노드를 찾는 작업
Data Structure15
3. Heap Implementation
Heap 구현 배열로 표시하는 것이 구현에 매우 효율적
Why? Heap is complete or nearly complete!!! Parent 와 Child 사이 , Siblings 사이에 배열에서의 그 위치에 대한 규칙
형성 배열에 위치하는 순서
루트부터 시작해서 위에서 아래로 , 왼쪽에서 오른쪽으로 진행 Root 노드는 항상 배열 인덱스 0.
Data Structure16
3. Heap Implementation
Heap 구현 (n: 노드의 총 개수 ) 배열 인덱스 i 에 위치한 노드에 대해서… LeftChild(i) is at 2i + 1 RightChild(i) is at 2i + 2 Parent(i) is at (i-1)/2
If i = 0, i is at the root and has no parent.
Data Structure17
3. Heap Implementation
Heap 구현 (n: 노드의 총 개수 ) Right Sibling(j) is at j + 1 Left Sibling(k) is at k - 1 The location of the first leaf is at n/2 The location of the last non-leaf is at n/2 - 1
Data Structure18
3. Heap Implementation
Insert Heap
Algorithm insertHeap (heap, last, data)// heap: array of valid heap// last: index to the last node in heap// data: data to be inserted
1. if (heap full) 1. return false2. end if3. last++4. move data to last node5. reheapUp (heap, last)6. Return true
Data Structure19
3. Heap Implementation
Reheap Up
Algorithm reheapUp (heap, newNode)// heap: array containing an invalid heap// newNode: index location to new data in heap
1. if (newNode is not the root) 1. set parent to parent of newNode 2. if (newNode Key > parent Key) 1. exchange newNode and parent 2. reheapUp (heap, parent) 3. end if2. end if
Data Structure20
3. Heap Implementation
Heap Build
Algorithm buildHeap (heap, size)// heap: array containing an invalid heap// size: number of elements in array
1. Set walker to 12. loop (walker < size) 1. reheapUp (heap, walker) 2. walker++3. end loop
Building a heap from unsorted array
Data Structure21
3. Heap Implementation
Delete Heap Node
Algorithm deleteHeap (heap, last, dataOut)// heap: array of valid heap// last: index to the last node in heap// dataOut: reference for output area
1. if (heap empty) 1. return false2. end if3. set dataOut to root data4. move last data to root5. reheapDown (heap, 0)6. Return true
Data Structure22
3. Heap Implementation
Reheap Down
Algorithm reheapDown (heap, root)// heap: array of data// root: root of heap or subheap1. if (there is a left subtree) 1. set leftkey to left subtree key 2. if (there is a right subtree) 1. set rightKey to tight subtree key 2. if (leftKey > rightKey) 1. set largeSubtree to left subtree 3. else 1. set largeSubtree to right subtree 3. else 1. set largeSubtree to left subtree 4. if (root key < largeSubtree key) 1. exchange root and largeSubtree 2. reheapDown (heap, largeSubtree) 5. end if2. end if
4545
6767
5656
3232
88 2323 1919
45 67 32 56 8 32 23 19
reheapDown
Data Structure23
4. Heap Application - Selecting Kth Element
Selecting Kth Element in an unsorted list First solution
Sort the list & Select the element at location k Second solution
USE HEAP!!!
How to execute the second solution? 1. Heap Creation from an unordered list
크기 순으로 4 번째 엘리먼트는 ?
Data Structure24
How to execute the second solution? 2. delete k – 1 elements from heap 3. place the deleted element at the end of the heap and
reduce the heap size by 1 4. After k – 1 elements have been deleted and moved, the top
element (index 0 in array) is the kth element
5. reheap to restore the heap so that we are ready for another selection
찾고자 하는 4 번째 엘리먼트
4. Heap Application - Selecting Kth Element
Data Structure25
What is Priority Queue? 응급실에서 환자 치료의 예
큐 : 먼저 온 사람을 먼저 치료 스택 : 나중에 온 사람을 먼저 치료 우선순위 큐 (Priority Queue) : 우선순위가 높은 사람 ( 예 : 위급한 사람 ) 을 먼저 치료
Priority Queue
5. Heap Application – Priority Queue
an abstract data type supporting the following three operations:
- add an element to the queue with an associated priority - Remove and return the element from the queue that has the highest priority- (optionally) peek at the element with highest priority without removing it
Data Structure26
Priority Queue vs. Stack/Queue 시간 : 스택 , 큐
스택 , 큐에서는 시간에 따라 자료구조를 조직화 시간을 포함한 여러 가지 가치를 우선순위로 가지는 자료구조
우선순위 큐 우선 순위 큐는 각 노드에 “ 우선순위 값” 필드가 필요 .
우선순위 큐는 스택이나 큐 보다 일반적인 구조큐나 스택은 우선순위 큐의 특수한 형태로서 시간에 그 우선순위를 부여한 것임
따라서 키 필드가 불필요
5. Heap Application – Priority Queue
Priority Queue
Stack Queue
Data Structure27
Heap is an excellent structure to implement priority queue
5. Heap Application – Priority Queue
Data Structure28
Priority Queue Example An event is represented by
Priority number (1 ~ 5) Serial number (0 ~ 999)
5. Heap Application – Priority Queue
Data Structure29
Priority Queue Example
5. Heap Application – Priority Queue
Data Structure30
6. Heap Efficiency
Heap Efficiency Heap is efficient for extraction of the largest (or
smallest) element. Heap is efficient for both insertion and deletion Heap is a compromise of sorted and unsorted structures
Sorted (array or linked-list) structures Very efficient in extraction of largest (smallest)
element Inefficient in insertion
Unsorted (array or linked-list) structures Inefficient in extraction of largest (smallest) element Very efficient in insertion
Heap Efficiency
Representation Insertion Deletion
Unordered array O(1) O(n)
Unordered linked list O(1) O(n)
Sorted array O(n) O(1)
Sorted linked list O(n) O(1)
Heap O(log n) O(log n)
6. Heap Efficiency
Data Structure31