Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ceng-112 Data Structures I2007 1 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.

Similar presentations


Presentation on theme: "Ceng-112 Data Structures I2007 1 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."— Presentation transcript:

1 Ceng-112 Data Structures I2007 1 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

2 Ceng-112 Data Structures I2007 2 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.

3 Ceng-112 Data Structures I2007 3 Figure 9-2 Heaps

4 Ceng-112 Data Structures I2007 4 Figure 9-3 Heaps

5 Ceng-112 Data Structures I2007 5 Figure 9-8

6 Ceng-112 Data Structures I2007 6 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

7 Ceng-112 Data Structures I2007 7 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

8 Ceng-112 Data Structures I2007 8 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

9 Ceng-112 Data Structures I2007 9 Figure 9-4 Heap Algorithms - ReheapUp

10 Ceng-112 Data Structures I2007 10 Figure 9-5

11 Ceng-112 Data Structures I2007 11 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

12 Ceng-112 Data Structures I2007 12 Figure 9-6 Heap Algorithms - ReheapDown

13 Ceng-112 Data Structures I2007 13

14 Ceng-112 Data Structures I2007 14 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

15 Ceng-112 Data Structures I2007 15 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 * 2 + 1 4 else 1 largeChildKey = rightKey 2 largeChildIndex = root * 2 + 2 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

16 Ceng-112 Data Structures I2007 16 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 + 1 3 return end buildHeap

17 Ceng-112 Data Structures I2007 17 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 + 1 3 heap[last] = data 4 reheapUp (heap, last) 5 return true end insertHeap

18 Ceng-112 Data Structures I2007 18 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 - 1 5 reheapDown (heap, 0, last) 6 return true end deleteHeap

19 Ceng-112 Data Structures I2007 19 Heap Applications Common applications of Heaps are; 1.Selection algorithms, 2.Priority queues, 3.Sorting.

20 Ceng-112 Data Structures I2007 20 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.

21 Ceng-112 Data Structures I2007 21 Figure 9-12 Example: If we want to know the fourth largest element of the list: Heap Applications – Selection Algorithms

22 Ceng-112 Data Structures I2007 22 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.

23 Ceng-112 Data Structures I2007 23 Figure 9-13 Heap Applications – Priority Queues

24 Ceng-112 Data Structures I2007 24 Figure 9-15 Exercise Which of the structures is a heap and which is not?

25 Ceng-112 Data Structures I2007 25 Figure 9-16 Apply the reheapUp algorithm to this nonheap structure: Exercise

26 Ceng-112 Data Structures I2007 26 Figure 9-17 Exercise Apply the reheapDown algorithm to this nonheap structure:

27 Ceng-112 Data Structures I2007 27 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.

28 Ceng-112 Data Structures I2007 28 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 40. 3.Show the parent of 11, parent of 20 and parent of 25.

29 Ceng-112 Data Structures I2007 29 Which of the following sequences are heaps? a.42 35 37 20 14 18 7 10 b.42 35 18 20 14 30 10 c.20 20 20 20 20 20 Exercise

30 Ceng-112 Data Structures I2007 30 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.


Download ppt "Ceng-112 Data Structures I2007 1 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."

Similar presentations


Ads by Google