intro to heap & adding and removing a node presented to: sir ahsan raza presented by: shah rukh...
TRANSCRIPT
INTRO TO HEAP & Adding and Removing a NODE
Presented to: Sir AHSAN RAZA
Presented by: SHAH RUKH Roll # 07-22Semester BIT 3rd
Session 2007—2011
Department of Computer Science,B.Z.University Multan
Heaps DefinitionHeaps DefinitionA heap is a certain kind of complete binary tree.
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.
HeapsHeapsA heap is a certain kind of complete binary tree.
When a completebinary tree is built,
its first node must bethe root.
When a completebinary tree is built,
its first node must bethe root.
Root
HeapsHeapsComplete binary tree.
Left childof theroot
The second node isalways the left child
of the root.
The second node isalways the left child
of the root.
HeapsHeapsComplete binary tree.
Right childof the
root
The third node isalways the right child
of the root.
The third node isalways the right child
of the root.
HeapsHeapsComplete binary tree.
The next nodesalways fill the next
level from left-to-right..
The next nodesalways fill the next
level from left-to-right..
HeapsHeapsComplete binary tree.
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
HeapsHeapsComplete binary tree.
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
HeapsHeapsComplete binary tree.
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
HeapsHeapsComplete binary tree.
HeapsHeapsA heap is a certain kind of complete binary tree.
Each node in a heapcontains a key thatcan be compared toother nodes' keys.
Each node in a heapcontains a key thatcan be compared toother nodes' keys.
19
4222127
23
45
35
HeapsHeapsA heap is a certain kind of complete binary tree.
The "heap property"requires that each
node's key is >= thekeys of its children
The "heap property"requires that each
node's key is >= thekeys of its children
19
4222127
23
45
35
What it is not: It is not a BSTIn 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 subtreeThe entry in the node n is less than every entry in its right
subtree.BST is not necessarily a complete tree
What it is: Heap DefinitionA 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 childrenThe tree is a COMPLETE tree.
Application : Priority QueuesA priority queue is a container class that allows entries to be
retrieved according to some specific priority levelsThe highest priority entry is removed firstIf 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
The Priority Queue ADT with HeapsThe entry with the highest priority is always at the root nodeFocus on two priority queue operations
adding a new entryremove the entry with the highest priority
In both cases, we must ensure the tree structure remains to be a heapwe are going to work on a conceptual heap without worrying about
the precise implementation
Adding a Node to a HeapAdding a Node to a HeapPut 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.
19
4222127
23
45
35
42
Adding a Node to a HeapAdding a Node to a HeapPut 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.
19
4222142
23
45
35
27
Adding a Node to a HeapAdding a Node to a HeapPut 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.
19
4222135
23
45
42
27
Adding a Node to a HeapAdding a Node to a HeapThe parent has a key that is
>= new node, orThe node reaches the root.The process of pushing the
new node upward is called reheapification upward.
19
4222135
23
45
42
27
Note: we need to easily go from child to parent as well as parent to child.
Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the
root.
19
4222135
23
45
42
27
Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the
root.
19
4222135
23
27
42
Removing the Top of a HeapRemoving the Top of a HeapMove 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.
19
4222135
23
27
42
Removing the Top of a HeapRemoving the Top of a HeapMove 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.
19
4222135
23
42
27
Removing the Top of a HeapRemoving the Top of a HeapMove 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.
19
4222127
23
42
35
Removing the Top of a HeapRemoving the Top of a HeapThe 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 reheapification downward.
19
4222127
23
42
35
Priority Queues RevisitedA priority queue is a container class that allows entries to be
retrieved according to some specific priority levelsThe highest priority entry is removed firstIf 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
Adding a Node: same priority 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.
19
4222127
23
45
35
45*
Adding a Node : same priority 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.
19
4222145*
23
45
35
27
Adding a Node : same priority 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.
19
4222135
23
45
45*
27