Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.

Similar presentations


Presentation on theme: "CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly."— Presentation transcript:

1 CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly the bottom level, is filled - the bottom level is filled from left to right so that all vacant positions are on the right hand side D F J HKL complete binary tree D F J HKL not complete

2 CPSC 252 Binary Heaps Page 2 A maximum binary heap is a complete binary tree where the data in every node is greater than or equal to the data in each of its child nodes. 7 5 7 434 A minimum binary heap is a complete binary tree such that the data in every node is less than or equal to the data in each of its child nodes. 2 5 7 578

3 CPSC 252 Binary Heaps Page 3 It is important to realize that two binary heaps can contain the same data but the data may appear in different positions in the heap: 2 5 7 578 2 5 5 778 Both of the minimum binary heaps above contain the same data: 2, 5, 5, 7, 7, 8. Even though both heaps satisfy all the properties necessary of a minimum binary heap, the data is stored in different positions in the tree.

4 CPSC 252 Binary Heaps Page 4 The fact that a binary heap is a complete binary tree allows us to conveniently represent the heap using an array rather than a linked structure to represent the heap. D F J HKL DFJHKL We must now determine a way of navigating the heap. Using the contiguous representation, we can determine the following patterns. Let’s assume that we are at a node with index k in the array: index of left child:index of parent: index of right child:

5 CPSC 252 Binary Heaps Page 5 Inserting an Item into a Heap Consider the minimum heap presented below. Let’s suppose that we want to insert an E into the heap. We begin by inserting the E at the bottom of the heap (in such a way that the binary tree remains complete) and then performing a reheap up operation – we successively compare the new item with its parent. If it is smaller than its parent we swap it with the parent and continue… D F J HKL M D F J HKL M E

6 CPSC 252 Binary Heaps Page 6 D F J KL M D J KL M D F J HKL M E

7 CPSC 252 Binary Heaps Page 7 Algorithm: reheapUp( root, bottom ) if( bottom > root ) set parent to index of parent of bottom element if( data[ parent ] > data[ bottom ] ) swap( data[ parent ], data[ bottom ] ) reheapUp( root, parent )

8 CPSC 252 Binary Heaps Page 8 Removing the Item at the Top of a Heap Consider the minimum heap presented below. Suppose we want to remove the item at the top of the heap. We begin by replacing the item at the top of the heap with the rightmost element on the bottom level of the heap. This preserves the shape of the binary tree (it is still complete) but the order property is lost. We regain the ordering by performing a reheap down operation. We swap the root node with the smaller of its two child nodes and continue down the tree in this fashion until we reach the bottom of the tree. D F J HKL M

9 CPSC 252 Binary Heaps Page 9 HKLL F J HKL D F J HKL M

10 CPSC 252 Binary Heaps Page 10 Algorithm: reheapDown( root, bottom ) if( root is not a leaf ) set minChild to index of smaller child if( data[ root ] > data[ minChild ] ) swap( data[ root ], data[ minChild ] ) reheapDown( minChild, bottom )

11 CPSC 252 Binary Heaps Page 11 Time Complexity of Reheap Up and Reheap Down Operations The number of operations depends on the depth of the tree. A complete binary tree of height h has between 2 h-1 and 2 h - 1 nodes: D F J HKL M D F J H The height of the heap in terms of the number of nodes N in the heap is log 2 N, and the time complexity of the reheap up and reheap down operations is therefore also log 2 N.

12 CPSC 252 Binary Heaps Page 12 A priority queue is a data structure in which the smallest (or largest) element is always at the front. We can implement a priority queue using a binary heap. So the priority queue would be implemented as an ADT with a binary heap as the underlying concrete data structure. Enqueuing an item would amount to performing a reheap-up operation and is therefore O( log N ). Dequeuing an item would amount to performing a reheap- down operation and is therefore also O( log N ). We could also use a sorted or unsorted linear structure (such as a linked list or an array) to implement a priority queue. However, one or the other of the enqueue and dequeue operations is O(N).


Download ppt "CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly."

Similar presentations


Ads by Google