二元樹和AVL樹

二元樹創建和刪除(利用Inorder traversal找誰來繼承刪除的點)
AVL二元平衡樹
AVL Tree 模擬器

Properties of Binary tree

V: vertex
E: edge

\[ V=E+1 \]
proof
Using induction.
  1. Let V=1,
    Since E=0
    Hence V=E+1
  2. Suppose \( 1\leq k\leq n \rightarrow V=k, E=k-1 \) is right.
  3. To proof V=k+1 is right.
    E=(k-1)+1=k.
    \(V\) split to \( \{V_0, V_1, V_2,\dots, V_k \}\)
    \( V_0 \) is root.
    \( \{V_1, V_2,\dots,V_k \}\) is set of sum of vertex of subtree of \( V_0 \).
    \( E=E_1+E_2+\dots+E_k+k \)
    The k is number of connecting of \(\{V_1,V_2,\dots,V_k\}\)

    \( V_1 = E_1 +1 \)
    \( V_2 = E_2 +1 \)
    \( \dots \)
    \( V_k = E_k +1 \)

    \( V_1+ V_2+\dots+V_k \)
    \( = (E_1+1)+(E_2+1)+\dots+(E_k+1)=E_1+E_2+\dots+E_k+k \)
    \( \rightarrow V = V_0 +(V_1+V_2+\dots+V_k), \because V_0 \text{ is root} \therefore V_0= 1 \)
    \( \rightarrow V=(V_1+V_2+\dots+V_k)+1 \)
    \( \rightarrow V=(E_1+E_2+\dots+E_k+k)+1 \)
    \( \rightarrow V=E+1 \)
    \( \because E=k \)
    \( \therefore V=k+1 \)
    Q.E.D.

skew tree && complete binary tree


The nodes on level i are up to \[ 2^{i-1},i\geq 1 \]
The binary tree have k depth. The number of nodes are up to \[ 2^k-1, k\geq 1 \]
On binary tree.
Suppose number of degree 0 nodes is \( n_0 \),
number of degree 1 nodes is \( n_1 \),
number of degree 2 nodes is \( n_2 \),
number of all nodes is \( n \).
\[ n_0 = n_2+1 \] proof
\( n=n_0+n_1+n_2 \)
Let \( B \) is the number of edges. \( n=B+1 \)
\( \rightarrow B = n_1+2n_2 \)
\( \rightarrow n=n_1+2n_2+1 \)
\( \rightarrow n_0+n_1+n_2 = n_1+2n_2+1 \)
\( \therefore n_0=n_2+1 \)

Full binary tree

Fulled all nodes on binary tree.
Therefore, number of nodes are \( 2^k-1 , k\geq 0\)

depth of complete / full binary tree

\[ \lceil \log_{2}(n+1) \rceil \]

formal binary tree

Internal node is the node of tree and is not the leaf.
All Internal nodes have two child nodes that named formal binary tree.

put binary tree into array

Label of node is same as array index(omitted index 0).
Suppose the node i (\( i\neq 1 \))
Its label of parent is \( i/2 \).

Suppose the node i.
Its label of left child is \( 2i \).
\( 2i>n \rightarrow \not\exists \text{left child}\).

Suppose the node i.
Its label of right child is \( 2i+1 \).
\( 2i+1>n \rightarrow \not\exists \text{right child}\).

缺點

如果不是完備/完滿二元樹,會造成陣列空間的浪費。
節點的新增、刪除會搬動大部分的節點,造成cpu的負擔,使用 link list可以解決這個問題。

link list on binary tree

code(c)


If add a pointer to parent.
Modify like as:

code(c)



Order traversal

Suppose code:

code(c)


  • (LVR)inorder traversal / infix notation
    order: DBFEGAC
    c++小程式(no harm
  • (LRV)postorder traversal / postfix notation
    order: DFGEBCA
  • (VLR)preorder traversal / prefix notation
    order: ABDEFGC


The formulae on binary tree.
\[ [(A+B)*C]+[(E-F)/D] \]


Inorder / Infix (LVR)

Order:
A + B * C + [ (E - F) / D ]

code(c)

Postorder / postfix (LRV)

Order:
A B + C * E F - D / +

Preorder / prefix (VLR)

Order:
+ * + A B C / - E F D
還原技巧:
括號起來的地方當作一個變數。
postorder: 遇兩個變數再遇一個操作數就括號起來。
preorder: 遇一個操作數再遇兩個變數就括號起來。

Copy function

code(c)


Equal function

code(c)


threaded binary tree

針對樹葉節點的空指標可以有效利用。
使得有些運算能加快(走訪)。

code(c)


開頭空白節點的利用
一旦threaded binary tree 建立完成,infix order(LVR)的走訪就簡單多了
順序: G D H B A I E C F

解釋:
如果右引線為true的話,return 右子樹node的記憶體位址,
否則(執行 while)會找右子樹的最左樹葉,
並return 該樹葉的記憶體位址。

nextThreadedNode代開頭空白節點return G node的記憶體位址
nextThreadedNode代G node的記憶體位址 return D node的記憶體位址
以此類類推。

Infix order(中序走訪)
code(c):