1, definition: Tree is a nonlinear structure, is a one-to-many data structure.
The structure of the analysis tree, we use recursive method, the root node below can be considered as a subtree.
2, the storage structure of the tree:
We usually store it in the child's brother law. That is, the child on the left of the left side of the node at the end of the node, and the right sibling of the knot is placed on the right side of the knot.
This creates a two-fork tree.
Binary trees and trees can correspond to each other.
3. Binary tree and its properties
In short, the binary tree has many special properties, the direct study of the tree will be a bit troublesome, so we study the tree by studying the binary tree.
Many of the features of a binary tree can help us solve many problems, such as finding problems and sorting problems.
4, Binary tree storage
Binary tree storage is generally used in the form of a linked list, that is, a two-linked list.
5. Traverse the binary tree
Pre-sequence traversal, middle sequence traversal, post-order traversal
6, the establishment of two-fork tree
As an example, a two-fork tree is established by the previous sequential traversal method. The core is a recursive approach.
#include <stdlib.h> #include "conio.h" #include <stdio.h>//define the nodes of the tree typedef struct treenode{ Char data; struct TreeNode * lchild; struct TreeNode * rchild;} Treenode,*bitree; Bitree Createtree (Bitree T) { char ch; scanf ("%c", &ch); if (ch = = ' # ') T = NULL; else { T = (TreeNode *) malloc (sizeof (TreeNode)); T->data = ch; T->lchild=createtree (t->lchild); T->rchild=createtree (T->rchild); } return T;} void Pretree (Bitree t) { if (t) { printf ("%c", t->data); Pretree (t->lchild); Pretree (T->rchild); }} void Main () { bitree T; t = Createtree (t); Pretree (T);}
7, tree, binary tree, forest mutual transformation
(1) Tree---"Two fork tree: left the first child on the left child, right brother on the right child
(2) Forest--"Two fork tree: convert each tree to two fork tree, then take the root node of the latter tree as the last tree right child
(3) Binary tree-"tree"
8, Heffman
Data structure--tree