Build and traverse Binary Trees

Source: Internet
Author: User

Create a binary tree traversal rule:

1. First Order: Root-left-right

2. Central order: Left-root-Right

3. descending order: Left-right-Root


Binary Tree definition and auxiliary functions are as follows:

struct node {      int data;      struct node* left;      struct node* right;  };    void visit(int data)  {      printf("%d ", data);  }  int indata(){    int data;    scanf("%d",&data);    return data;}




Create a binary tree in sequence:

Struct node * createbitree () // first creates a binary tree {char X; // X is the root node struct node * t; X = indata (); if (x = '')/* determines whether the current subtree has been created */return NULL; else {T = (struct node *) malloc (sizeof (struct node )); t-> DATA = x; t-> left = createbitree (); t-> right = createbitree ();} return t ;}
Traverse Binary Trees in sequence:

void preOrder(struct node* root)  {      if (root == NULL)          return;      visit(root->data);      preOrder(root->left);      preOrder(root->right);  }  



Create a binary tree in the middle order:

Struct node * createbitree () // first creates a binary tree {char X; // X is the root node struct node * t; X = indata (); if (x = '')/* determines whether the current subtree has been created */return NULL; else {T = (struct node *) malloc (sizeof (struct node )); t-> left = createbitree (); t-> DATA = x; t-> right = createbitree ();} return t ;}
Traverse the binary tree in the middle order:

void inOrder(struct node* root)  {      if (root == NULL)          return;      inOrder(root->left);      visit(root->data);      inOrder(root->right);  }  




Create a binary tree in descending order

Struct node * createbitree () // first creates a binary tree {char X; // X is the root node struct node * t; X = indata (); if (x = '')/* determines whether the current subtree has been created */return NULL; else {T = (struct node *) malloc (sizeof (struct node )); t-> left = createbitree (); t-> right = createbitree (); t-> DATA = x;} return t ;}
Post-order traversal of Binary Trees

void inOrder(struct node* root)  {      if (root == NULL)          return;      inOrder(root->left);       inOrder(root->right);      visit(root->data); }  



For more information, see Xiao Liu.





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.