*This preview shows
pages
1–12. Sign up
to
view the full content.*

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
**Unformatted text preview: **Computer Science Department University of Central Florida Binary Heaps & Priority Queues COP 3502 – Computer Science I Binary Heaps & Priority Queues page 2 Binary Heaps Heap: A heap is an Abstract Data Type Just like stacks and queues are ADTs Meaning, we will define certain behaviors that dictate whether or not a certain data structure is a heap So what is a heap? More specifically, what does it do or how do they work? A heap looks similar to a tree But a heap has a specific property/invariant that each node in the tree MUST follow Binary Heaps & Priority Queues page 3 Binary Heaps Heap: In a heap, all values stored in the subtree of a given node must be less than or equal to the value stored in that node This is known as the heap property And it is this property that makes a heap a heap! Binary Heaps & Priority Queues page 4 Binary Heaps Heap: In a heap, all values stored in the subtree of a given node must be less than or equal to the value stored in that node If B is a child of node A, then the value of node A must be greater than or equal to the value of node B This is a called a Max-Heap Where the root stores the highest value of any given subtree Binary Heaps & Priority Queues page 5 Binary Heaps Heap: Alternatively, if all values stored in the subtree of a given node are greater than or equal to the value stored in that node This is called a Min-Heap (where root is smallest value) Binary Heaps & Priority Queues page 6 Binary Heaps Binary Heap: What we just described was a basic Heap Now for a heap to be Binary Heap , it must adhere to one other property: The Shape Property : The heap must be a complete binary tree Meaning, all levels of the tree, except possibly the last one, must be fully filled And if the last level is not complete, the nodes of the level are filled from left to right ***And it just so happens that the previous pictures shown were all examples of binary heaps Binary Heaps & Priority Queues page 7 Binary Heaps Building a Complete Binary Tree: When a complete binary tree is built, its first node must be the root. Root Binary Heaps & Priority Queues page 8 Binary Heaps Building a Complete Binary Tree: Left child of the root The second node is always the left child of the root. Binary Heaps & Priority Queues page 9 Binary Heaps Building a Complete Binary Tree: Right child of the root The third node is always the right child of the root. Binary Heaps & Priority Queues page 10 Binary Heaps Building a Complete Binary Tree: The next nodes always fill the next level from left-to- right . . Binary Heaps & Priority Queues page 11 Binary Heaps Building a Complete Binary Tree: The next nodes always fill the next level from left-to- right ....

View
Full
Document