/
Nearly Complete Binary Trees and Heaps EFINITIONS i The depth of a node in a binary tree Nearly Complete Binary Trees and Heaps EFINITIONS i The depth of a node in a binary tree

Nearly Complete Binary Trees and Heaps EFINITIONS i The depth of a node in a binary tree - PDF document

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
549 views
Uploaded On 2014-12-01

Nearly Complete Binary Trees and Heaps EFINITIONS i The depth of a node in a binary tree - PPT Presentation

ii The height or depth of a binary tree is the maxi mum depth of any node or 1 if the tree is empty Any binary tree can have at most 2 nodes at depth Easy proof by induction EFINITION A complete binary tree of height is a binary tree which contain ID: 19553

The height

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Nearly Complete Binary Trees and Heaps E..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Examples: Not nearly complete: ( a ) Not nearly complete: ( b ) fails. Nearly complete. 1 2 3 4 5 6 7 8 9 1011121314151617 18 19202122J Q D B T Z G LCUANFRHVI E WKMY , and 5/25+1 == 11, and we find 4567891012131115 42 1 2 3 4 5 6 7 8 9 1011121314151617 18 19202122 81 Heap condition at node p 1)/2 pr In general, the worst-case number of comparisons to lg(maxlg( lg(max(lg( represented in an array A of size at least n+ ( A, n = n + 1; A[ n�] A[ ¬n/2¼] ) n], A[ ¬n/2¼] ); Find the largest element Given a nearly complete binary tree, in which the heap property can fail only at the children of the root, we can make the tree into a heap using a procedure called max-heapify() Among the root and its two children (nodes qwe find the largest element. (Two comparisons In this case, the largest (72) occurs in node r If the largest of these three elements were to occur in the root (not the case here), we would be done. If the largest occurs in a child of the root (as happens here, node r), we exchange the element in the root with the element in this child. 29 heap propertycould fail here q rs With the array representation, we can write max-heapify() like this. // A is an array of size at least n, which we think of // as a nearly complete binary tree. In the subtree // of A[1..n] rooted at A[i] , the heap property // holds everywhere except possibly at the children // of A[i]. This function makes the subtree of // A[1..n] rooted at A[i] into a heap. ( A, = i A[2 largest = 2 +1 A[2+1]� A[ +1; i ) i], A[largest]) ( A, We can also write max-heapify() non-recursively like this: However, we can not , decide if the heap contains an element x , find the k largest element in the heap (unless is 1, or at least is very close to 1). , remove x from the heap, if it is present.