"Binary Tree linked list"
1. Node Definition:
typedef struct node{
int data;
struct Node*lchild,*rchild;
}tree,*bitree;
2. Create a two-fork tree:
Bitree creat_tree (bitree root,int num) {//Build two fork Tree
if (root==null)
{
root= (tree) malloc (sizeof);
if (root==null)
{
printf ("No Memory available\n");
return NULL;
}
root->data=num;
root->lchild=null;
root->rchild=null;
}
Else
{
if (num < root->data)
Root->lchild=creat_tree (Root->lchild,num);
Else
Root->rchild=creat_tree (Root->rchild,num);
}
return root;
}
The following is the traversal of the two-fork tree, because the beginner only writes recursive traversal, has not studied the non-recursive traversal
3. First-order traversal of the binary tree, the order of traversal, root, left, right
void Pre_order (Bitree root) {
if (root==null)
Return
printf ("%d\t", root->data);
Pre_order (Root->rchild);
Pre_order (Root->lchild);
}
4. Middle sequence Traversal binary tree, the traversal order is, left, middle, and right
void In_order (Bitree root) {
if (root==null)
return;
In_order (Root->rchild);
printf ("%d\t", root->data);
In_order (Root->lchild);
}
5. Post-order traversal of the binary tree, the traversal sequence is, left, right, 8
void Post_order (Bitree root) {
if (root==null)
return;
Post_order (Root->rchild);
Post_order (Root->lchild);
printf ("%d\t", root->data);
}
6. Function Body part
int main ()
{
int n,num;
Bitree Root=null;
printf ("Imput the total number of tree:\n");
scanf ("%d", &n);
while (n--) {
printf ("Please input the num of insert:\n");
scanf ("%d", &num);
Root=creat_tree (Root,num);
}
printf ("Please choice the traversal mean:\n1--pre_order\n2--in_order\n3--post_order\n");
int choice;
scanf ("%d", &choice);
Switch (choice) {
Case 1:
Pre_order (root);
Break
Case 2:
In_order (root);
Break
Case 3:
Post_order (root);
Break
}
return 0;
}
C binary tree linked list operation---establishment, (recursive) pre-sequence traversal, middle sequence traversal, post-order traversal