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.