java - Is this a valid MaxHeap structure? -
i trying convert minheap class maxheap class. given code minheap class, 1 method of add. looks this:
while (index > 1 && getparent(index).compareto(newelement) > 0)
the first node automatically set null, gets add placed in node 1 onwards. code stated gives minheap structure. change maxheap, flipped comparator sign so:
while (index > 1 && getparent(index).compareto(newelement) < 0)
the items entering being stored integer value. in order of insertion are:
3 7 8 10 6 1 9 2
in minheap structure, these stored in nodes so:
1 2 3 6 7 8 9 10
in maxheap structure, changing sign stores them so:
10 8 9 2 7 3 6 1
note how no longer in same kind of sequential order in minheap structure. matter or still valid maxheap?
apologies awful attempt @ showing tree structure.
yes. if heap being stored valid max-heap. max-heap have property every node (except root) contain value less or equal parent. heap obey rule.
Comments
Post a Comment