ORIGIN

The last Internal Node of a Complete Binary Tree

Data Structrue 2 mins377 words

This problem derives from Heap Sort.

When you start to build the heap, you should start with the last internal node of the tree. And the index is usually $\frac{N}{2} - 1$, N is the total length of the array.

But why is that ?

Complete Binary Tree

First, you need to know that when representing a tree using array, the tree is a Complete Binary Tree. See the following picture.

complete-BT

The nodes in the array are stored like this: [A, B, C, D, E, F, G, H, I, J]

And the number of nodes $N$ can be represent as $2^{0}+2^{1}+\ldots +2^{k}+s$. ($k = depth - 1$, $s$ is the number of nodes in the deepest layer)

Proof

I’m proving this in a graphical layer.

First, let’s complete the tree to make it a Full Binary Tree.

graphviz (1)

As we can see, before completion, the last internal node is $E$.

Index of $E$ =$N$ - Red_Nodes - Deepest_Layer_Nodes

Let’s solve one by one.

  1. Deepest_Layer_Nodes($s$):

    $$
    N=2^{0}+2^{1}+\ldots +2^{k}+s \\
    \Rightarrow s=N-2^{0}-2^{1}- \cdots -2^{k} \
    $$
    $k + 1$ is the depth of the tree. s

  2. Red_Nodes( R )

    As seen from the graph, number of red nodes is relative with the gray nodes.

    Assume $F$ is number of nodes of Full Binary Tree, $D$ is the number of Gray Nodes.

    $$
    F=2^{0}+2^{1}+\ldots +2^{k+1}\\
    D=F-N
    $$

    From the previous graph we can see that when D is odd, $R = (D - 1) / 2$

    Now let’s see another graph, when the number of gray nodes is even.

    graphviz (2)

    When D is even, $R = D / 2$

    $\therefore R = floor(D / 2)$

  3. Then $E=2^{0}+\ldots +2^{k}-R$

    $\because R=floor\left( \dfrac{\left( 2^{0}+\ldots +2^{k+1}-N\right) }{2}\right) $

    $=floor\left( \dfrac{1}{2}+2^{0}+\ldots +2^{k}-\dfrac{N}{2}\right) =floor\left( \dfrac{1}{2}\right) +floor\left( 2^{0}+\ldots +2^{k}\right) -floor\left( \dfrac{N}{2}\right)$

    $=2^{0}+\ldots +2^{k}-floor\left(\dfrac{N}{2}\right)$

    $\therefore E=floor\left(\dfrac{N}{2}\right)$

  4. Because the index started from 0, so $E = floor\left(\dfrac{N}{2}\right) - 1$

TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|