Understand the maximum number of elements in a subtree happens for the left subtree of a tree that has the last level half full.Draw this on a piece of paper to realize this.
一旦明確,勢必2N/3的是輕鬆搞定。
讓我們假設樹中的節點總數爲N.
節點的樹數= 1 +(在左子樹節點的數量)+(在右子樹的節點數)
對於我們的情況下樹有最後一級半滿的,如果我們假設右子樹的高度h,然後左子樹,如果高度(H + 1):
數在左子樹中的節點= 1 + 2 + 4 + 8 ... 2 ^(h + 1)= 2 ^(h + 2)-1 .....(i)
在右子樹= 1 + 2 + 4 + 8 .... 2 ^(H)2 ^(H + 1)-1 .....(II)
因此節點=數,插入到:
樹中的節點的總數= 1 +(在左子樹的節點的數量)+(在右子樹的節點的數量)
=> N = 1 + (2^(h+2)-1) + (2^(h+1)-1)
=> N = 1 + 3*(2^(h+1)) - 2
=> N = 3*(2^(h+1)) -1
=> 2^(h+1) = (N + 1)/3
堵在這個值代入式(i)中,我們得到:
Number of nodes in Left Subtree = 2^(h+2)-1 = 2*(N+1)/3 -1 =(2N-1)/3 < (2N/3)
因此上的節點中的子樹的最大數目的上界具有N樹節點是2N/3。在節點的
這個博客提供了一個簡單的計算:http://bit.ly/138f43F。 – akaHuman 2013-06-26 12:28:39