{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Cpsc 223 fall 2010 3 1 6 4 this is a not a heap why

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: er of variants however •  Unlike a binary search tree, a heap is always a complete tree! A min heap is just the opposite •  How does it do this? –  By relaxing the ordering constraint –  … children of nodes have smaller values (max-heap) –  thus, the root always has the max value CPSC 223  ­ ­ Fall 2010 21 The Heap ADT A (binary) “heap” is a complete binary tree such that: –  Every parent node has a larger search key value than its children (for max-heap) –  Trivially, an empty tree is a heap 10 15 9 5 8 2 4 This is a heap 8 6 7 8 2 4 This is a not a heap … why? CPSC 223  ­ ­ Fall 2010 3 1 6 4 This is a not a heap … why? 22 11 10/21/10 The Heap ADT The basic Heap ADT operations: // get the item with largest key in heap (alt findMin) Entry findMax(); // delete the item with the largest key in the heap void deleteMax(); // insert item into the heap void insert(const Entry& newItem); CPSC 223  ­ ­ Fall 2010 23 Array-Based Heap Implementation Because a heap is always complete we can “efficiently” represent a heap using an array –  If node n is stored at index i –  The children of n are stored at 2i+1 (left) and 2i+2 (right) –  The parent of n is at position Ⱥ(i–1)/2Ⱥ 10 we may have empty slots at the end … 9 8 10 9 5 2 4 8 5 2 4 0 2 3 4 5 1 CPSC 223  ­ ­ Fall 2010 6 24 12 10/21/10 Array-Based Heap Implementation •  We assume that Heap has data members: Entry items[MAXITEMS]; // array of heap items int size;...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern