Definition of Tree

Tree: hierarchical access
mimic explanation:
Lesson 8 Tree - 图1

  • dir/subdir/subdir/file
    1. root


subtree subtree subtree
….. …. …..

  • Lesson 8 Tree - 图2#card=math&code=T%20%5Cequiv%20%28root%3B%20T1%2CT_2%2C…%2CT_n%29) recursive definition
    ![](https://g.yuque.com/gr/latex?T_1%20%5Cequiv%20(sub-root%3B%20T
    %7B11%7D%2CT%7B12%7D%2CT%7B13%7D%2C…%2CT%7B1m%7D)#card=math&code=T_1%20%5Cequiv%20%28sub-root%3B%20T%7B11%7D%2CT%7B12%7D%2CT%7B13%7D%2C…%2CT%7B1m%7D%29)
    ![](https://g.yuque.com/gr/latex?T
    %7Bterm%7D%20%5Cequiv%20(root%3B%20)#card=math&code=T_%7Bterm%7D%20%5Cequiv%20%28root%3B%20%29) termination condition

  • structure and naming

Lesson 8 Tree - 图3

Tree representation

Lesson 8 Tree - 图4

use vector as container

Lesson 8 Tree - 图5

use linked list as container

Lesson 8 Tree - 图6

  • two types of pointer: child and sibling
    detailed:
    Lesson 8 Tree - 图7
    This is called binary tree
    left-child right-sibling representation of general tree
func constructTree(data)
if data empty
    return null
else
    create root node from data[0]
    partition data to n sets
    for i = 1 to n
        T[i] = constructTree(i-th partition of data)
    link root to T[i]'s
return address of root node

How Many nodes?

  • relation between height and number of nodes
    Lesson 8 Tree - 图8

Lesson 8 Tree - 图9
Lesson 8 Tree - 图10-1%20%5Cleq%20h%20%5Cleq%20n-1#card=math&code=lg%28n%2B1%29-1%20%5Cleq%20h%20%5Cleq%20n-1)

We try to decrease the height of the tree —— make it more similar to tree than linked list

Full Binary Tree

Lesson 8 Tree - 图11
left: 0 operate: 2
right: 1
operate:2 + 1

We can use index to get the full binary tree’s node

front Lesson 8 Tree - 图12 nodes of a binary tree: complete binary tree

Expression Tree

Lesson 8 Tree - 图13
link the former infix evaluation to the tree