CS1102 Tut 9 Priority Queues and Heaps - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS1102 Tut 9 Priority Queues and Heaps

Description:

Heap property: for every node v, the search key in v is greater to those in the children of v ... Hence we can also do rotation to maintain the property of heap! ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 29
Provided by: soc128
Category:

less

Transcript and Presenter's Notes

Title: CS1102 Tut 9 Priority Queues and Heaps


1
CS1102 Tut 9 Priority Queues and Heaps
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Question 1
  • Heap property for every node v, the search key
    in v is greater to those in the children of v
  • BST property for every node v, the search key in
    v is greater then those in its left child, and
    less then those in its right child

3
Question 1
  • Is a treap a heap?
  • A heap must be complete. A treap does not, so a
    treap is not a heap!

4
Question 1
  • How should you draw ?
  • You should realize that, to satisfy the heap
    property, you must pick the node with the maximum
    priority to be the root node
  • You can then divide the children into the left
    and right subtrees by their key values

5
Question 1
6
Question 1
  • How many possible treaps?
  • The root node must be unique because it has the
    be the max priority
  • The elements in the left and right subtree must
    also be unique (since they follow the key in the
    root)
  • The root of the left subtree must be max
    priority, same for the right subtree
  • Hence recursively, the tree must be unique!

7
Question 1
  • Efficient algorithm to insert a node
  • B.Tree insert from top
  • Heap insert below and bubble up
  • Treap Can we insert below ?
  • No!
  • So insert from the top!

8
Question 1
  • Suppose we insert a node at the root
  • If it has a higher priority then the root, then
    we know then it should replace the root, and the
    old root be inserted into the left or right
    subtrees
  • If it has a lower priority then the root, then we
    know that it should be inserted into the left or
    right subtrees
  • But what is wrong with this?
  • This might violate the b-tree property!

9
Question 1
  • Hence, we insert the node by just considering the
    key values
  • Recall rotation in AVL trees allow us to maintain
    the property of b-tree while balancing the height
  • Hence we can also do rotation to maintain the
    property of heap!
  • Algorithm is a O(logh) algorithm!

10
Question 2
  • updateKey(p, newVal)
  • When you change the nodes value in a heap, it
    should be bubbled up or bubbled down
  • If it has been increased, then its value could
    now be bigger then its parent, hence bubble up is
    needed
  • Similarly if it is decreased, then bubble down is
    needed

11
Question 2
  • updateKey(int p, Comparable newVal)
  • if(newVal gt arrayp) //newVal.compareTo(arrayp
    ) gt 0
  • arrayp newVal
  • bubbleUp(p)
  • else
  • arrayp newVal
  • bubbleDown(p)

12
Question 2
  • delete(p)
  • To delete a node, we replace the current node
    with the last node in the heap, then bubble
    up/down accordingly
  • We can just make use of the updateKey operation,
    just imagine that we are updating the nodes
    value to the last node in the heap!

13
Question 2
  • delete(int p)
  • Comparable newVal arraycurrentSize 1
  • currentSize--
  • updateKey(p, newVal)

14
Question 3
  • Much more complicated because of several arrays!
  • Algorithm is the same, only implementation is
    different

15
Question 3
  • Key, stores all the values in the key
  • Into, stores the index position of keyi
  • Outof, stores the heap indirectly by storing
    the index of value in key

16
Question 3
  • To solve this, break the problem into smaller
    problems
  • First step is to determine the position of the
    node where the update is needed
  • This allows us to determine the parent and
    children

17
Question 3
Change to 45
18
Question 3
  • To do that, we have to search for the oldValue in
    the key array such that
  • Keyi oldValue
  • Once we have determine i, we can use the into
    array to find its position
  • position intoi

19
Question 3
  • First find oldValue 25

Change to 110
20
Question 3
  • We can determine its position

Change to 110
21
Question 3
  • Update the value 25 to 110

Change to 110
110
22
Question 3
  • From what we have done in question 2, if the key
    was increased we need to bubble up
  • If key was decreased we need to bubble down

23
Question 3
  • Key has been increased, so bubbleup

Change to 110
110
24
Question 3
  • To get its parent, just take position - 1 divided
    by 2, (6 - 1)/ 2 2

Change to 110
110
25
Question 3
  • How to bubbleup or down ?
  • Need to swap values in into and outof arrays!

26
Question 3
  • To get its parent, just take position - 1 divided
    by 2, (6 - 1)/ 2 2

Change to 110
110
27
Question 4
28
Question 4
Write a Comment
User Comments (0)
About PowerShow.com