1 / 31

Chapter 9. Heaps

Chapter 9. Heaps. Internet Computing Laboratory @ KUT Youn-Hee Han. 1. Basic Concepts. Where We Are?. Tree. Binary Tree. AVL Search Tree. Binary Search Tree. Heap. 힙 (Heap): 수북하게 쌓아 올린 더미. 1. Basic Concepts. Heap : a binary tree structure with the properties …

echarles
Download Presentation

Chapter 9. Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9. Heaps Internet Computing Laboratory @ KUT Youn-Hee Han

  2. 1. Basic Concepts • Where We Are? Tree Binary Tree AVL Search Tree Binary Search Tree Heap 힙 (Heap): 수북하게쌓아 올린 더미

  3. 1. Basic Concepts • Heap: 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 • 주의할 특징 • 왼쪽 자식과 오른쪽 자식 사이에는 어느 쪽 키가 크던 상관이 없다.

  4. 1. Basic Concepts • Heap의속성 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)

  5. 1. Basic Concepts • Max Heap (일반적 의미의 Heap) • 키 값이 큰 레코드가 우선순위가 높은 것으로 간주 • 루트노드의 키가 가장 크다. • Min Heap • Max Heap과 정 반대 • 키 값이 작을수록 우선순위가 높다. • 예] 내신등급 Data Structure

  6. 1. Basic Concepts • Examples of Heaps • Invalid heaps (Why?)

  7. 1. Basic Concepts • 정렬 관점에서… • BST는 약한 의미로 정렬. • 왼쪽 자식 키보다 오른쪽 자식 키가 크다. • Heap도 약한 의미로 정렬. • 부모의 키가 자식의 키보다 우선 순위가 높다 • 왼쪽 자식 키와 오른쪽 자식 키는 무관하다. Heap Binary search tree Data Structure

  8. 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 Structure

  9. 2. Maintenance Operations • Inserting a node into heap 1. 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 Structure

  10. 2. Maintenance Operations • Inserting a node into heap • 효율 • O(lgN) • 삭제보다 비교 횟수가 적음 • [힙의 삽입] • “신입사원이 오면 일단 말단 자리에 앉힌 다음, 능력껏 위로 진급시키는 것(進級, Reheap Up, Promotion)” Data Structure

  11. 2. Maintenance Operations • Deleting a node from heap 1. 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 Structure

  12. 2. Maintenance Operations • Deleting a node from heap • 우선순위가 가장 큰 루트노드를 삭제 • Completeness를 유지하기 위해서는 루트의 삭제와 동시에 마지막 요소를 루트노드 위치로 옮김. • Reheap Down • 힙 모습의 복원(Heap Rebuilding)  Reheap ! • 루트로부터 시작해서 제자리를 찾기까지 굴러 떨어짐 • 왼쪽, 오른쪽 자식 모두를 비교해서 더 큰 것과 스와핑 (Swaping) Data Structure

  13. 2. Maintenance Operations • Deleting a node from heap • 효율 • 마지막 원소를 루트로 복사하는 데 O(1) • 최악의 경우 루트로부터 리프까지 굴러 떨어짐.. • 비교의 횟수는 총 2lgN이 된다. • 삭제 효율은 O(1) + O(2lgN) ≈ O(lgN) Data Structure

  14. 2. Maintenance Operations • BST vs. Heap • 삽입, 삭제, 탐색의 관점에서 Heap이 BST보다 더 좋은 구조임. • BST는 최악의 경우 연결 리스트와 유사 O(N)의 효율 • Skewed Binary Tree (편향된 이진 트리) • 힙은 완전 이진트리로서 균형 트리 • 균형 트리의 높이는 항상 lg(N)에 가까움 탐색은 주로 가장 큰 (or 작은) 값의노드를 찾는 작업 Data Structure

  15. 3. Heap Implementation • Heap 구현 • 배열로 표시하는 것이 효율적 • Why? Heap is complete or nearly complete!!! • Parent와 Child 사이, Siblings 사이에 배열에서의 그 위치에 대한 규칙 형성 • 배열에 위치하는 순서 • 루트부터 시작해서 위에서 아래로, 왼쪽에서 오른쪽으로 진행 • Root 노드는 항상 배열 인덱스 0. Data Structure

  16. 3. Heap Implementation • Heap 구현 (n: 노드의 총 개수) • 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 Structure

  17. 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 Structure

  18. 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 false 2. end if 3. last++ 4. move data to last node 5. reheapUp (heap, last) 6. Return true Data Structure

  19. 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 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 if 2. end if Data Structure

  20. 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 1 2. loop (walker < size) 1. reheapUp (heap, walker) 2. walker++ 3. end loop Building a heap from unsorted array Data Structure

  21. 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 false 2. end if 3. set dataOut to root data 4. move last data to root 5. reheapDown (heap, 0) 6. Return true Data Structure

  22. 3. Heap Implementation • Reheap Down Algorithm reheapDown (heap, root) // heap: array of data // root: root of heap or subheap 1. 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 if 2. end if 45 67 32 56 8 23 19 reheapDown Data Structure

  23. 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 Structure

  24. 4. Heap Application - Selecting Kth Element • 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 번째엘리먼트 Data Structure

  25. 5. Heap Application – Priority Queue • What is Priority Queue? • 응급실에서 환자 치료의 예 • 큐: 먼저 온 사람을 먼저 치료 • 스택: 나중에 온 사람을 먼저 치료 • 우선순위 큐 (Priority Queue) : 우선순위가 높은 사람 (예: 위급한 사람)을 먼저 치료 • 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 Structure

  26. 5. Heap Application – Priority Queue • Priority Queue vs. Stack/Queue • 시간: 스택, 큐 • 스택, 큐에서는 시간에 따라 자료구조를 조직화 • 시간을 포함한 여러 가지 가치를 우선순위로 가지는 자료구조우선순위 큐 • 우선 순위 큐는 각노드에 “우선순위 값” 필드가 필요. • 우선순위 큐는 스택이나 큐 보다 일반적인 구조 • 큐나 스택은 우선순위 큐의 특수한 형태로서 시간에 그 우선순위를 부여한 것임 • 따라서 키 필드가 불필요 Priority Queue Stack Queue Data Structure

  27. 5. Heap Application – Priority Queue • Heap is an excellent structure to implement priority queue Data Structure

  28. 5. Heap Application – Priority Queue • Priority Queue Example • An event is represented by • Priority number (1 ~ 5) • Serial number (0 ~ 999) Data Structure

  29. 5. Heap Application – Priority Queue • Priority Queue Example Data Structure

  30. 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 Data Structure

  31. 6. Heap Efficiency • Heap Efficiency Data Structure

More Related