+ All Categories
Home > Documents > 05a-HeapTree

05a-HeapTree

Date post: 04-Jun-2018
Category:
Upload: frankjamison
View: 216 times
Download: 0 times
Share this document with a friend

of 19

Transcript
  • 8/13/2019 05a-HeapTree

    1/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 1

    Heap

    Trees

  • 8/13/2019 05a-HeapTree

    2/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 2

    Heaps

    A particular kind of binary tree called min-Heap or just

    Heap has the following properties:

    The value of each node is less than the values stored in

    each of its children

    The tree is perfectly balanced and leaves in the last level

    are all in the leftmost position

    Similarly, in a max Heap tree the value of any node is

    greater than the values of its children. Therefore, the root

    has the highest value in the tree.

  • 8/13/2019 05a-HeapTree

    3/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 3

    An example of a Min-Heap

    A

    ED

    CB

    IH J

    GF

    A B C D E F G H I J

    0 1 2 3 4 5 6 7 8 9 10 11 12 13

    Note that for every element i, its left child is located at 2* iand its

    right child is located at 2*i+1 and its parent is at locationi/2

  • 8/13/2019 05a-HeapTree

    4/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 4

    Inserting into a Max-Heap Tree

    To add an element, the element is added at the end of the heap asthe last leaf

    Restoring the heap property in the case of inserting a new node is

    achieved by moving from the last node (the node that was just

    added) to the root. This is called percolating up the tree

    Thus to insert a new node, node P, to the heap

    Put Pat the end of the heap

    While Pis not the root and value of P > value of parent (p)Swap Pwith its parent

  • 8/13/2019 05a-HeapTree

    5/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 5

    Example: Insert 15 to the following heap tree

    20

    78

    1810

    52 6

    1413

    20

    78

    1810

    52 6

    1413

    15

    Insert 15 (call it node P): 15 is added as the last leaf. Now we

    need to swap P with its parent because Ps value is greater

    P

    20

    78

    1810

    52 6

    1413

    15P

  • 8/13/2019 05a-HeapTree

    6/19

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Nati onal Un iversity Page 6

    20

    158

    1810

    52 6

    1413

    7

    20

    108

    1815

    52 6

    1413

    7

    P is still greater than its parent. Thus we need to do another

    swap

    P

    P is not greater than its parent anymore. So we are done

    P

  • 8/13/2019 05a-HeapTree

    7/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 7

    Another way of looking at it (array form)

    Lets consider the Min-Heap this time

    To insert an element Pinto a heap, Create a hole in the next available location

    If Pcan be place in the hole without violating heap

    order, then we do so and we are done;

    Otherwise, we slide the element that is in the holes

    parent into the hole, thus bubbling the hole up toward

    the root

    We continue until Pcan be placed into the hole

  • 8/13/2019 05a-HeapTree

    8/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 8

    13

    3124

    1621

    2665 32

    6819

    13

    24

    1621

    2665 32

    6819

    31

    21

    13

    24

    16

    2665 32

    6819

    31

    21

    13

    24

    1614

    2665 32

    6819

    31

    Trying to insert 14 into the following heap

    14 < 21move parent to the hole

    14 > 13move 14 to the hole

    14 < 31move parent to the hole

  • 8/13/2019 05a-HeapTree

    9/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 9

    Insert Algorithm

    void BinaryHeap::insert(const int& x)

    {

    if (isfull())

    throw Overflow();

    // percolate up

    int hole = ++currentsize; // creating a new hole (position)

    for (; hole > 1 && x < array[hole/2]; hole=hole/2)

    array[hole] = array [hole/2]; // bubbling the hole up

    array[hole] = x; // Placing the value into the hole

    }

    Compares the value

    with its parent

  • 8/13/2019 05a-HeapTree

    10/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 10

    Algorithm for Deleting From a Max-Heap Tree

    Locate the node to be deleted and call it node P

    Locate the last node in the heap

    Swap the values of Pwith the last node

    Delete the last node

    While Pis not a leaf, and P< any of its children

    Swap P with the larger child

  • 8/13/2019 05a-HeapTree

    11/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 11

    Example of deleting an element from a max-heap tree

    20

    78

    1510

    52 6

    1413

    6

    78

    1510

    52 20

    1413

    We swap the last element with 20 first

    Suppose we wantto delete 20

  • 8/13/2019 05a-HeapTree

    12/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 12

    6

    78

    1510

    52 20

    1413

    Swap P with its larger child if the value of P is lower than the

    larger child

    Now we delete 20

    P

    6

    78

    1510

    52

    1413

    P

    15

    78

    610

    52

    1413

    P

  • 8/13/2019 05a-HeapTree

    13/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 13

    Swap P with its larger child if the value of P is lower than the

    larger child

    15

    78

    610

    52

    1413

    P

    15

    78

    1410

    52

    613P

  • 8/13/2019 05a-HeapTree

    14/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 14

    Other operations include:

    DescreaseKey(p, delta): Lowers the values of the item at position p by the positive

    amount delta This may violate the heap. We may need to percolate up to

    restore the heap in a min-heap

    IncreaseKey(p, delta): Increases the values of the item at position p by the positive

    amount delta This may violate the heap. We may need to percolate down to

    restore the heap in a min-heap

    Remove(p) To remove an item in position p, we can

    Apply Decreasekep(p, infinity) to move it up to the root Apply deleteMin to remove it from the min-heap

  • 8/13/2019 05a-HeapTree

    15/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 15

    13

    2119

    1614

    2665 32

    6819

    31

    Trying to delete 13 from the heap

    2119

    1614

    2665 32

    6819

    31

  • 8/13/2019 05a-HeapTree

    16/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 16

    14

    2119

    16

    2665 32

    6819

    14

    19

    21

    16

    2665 32

    6819

    Both children, 14 and 16 are smaller than 31we move 14 to the hole

    Both children, 19 and 21 are smaller than 31we move 19 to the hole

    31

    31

  • 8/13/2019 05a-HeapTree

    17/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 17

    14

    19

    26 21

    16

    65 32

    6819

    14

    19

    26 21

    16

    3165 32

    6819

    One child, 26, is smaller than 31we move 26 to the hole

    This is the final tree after 13 is deleted and heap is restored

    31

  • 8/13/2019 05a-HeapTree

    18/19Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal Un iversity Page 18

    deleteMin Algorithmvoid BinaryHeap::deleteMin (int& minItem)

    { if (isEmpthy())

    throw Underflow();minItem = array[1];

    array[1] = array [currentsize - -]; // moves the value of the last node into the first nodepercolateDown(1);

    }

    //---------------------------------------------------------------------------------------------------------------------

    void BinaryHeap ::precolateDown(int hole)

    {int child;

    int tmp = array[hole]; // stores the value of the last element in the heap into a temp variable

    for (; hole*2

  • 8/13/2019 05a-HeapTree

    19/19D Ah d R H d hA R Hadaegh Nati onal Un iversity Page 19

    Another way of looking at it (array form)

    Lets again consider the Min-Heap For the deleteMin operation

    Since the minimum is at the root, removing the minimum createsa hole at the root of the tree.

    Let Pbe the last element in the heap

    If Pcan be place in the hole without violating heap order, then we

    do so and we are done; (This is unlikely)

    Otherwise, compare the children of the hole and move the smaller

    one to the hole bubbling the hole down toward the bottom

    We continue until Pcan be placed into the hole


Recommended