Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be placed on eighter the left or right subtree. Heaps
Ceng-112 Data Structures I Heaps A heap is a binary tree structure with the following rules: 1.The tree is complete or nearly complete. 2.The key value of each node is greater than or equal to the key value of each of its descendents. They are often implemented in an array rather than a linked list. We are able to calculate the location of the right and left subtrees.
Ceng-112 Data Structures I Figure 9-2 Heaps
Ceng-112 Data Structures I Figure 9-3 Heaps
Ceng-112 Data Structures I Figure 9-8
Ceng-112 Data Structures I A heap can be built in a dynamic tree structure. It is most often implemented in an array. This implementation is possible, because the heap is complete or nearly complete. Therefore the relationship between a node and its children is fixed and can be calculated as shown below: Heap Data Structure
Ceng-112 Data Structures I For a node located at index i, – its children are found at: Left child: 2i + 1 Right child: 2i + 2 –its parent is located at [(i – 1)/2] –Given the index for a left child j; its right sibling (j + 1) –Given the index for a right child k; its left sibling (k – 1) –Given the size n of a complete heap, the location of the first leaf is [n/2]. Heap Data Structure
Ceng-112 Data Structures I There are two basic maintenance operations that are performed on a heap: Insert a node and, Delete a node. To implement the insert and delete operations, we need two basic algorithms: –reheap up –reheap down Basic Heap Algorithms
Ceng-112 Data Structures I Figure 9-4 Heap Algorithms - ReheapUp
Ceng-112 Data Structures I Figure 9-5
Ceng-112 Data Structures I algorithm reheapUp (ref heap, val newnode ) Reestablishes heap by moving data in child up to its correct location in the heap array. PRE heap is array containing an invalid heap. newNode is index location to new data in heap. POST newNode has been inserted into heap. 1 if (newNode not zero) 1 parent = (newNode – 1) / 2 2 if (heap[newNode].key > heap[parent].key) 1 swap(newNode, parent) 2 reheapup(heap, parent) 2 return end reheapUp Heap Algorithms - ReheapUp
Ceng-112 Data Structures I Figure 9-6 Heap Algorithms - ReheapDown
Ceng-112 Data Structures I
Ceng-112 Data Structures I algorithm reheapDown (ref heap, val root, val last ) Reestablishes heap by moving data in root down to its correct location in the heap array. PRE heap is an array data. root is root of heap or subheap. last is an index to the last element in heap. POST heap has been restored. Heap Algorithms - ReheapDown
Ceng-112 Data Structures I algorithm reheapDown (ref heap, val root, val last ) Determine which child has larger key. 1 if (root *2 + 1 <= last) There is at least one child. 1 leftKey = heap[root * 2 +1].data.key 2 rightkey = heap[root *2 + 2].data.key 3 if (leftKey > rightKey) 1 largeChildKey = leftKey 2 largeChildIndex = root * else 1 largeChildKey = rightKey 2 largeChildIndex = root * Test if root > larger subtree. 5 if (heap[root].data.key < heap[largeChildIndex].data.key) 1 swap(root, largeChildIndex) 2 reheapDown(heap, largeChildIndex, last) end reheapdown Heap Algorithms - ReheapDown
Ceng-112 Data Structures I Heap Algorithms - Build Heap wall The build heap algorithm is very simple. Given the array we need to convert to a heap, we walk through the array, starting at the second element, calling reheap up for each array element to be inserted into the heap. algorithm buildHeap(ref heap, val size ) 1 walker = 1 2 loop (walker <= size) 1 reheapUp(heap, walker) 2 walker = walker return end buildHeap
Ceng-112 Data Structures I Figure 9-10 Heap Algorithms - Insert Heap To insert a node we need to locate the first empty leaf in the array. We move the new data to the first empty leaf and reheap up. algorithm insertHeap(ref heap, ref last, ref data ) 1 if (heap full) 1 return false 2 last=last heap[last] = data 4 reheapUp (heap, last) 5 return true end insertHeap
Ceng-112 Data Structures I Figure 9-11 Heap Algorithms - Delete Heap algorithm deleteHeap(ref heap, ref last, ref dataOut ) Deletes root of heap and passes data back to caller. Root has been deleted from heap and root data placed in dataOut. 1 if (heap empty) 1 return false 2 dataOut= heap[0] 3 heap[0] = heap[last] 4 last = last reheapDown (heap, 0, last) 6 return true end deleteHeap
Ceng-112 Data Structures I Heap Applications Common applications of Heaps are; 1.Selection algorithms, 2.Priority queues, 3.Sorting.
Ceng-112 Data Structures I Heap Applications – Selection Algorithms To determine the k th element in an unsorted list, there are two solution: 1.First sort the list and select the element at location k, or 2.Create a heap and delete k-1 elements from it.
Ceng-112 Data Structures I Figure 9-12 Example: If we want to know the fourth largest element of the list: Heap Applications – Selection Algorithms
Ceng-112 Data Structures I Heap Applications – Priority Queues The heap is an excellent structure to use for a priority queue. One common technique uses an encoded priority number that consists of the priority plus a sequential number represents the event’s place within the queue. The serial number must be in descending order.
Ceng-112 Data Structures I Figure 9-13 Heap Applications – Priority Queues
Ceng-112 Data Structures I Figure 9-15 Exercise Which of the structures is a heap and which is not?
Ceng-112 Data Structures I Figure 9-16 Apply the reheapUp algorithm to this nonheap structure: Exercise
Ceng-112 Data Structures I Figure 9-17 Exercise Apply the reheapDown algorithm to this nonheap structure:
Ceng-112 Data Structures I Figure 9-18 Exercise 1.Show the array implementation. 2.Apply the delete operation. Repair the heap after the deletion. 3.Insert 38 in the heap. Repair the heap after the insertion.
Ceng-112 Data Structures I Figure 9-19 Exercise 1.Show the left and right children of 32 and 27 in the heap. 2.Show the left children of 14 and Show the parent of 11, parent of 20 and parent of 25.
Ceng-112 Data Structures I Which of the following sequences are heaps? a b c Exercise
Ceng-112 Data Structures I HW-10 1.Use array structure and create a Heap tree with positive integer numbers which are taken from the screen. 1.Array should include 14 data entry. 2.Collect firts 8 entries from the user in this array as unordered. 3.Use “buildheap” algorithm and establish the heap tree structure on this array 2.Collect the other entries from user with “add new node” option and use “insertheap” algortihm to placed the node in correct position. 3.Write the Heep delete function which establishes to delete root entry from the Heap. 4.Write a function which lists the entries of the Heap in array structure. 5.Collect all above functions under a user menu. Load your HW-10 to FTP site until 21 May. 07 at 09:00 am.