1 / 31

Heaps Definition

INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll # 07-22 Semester BIT 3 rd Session 2007—2011 Department of Computer Science, B.Z.University Multan. Heaps Definition.

Download Presentation

Heaps Definition

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. INTRO TO HEAP & Adding and Removing a NODEPresented to: Sir AHSAN RAZA Presented by:SHAH RUKH Roll # 07-22 Semester BIT 3rd Session 2007—2011 Department of Computer Science, B.Z.University Multan

  2. Heaps Definition • A heapis a certain kind of complete binary tree.

  3. Binary TREE It has a Root and has two CHILDREN. Left child or left subtree. Right child or right subtree. Every child has its two children (left & right) then this child is called a node. If a node has no children then this node is said to be as a leaf.

  4. Heaps • A heap is a certain kind of complete binary tree. Root When a complete binary tree is built, its first node must be the root.

  5. Heaps • Complete binary tree. Left child of the root The second node is always the left child of the root.

  6. Heaps • Complete binary tree. Right child of the root The third node is always the right child of the root.

  7. Heaps • Complete binary tree. The next nodes always fill the next level from left-to-right.

  8. Heaps • Complete binary tree. The next nodes always fill the next level from left-to-right.

  9. Heaps • Complete binary tree. The next nodes always fill the next level from left-to-right.

  10. Heaps • Complete binary tree. The next nodes always fill the next level from left-to-right.

  11. Heaps • Complete binary tree.

  12. Heaps • A heap is a certain kind of complete binary tree. 45 35 23 27 21 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys.

  13. Heaps • A heap is a certain kind of complete binary tree. 45 35 23 27 21 22 4 19 The "heap property" requires that each node's key is >= the keys of its children

  14. What it is not: It is not a BST • In a binary search tree, the entries of the nodes can be compared with a strict weak ordering. Two rules are followed for every node n: • The entry in node n is NEVER less than an entry in its left subtree • The entry in the node n is less than every entry in its right subtree. • BST is not necessarily a complete tree

  15. What it is: Heap Definition • A heap is a binary tree where the entries of the nodes can be compared with the less than operator of a strict weak ordering. In addition, two rules are followed: • The entry contained by the node is NEVER less than the entries of the node’s children • The tree is a COMPLETE tree.

  16. Application : Priority Queues • A priority queue is a container class that allows entries to be retrieved according to some specific priority levels • The highest priority entry is removed first • If there are several entries with equally high priorities, then the priority queue’s implementation determines which will come out first (e.g. FIFO) • Heap is suitable for a priority queue

  17. The Priority Queue ADT with Heaps • The entry with the highest priority is always at the root node • Focus on two priority queue operations • adding a new entry • remove the entry with the highest priority • In both cases, we must ensure the tree structure remains to be a heap • we are going to work on a conceptual heap without worrying about the precise implementation

  18. Adding a Node to a Heap Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 35 23 27 21 22 4 19 42

  19. Adding a Node to a Heap Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 35 23 42 21 22 4 19 27

  20. Adding a Node to a Heap Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 42 23 35 21 22 4 19 27

  21. Adding a Node to a Heap The parent has a key that is >= new node, or The node reaches the root. The process of pushing the new node upward is called reheapificationupward. 45 42 23 35 21 22 4 19 27 Note: we need to easily go from child to parent as well as parent to child.

  22. Removing the Top of a Heap Move the last node onto the root. 45 42 23 35 21 22 4 19 27

  23. Removing the Top of a Heap Move the last node onto the root. 27 42 23 35 21 22 4 19

  24. Removing the Top of a Heap Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 27 42 23 35 21 22 4 19

  25. Removing the Top of a Heap Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 27 23 35 21 22 4 19

  26. Removing the Top of a Heap Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 35 23 27 21 22 4 19

  27. Removing the Top of a Heap The children all have keys <= the out-of-place node, or The node reaches the leaf. The process of pushing the new node downward is called reheapificationdownward. 42 35 23 27 21 22 4 19

  28. Priority Queues Revisited • A priority queue is a container class that allows entries to be retrieved according to some specific priority levels • The highest priority entry is removed first • If there are several entries with equally high priorities, then the priority queue’s implementation determines which will come out first (e.g. FIFO) • Heap is suitable for a priority queue

  29. Adding a Node: same priority Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 35 23 27 21 22 4 19 45*

  30. Adding a Node : same priority Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 35 23 45* 21 22 4 19 27

  31. Adding a Node : same priority Put the new node in the next available spot. Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 45 45* 23 35 21 22 4 19 27

More Related