## 1, Storage structure of tree

### (1) Basic concepts

Tree is a nonlinear data structure. It is a set with hierarchical relationship composed of n (n > = 0) finite nodes. It is called a tree because it looks like an upside down tree, that is, it has roots up and leaves down.

Node: each data element stored in a tree structure is called a "node".

Degree of node: for a node, the number of subtrees (how many branches the node has) is called the degree of the node.

Degree of tree: the depth (height) of a tree is the largest level of nodes in the tree.

Node hierarchy: defined from the root, the root is the first layer, and the child nodes of the root are the second layer, and so on.

Height or depth of the tree: the maximum level of nodes in the tree.

## 2, Binary tree

### (1) Basic concepts

#### 1. Full binary tree

If the degree of each node in a binary tree is 2 except the leaf node, the binary tree is called a full binary tree.

In addition to satisfying the properties of ordinary binary trees, full binary trees also have the following properties:

- The number of nodes in layer i of the full binary tree is 2^(n-1).
- A full binary tree with depth K must have 2^k-1 nodes and 2^(k-1) leaves.
- There is no node with degree 1 in the full binary tree. There are two subtrees with the same depth in each branch point, and the leaf nodes are at the bottom.
- The depth of a full binary tree with n nodes is log2(n+1).

#### 4. Complete binary tree

In a full binary tree, starting from the last node and continuously removing any node, it is a complete binary tree.

The depth of a complete binary tree with n nodes is ⌊ log2n ⌋ + 1.

⌊log2n⌋ Indicates less than log2n The maximum integer of. For example,⌊log24⌋ = 2，and ⌊log25⌋ The result is also 2.

In a binary tree, if the number of leaves is n0 and the number of nodes with degree 2 is n2, then n0=n2+1

### (2) Storage structure of binary tree

Binary tree can generally use two storage structures, a sequential structure and a chain structure.

#### 1. Sequential storage

Sequential structure storage is to use arrays for storage. Generally, arrays are only suitable for representing complete binary trees, because there will be a waste of space if they are not complete binary trees.

#### 2. Chain storage

The chain storage structure of binary tree refers to that a binary tree is represented by a linked list, that is, a chain is used to indicate the logical relationship of elements. The usual method is that each node in the linked list is composed of three fields, data field and left and right pointer field. The left and right pointers are used to give the storage address of the chain node where the left child and right child of the node are located respectively.

``// Binary chain` `struct BinaryTreeNode` `{` `struct BinTreeNode* _pLeft; // Point to the left child of the current node` `struct BinTreeNode* _pRight; // Point to the right child of the current node` `BTDataType _data; // Current node value range` `}` `// Trigeminal chain` `struct BinaryTreeNode` `{` `struct BinTreeNode* _pParent; // Points to the parent of the current node` `struct BinTreeNode* _pLeft; // Point to the left child of the current node` `struct BinTreeNode* _pRight; // Point to the right child of the current node` `BTDataType _data; // Current node value range` }；`

### (3) Four traversals of binary tree

#### 1. Preorder traversal

recursion

`Status PreOrderTraverse(BiTree T){` `if(T==NULL) return OK;//Empty binary tree` `else{` `visit(T);//Access root node` `PreOrderTraverse(T->lchild);//Recursive traversal of left subtree` `PreOrderTraverse(T->rchild);//Recursive traversal of right subsets` `}` `}`

non-recursive

void preorder_dev(bintree t){ seqstack s; s.top = -1; //Because top represents the position in the array here, null is - 1 if(!t){ printf("the tree is empty\n"); }else{ while(t || s.stop != -1){ while(t){ //As long as the node is not empty, it should be saved on the stack, regardless of its left and right nodes printf("%c ",t->data); push(&s,t); t= t->lchild; } t=pop(&s); t=t->rchild; } } }

#### 2. Medium order traversal

recursion

`Status PreOrderTraverse(BiTree T){` `if(T==NULL) return OK;//Empty binary tree` `else{` `PreOrderTraverse(T->lchild);//Recursive traversal of left subtree` `visit(T);//Access root node` `PreOrderTraverse(T->rchild);//Recursive traversal of right subsets` `}` `}`

non-recursive

void midorder(bintree t){ seqstack s; s.top = -1; if(!t){ printf("the tree is empty!\n"); }else{ while(t ||s.top != -1){ while(t){ push(&s,t); t= t->lchild; } t=pop(&s); printf("%c ",t->data); t=t->rchild; } } }

#### 3. Postorder traversal

recursion

Status PreOrderTraverse(BiTree T){ if(T==NULL) return OK;//Empty binary tree else{ PreOrderTraverse(T->lchild);//Recursive traversal of left subtree PreOrderTraverse(T->rchild);//Recursive traversal of right subsets visit(T);//Access root node } }

non-recursive

#### 4. Hierarchical traversal

Queue type definition:

typedef struct{

BTNode data[MaxSize]; // Store elements in the queue

int front,rear;// Head and tail pointer

}SqQueue;// Type of sequential loop queue

Algorithm:

void LevelOrder(BTNode *b) {

BTNode *p;SqQueue *qu;

InitQueue(qu); // Initialize queue

enQueue(qu, b); // The root node pointer enters the queue < / u >

while (!QueueEmpty(qu)) {/ / if the queue is not empty, the loop < / u >

deOueue(gu, p); / / outgoing header element node P < / u >

Printf ("% C", - P - > data); / / access node P < / u >

if (p-> lchild!=NULL) enQueue(qu, p-> lchild);</u>

//Join the left child when there is one < / u >

if (p->rchild!=NULL) enQueue(qu, p-> rchild);}</u>

//When you have a right child, join the team < / u >