In this paper, we introduce the concept of tree, and then give an example of implementing source code for two-fork tree and multi-fork tree.
First, the concept of trees
The tree (in essence, recursive is defined, recursion is the stack application, so the tree can not be separated from the recursive and stack): The tree is a finite combination of n points. N=0 is an empty tree, n=1 and only one node is called the root, n>1, the rest of the nodes are divided into m disjoint subsets, each subset is a tree.
Forest
Two-fork Tree
Full two fork tree depth is k, the number of nodes is 2 K-square-1 of the two fork tree.
The full binary tree depth is K, the node is n, and only if the number of each node is exactly the same as the corresponding full two fork tree.
Sequential storage: Easy access
Chained storage: Easy to remove
Binary sort tree: The value of each node is greater than its left dial hand node, and is less than its right child node.
Traversal: The nonlinear structure is accessed through a linear table. Apply to different needs.
Typical of having a binary sort tree, and then the middle sequence traversal, it realizes the output from the small arrival.
Pre-sequence traversal: Prints first, then left recursion, and finally right recursion.
Middle sequence traversal: first left recursion, then print, and finally right recursion.
Post-post traversal: First left recursion, then right recursion, last print.
Hierarchical traversal: Using queues. Left and right into the queue. First, the left child out of the stack to print and then access its children, if there is left and right into the stack. Then right child out of the stack printing if there is a child, and so on ...
Two or two fork tree
#include "stdio.h" #include "stdlib.h" #include "string.h"//Structure two fork tree: Zuozi is not big root, right subtree is greater than root. int constructtree (int data[],int num,int tree[]) {tree[1] = data[1];for (int i=2;i<9;i++) {int J=1;while (tree[j]!=0) { if (Data[i]<=tree[j])//The next Level 2 K-square-1 (starting from 1) {j=j*2;} else{j=j*2+1;}} TREE[J] = Data[i];} for (int i=0;i<20;i++) {printf ("%d", Tree[i]);} printf ("\ n"); return 0;} Traversal binary tree: recursion//relative and root position, divided into the middle sequence traversal, the pre-sequence traversal, post-order traversal. After the post-traversal of void Ergodic1 (int data[],int pos) {//is not equal to 0 is present, then it is necessary to look for the left and right if (data[pos*2]!=0)//right-hand traversal {printf ("%d", data[pos*2]); Ergodic1 (data,pos*2);} if (data[pos*2+1]!=0)//right exists then traverse {printf ("%d", data[pos*2+1]); Ergodic1 (data,pos*2+1);}} Middle sequence traversal void ergodic2 (int data[],int pos) {///Not equal to 0 is present, then look for left and right if (data[pos*2]!=0)//left presence to traverse {Ergodic2 (data,pos*2);} if (data[pos]!=0) {printf ("%d", Data[pos]);} if (data[pos*2+1]!=0)//right exists then traverse {ergodic2 (data,pos*2+1);}} After the post-traversal of void ergodic3 (int data[],int pos) {//is not equal to 0 that is present, then it is necessary to look for the left and right if (data[pos*2]!=0)//to exist then traverse {ergodic3 (data,pos*2); printf ("%d", data[pos*2]);} if (data[pos*2+1]!=0)//right exists then traverse {ERGODIC3 (data,pos*2+1);p rintf ("%d", data[pos*2+1]);}} int main () {int data[100] = {0,6,3,9,2,5,7,8,4,2};int num = 9;int tree[100] = {0};constructtree (data,num,tree); Ergodic1 ( tree,0);p rintf ("\ n"), Ergodic2 (tree,0);p rintf ("\ n"), ERGODIC3 (tree,0);p rintf ("\ n");}
Three, multi-fork tree-looking for common ancestors
Find common ancestor # include "Stdio.h" #include "stdlib.h" #include "string.h" typedef struct ST_TREE tree;struct st_tree {int deep; Char name[200];int nextnum;int next[200]; TREE *father;}; void Tree_init (tree *tree) {tree->deep = 0;//head node memset (tree->name,0,200); tree->nextnum = 0;memset (tree-> next,0,200); tree->father = NULL;} void Tree_insert (tree *tree0,tree *tree) {tree->father = Tree0;tree->deep = tree0->deep+1;tree0->next[ tree0->nextnum++]= (int) tree;} Tree *tree_getchild (Tree *tree0,int No)//{//return (tree *) tree0->next[no];//}//Separate head node tree header; Tree *getchild (Tree *father,char *name) {Tree *current = father;if (strcmp (Name, "") ==0) {return current;} It is particularly important to note that the values of different layer variables cannot be confused during recursion. No matter how the current changes, each cycle comes from the original, that is, later fint num = current->nextnum; TREE *f = current;for (int i=0;i<num;i++) {current = (TREE *) f->next[i];if (strcmp (name,current->name) ==0) { return current;} Else{tree *c = Getchild (Current,name), if (c) {return c;}}} return NULL;} void Insertpc (char*fathername,char *sonname) {Tree *f = Getchild (&header,fathername), if (f) {Tree *son = (tree *) malloc (sizeof (tree)); Tree_init (son); strcat (son->name,sonname); Tree_insert (F,son);} Else{insertpc ("", fathername);//Insert Head node INSERTPC (fathername,sonname);}} Find common ancestors: Compare depth tree *getancestor (char *a,char *b) {TREE *atree = Getchild (&header,a); TREE *btree = Getchild (&header,b), while (true) {if (Atree->father==null | | btree->father==null) {break;} if (strcmp (Atree->name, "") ==0 | | strcmp (atree->name, "") ==0)//{//break;//}if (atree->deep>btree-> Deep) {atree = Atree->father;} else if (btree->deep>atree->deep) {bTree = Btree->father;} Else{if (strcmp (atree->father->name,btree->father->name)!=0) {atree = Atree->father;btree = bTree- >father;} Else{return Atree->father;}}} return NULL;} int main (void) {Tree_init (&header), Insertpc ("A", "B"), Insertpc ("B", "D"), Insertpc ("A", "C"), Insertpc ("C", "E"); INSERTPC ("E", "F");//TREE *t = Getchild (&header, "F");//printf ("%s", t->name); TREE *ancestor = GetAncestor ("D", "F");p rintf ("%s", ancestor->name); return 0;}
Tree two fork Tree multi-fork Tree