Title Description:Enter a series of integers, establish a two-fork sort number, and carry out pre-order, middle-order, post-sequence traversal.Input:Enter the first line to include an integer n (1<=n<=100). The next line consists of n integers.Output:There may be more than one set of test data, for each set of data, the problem of the data to create a binary sorting tree, and the two-fork sorting tree for the pre-order, middle order and post-sequence traversal. Each of the traversal results outputs a row. There is a space after the last data in each row.Sample Input:5 1 6 5 9 8Sample output:1 6 5 9 8 1 5 6 8 9 5 8 9 6 1Tips:There may be duplicate elements in the input, but the output of the two-tree traversal sequence repeats elements without output. #include <iostream> #include <stdio.h> #include <string.h>using namespace std; struct Node {node *lchild;node *rchild;int c;} TREE[110];int loc;node* creat () {Tree[loc].lchild = Tree[loc].rchild = NULL;loc++;return &tree[loc-1];}void Post (node *t) {if (T->lchild) {post (t->lchild);} if (T->rchild) {post (t->rchild);} printf ("%d", t->c);} void Mid (node *t) {if (T->lchild) {mid (t->lchild);} printf ("%d", t->c), if (T->rchild) {mid (T->rchild);}} void Pre (node *t) {printf ("%d", t->c), if (T->lchild) {pre (t->lchild);} if (t->rchild) {pre (t->rchild);}}node * INSERT (node *t, int x) { if (!t) { t=creat (); T->c=x;return T;} else if (t->c>x) {T->lchild=insert (t->lchild,x);} else if (t->c<x) {T->rchild=insert (t->rchild,x);} return t;} int main () {int N;while (cin>>n) {loc=0;int x; node *t=null; while (n--) {cin>>x; T=insert (t,x),//insert function has a return value, be sure to write T=intsert (), do not write only insert. }pre (t); Cout<<endl;mid (t); Cout<<endl;post (t); Cout<<endl;} return 0;} node *t=null; Just a null pointer was created, not a node was created. Create a node in the Insert function, reference the creat function. The principle of binary tree establishment: a 110-size node array is established, and then the creat function simply returns a pointer to each array element, forming a tree. LOC should be defined as a global variable, and LOC should be used for the creat function. And not just the main function, with Loc.
Binary sort Tree