The linear structure, the tree structure, the graph structure and the pure set structure are the four kinds of data structure, and the tree structure is a kind of important nonlinear structure.
Take the two-fork tree as an example to understand the basic nature and usage of the tree structure:
Two-fork Tree:
Each node has a maximum of two subtrees trees, Saozi right subtree, the order can not be reversed
The nth layer of a non-empty binary tree has a maximum of 2^ (n-1) nodes.
A two-tree with a depth of H has a maximum of 2^h-1 nodes.
Perfect binary tree (full two-fork tree) The number of nodes is 2^h-1.
The nodes of the H-layer of the complete binary tree are concentrated on the left.
Perfect binary tree itself is also a complete binary tree
For a complete binary tree, set a node (non-root node) number I, then its Father node number is I/2, its left Dial hand node number is 2 * I, right sub-node number 2 * i + 1
Number of nodes with leaf nodes equal to 2 points + 1
Similarly:
M fork Tree leaf node number n0, there is a son of the number of nodes N1, there are two sons of the number of nodes N2, there are three sons of the number of nodes N3 ... What is the relationship between the knots of a m son and a nm?
N0 + n1 + n2 + ... nm-1 = 0 * n0 + 1 * n1 + 2 * n2 + 3 * n3 + 4 * N4 + ... m * NM
So: N0-1 = n2 + 2 * n3 + 3 * N4 + ... + (m-1) * NM
Storage structure:
Sequential storage:
A complete binary tree can be implemented with an array, generally binary tree if this storage method is used, the space cost is higher
The array implements the sequential storage code for the two-tree:
struct Node { int data; int lchild, rchild; int father;} Node; Node btree[];
Chain-Store:
struct Node { int data; struct Node * lchild; struct Node **New_node () { new node; NewNode0 ; NewNode->lchild = NULL; NewNode->rchild = NULL; return NewNode;}
Traversal of a binary tree:
Traversal of all nodes of a binary tree is accessed once, according to the order in which the root nodes are accessed: first-order traversal, middle-order traversal, and subsequent traversal:
First Order Traversal: root node--left dial hand tree, right subtree
Middle Sequence traversal: the right subtree, the root node, left dial hand tree
Post-order Traversal: root node, right subtree, left dial hand tree
First Order Traversal: 1, 2, 4, 5, 7--3--6
Mid-sequence Traversal: 1, 3, 8, 7, 2, 5, 4
Post-Traversal: 4--7, 5, 2, 8, 6--3
Recursive implementation of traversal:
#include <iostream>#include<stdio.h>using namespacestd; typedefstructNode {intdata; structNode *Lchild; structNode *Rchild;} Node; Node*New_node () {Node* NewNode =NewNode; NewNode->data =0 ; NewNode->lchild =NULL; NewNode->rchild =NULL; returnNewNode;} typedef Node*node; Node*Createbintree () {intA; Node*T; CIN>>A; if(A = =0) returnNULL; Else{T=New_node (); T->data =A; T->lchild =Createbintree (); T->rchild =Createbintree (); } returnT;}voidPreorder (node T) {if(T) {if(T->data! =0) cout<< T->data <<" " ; Preorder (T-lchild); Preorder (T-rchild); }}intMain () {Freopen ("In.txt","R", stdin); Node* Root =Createbintree (); Preorder (root); return 0 ;}//input data://1 2 4 0 0 5 0 7 0 0 3 0 6 8 0 0 0
Tree-shaped structure