Show that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by $$\lfloor n/2 \rfloor + 1, \lfloor n/2 \rfloor + 2. . . . .n.$$ since 2i and 2i+1 are the children for i th node. for the leaf this will not exist. The no of non leaf nodes are at most $$\lfloor n/2 \rfloor $$ since if i = n/2 if we calculate right child $$2i+1 = 2*(n/2)+1 = n +1 $$ It would outside of the boundary of heap (since we assumed we have n elements). hence the no of non leaf nodes should be at most $$\lfloor n/2 \rfloor $$ Since we index array from 1 to n and we have at most $\lfloor n/2 \rfloor $ non leaf nodes. The leaf nodes are $n/2+1,n/2+2.....n$
script
Thursday, January 10, 2013
6.1-7
Show that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by $$\lfloor n/2 \rfloor + 1, \lfloor n/2 \rfloor + 2. . . . .n.$$ since 2i and 2i+1 are the children for i th node. for the leaf this will not exist. The no of non leaf nodes are at most $$\lfloor n/2 \rfloor $$ since if i = n/2 if we calculate right child $$2i+1 = 2*(n/2)+1 = n +1 $$ It would outside of the boundary of heap (since we assumed we have n elements). hence the no of non leaf nodes should be at most $$\lfloor n/2 \rfloor $$ Since we index array from 1 to n and we have at most $\lfloor n/2 \rfloor $ non leaf nodes. The leaf nodes are $n/2+1,n/2+2.....n$
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment