C binary tree linked list operation---establishment, (recursive) pre-sequence traversal, middle sequence traversal, post-order traversal

Source: Internet
Author: User

"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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.