Source: http://www.cnblogs.com/coder2012/archive/2013/06/05/3102868.html
Tree
Tree structure is a kind of very important non-linear structure, it can well describe the objective world in a wide range of branches or hierarchical characteristics of the object, so in the computer field has a wide range of applications, such as operating system file management, compiler syntax structure and database system Information organization.
Tree-related definitions
- node degree : The number of sub-trees that a node contains is called the degree of the node;
- degree of the tree: the degree of the largest node in a tree is called the degree of the tree;
- leaf node or end node : A zero-degree node;
- non-terminal node or branch node : A node with a degree nonzero;
- parents or parent nodes: If a node has a child node, then it is called the parent node of its child node;
- child node or child node : The root node of a subtree containing a node is called a child node of that node;
- sibling nodes : nodes with the same parent node are called sibling nodes;
- The hierarchy of nodes: From the beginning of the root definition, the root is the 1th layer, the root of the child node is the 2nd layer, and so on;
- The height or depth of a tree: the maximum level of nodes in a tree;
- cousin Node : Parents in the same layer of the nodes are each other cousins;
- ancestor of a node : All nodes from the root to the branch of the node;
- descendants : Any node in a subtree that is rooted in a node is known as the descendant of that node.
- Forest : The collection of trees that are disjoint by M (m>=0) is called a forest;
Two fork Tree
Binary tree is an important type of tree structure. Many practical problems abstract data structure is often the form of two-tree, even the general tree can be easily converted to two-fork tree, and the binary tree storage structure and its algorithm is relatively simple, so the binary tree is particularly important.
A binary tree (BinaryTree) is a finite set of nodes of N (n≥0), which is either an empty (n=0), or a root node and two disjoint, left dial hand and right subtrees , each of which are called the root of the tree, and are composed of two forks.
Complete binary tree: For a binary tree, suppose its depth is D (d>1). In addition to layer D, the number of nodes in each layer has reached its maximum, and all nodes in layer D are continuously aligned from left to right, so that the two-tree is called a complete binary tree.
1. Two cross-tree traversal
The so-called traversal binary tree, is to follow a certain order, access to the binary tree all nodes, so that each node is only accessed once.
The traversal of a binary tree consists of three types:
- The DLR is called the previous root traversal (pre-sequence traversal)
- An operation that accesses a node occurs before it traverses its left and right subtrees.
- LDR is called the middle root traversal (middle sequence traversal)
- The operation of the access node occurs in the traversal of its left and right subtree (inter)
- Lrd called the post-root traversal (sequential traversal)
- An operation that accesses a node occurs after traversing its left and right subtrees.
Recursive implementations:
void preorder (NODE *p) {if (p!=null) {printf ("%d", P->data);p reorder (p->lchild);p reorder (p->rchild);}} void Inorder (Bintree t) { if (t) {//If the binary tree is not empty inorder (t->lchild); printf ("%c", t->data);//Access Node Inorder (T->rchild); }} Inordervoid Posorder (NODE *p) { if (p!=null) { posorder (p->lchild); Posorder (p->rchild); printf ("%d", p->data); } }
Non-recursive implementations:
/* Algorithm idea: Using the queue basic Operation 1. Initialize: root node into queue 2.while (queue non-empty) {A. First element out of Queue B. Original team first element corresponding left and right child (non-empty) into queue}*/void Traverse (node *t) { node *q[ ]; int Head,tail, I; q[0]=t;head=0;tail=1; while (Head<tail) { p=q[head++]; printf ("%c", t->data); if (p->lchild!=null) q[tail++]=p->lchild; if (p->rchild!=null) q[tail++]=p->rchild; }}
2. Construction of two-fork tree
Based on the first order traversal, the first sequence of the two-tree is listed as the input structure.
void Createbintree (Bintree *t) { char ch; if ((Ch=getchar ()) = = ") *t=null;//Read human space, place the corresponding pointer empty else{// Read people non-whitespace *t= (Bintnode *) malloc (sizeof (Bintnode));//Generate Nodes (*t)->data=ch; createbintree (& (*t) Lchild);//construct left subtree createbintree (& (*t)->rchild);//Construct Right sub-tree }}
Optimal binary Tree
In all two-fork trees of n leaves with a weight of wl,w2,...,wn, the two-tree with the least weight path length (i.e. the least cost) is called the optimal binary tree or Huffman tree .
Assuming there are n weights, the Huffman tree is constructed with n leaf nodes. N weights were set to W1, W2 、...、 wn, then Huffman tree
Constructing rulesFor:
- W1, W2 、..., wn as a forest with n trees (only one node per tree);
- In the forest, the tree with the lowest weights of two root nodes is selected as the left and right sub-tree of a new tree, and the root node weights of the new tree are the sum of its left and right subtree nodes.
- Remove the two selected trees from the forest and add the new tree to the forest;
- Repeat (2), (3), until there is only one tree left in the forest, the tree is the Huffman tree obtained.
Specific code:
#include "stdio.h" #include "stdlib.h" #define m-struct ptree//define binary tree node type {int w; Define node weights struct Ptree *lchild; Define left dial hand node pointer struct Ptree *rchild; Define right child node pointer}; struct pforest//Definition list node type {struct pforest *link;struct ptree *root;}; int wpl=0; Initialize WTL to 0struct ptree *hafm (), void Travel (), struct pforest *inforest (struct pforest *f,struct ptree *t); void Travel (struct ptree *head,int N) {//traverse struct Ptree *p;p=head;if (p!=null) for verifying the correctness of the HARFM algorithm) { if ((p->lchild) ==null && (p->rchild) ==null)//If it is a leaf node {printf ("%d", p->w); printf ("The hops of the node is:%d/n", N); wpl=wpl+n* (P->W); Calculate Weights}//iftravel (p->lchild,n+1); travel (p->rchild,n+1);} if}//travel struct Ptree *hafm (int n, int w[m]) {struct pforest *p1,*p2,*f;struct ptree *ti,*t,*t1,*t2;int i;f= (pforest * ) malloc (sizeof (pforest)); F->link=null;for (i=1;i<=n;i++)//generatedn a two-fork tree with only root nodes {ti= (ptree*) malloc (sizeof (Ptree));//Open up new node space ti->w=w[i]; Assigning a weight value to a node ti->lchild=null; ti->rchild=null; F=inforest (f, TI); The nodes are hung from top to bottom in the order of weights from small to large}//forwhile (((f->link)->link)!=null)//At least two binary trees {p1=f->link; p2=p1->link; f->link=p2->link; Take out the first two trees t1=p1->root; t2=p2->root; Free (p1); Release P1 free (p2); Release P2 t= (Ptree *) malloc (sizeof (Ptree));//Open new node space t->w = (t1->w) + (T2->W); Weighted addition t->lchild=t1; t->rchild=t2; Generation of new binary tree f=inforest (f,t); Each time you construct a binary tree, you have to rearrange the}//while p1=f->link; t=p1->root; Free (f); return (t); Return T} pforest *inforest (struct pforest *f,struct ptree *t) {//by weight from small to large in order to hang nodes from top to bottom in a tree struct pforest *p, *q, *r;str UCT ptree *ti;r= (pforest *) malloc (sizeof (pforest)); To open up new node space r->root=t;q=f;p=f->link; while (p!=null)//Find insertion position {Ti=p->root; if (T->w > Ti->w)//If the weight value of T is greater than the weight of ti {q=p; p=p->link; P Search backwards for}//if else p=null; Forced exit cycle}//whiler->link=q->link;q->link=r; R is followed by the return of Q (f); return f} void input (int &n,int w[m]) {printf ("Please input the sum of node/n");//Hint Input node scanf ("%d", &n); Input knot point printf ("Please input weight of every node/n"); Prompt to enter the weights for each node for (int i=1;i<=n;i++) scanf ("%d", &w[i]); Enter each node weight value} int main () {struct ptree *head;int n,w[m];input (n,w), HEAD=HAFM (n,w), Travel (head,0);p rintf ("The length of The best path is wpl=%d ", WPL);//output optimal path weight value and return 1;}
(turn) data structure: tree, binary tree, optimal binary tree