Enter a series of integers, set up a two-fork sorting tree, and perform first, middle, and post-order traversal
Code:
1#include <stdio.h>2#include <string.h>3 structnode{4Node *lchild,*Rchild;5 intC;6}tree[ the];7 intLoc;8Node *Create () {9Tree[loc].lchild=tree[loc].rchild=NULL;Ten return&tree[loc++]; One } A voidPostorder (Node *t) {//Post-post traversal - if(t->lchild!=NULL) { -Postorder (t->lchild); the } - if(t->rchild!=NULL) { -Postorder (t->rchild); - } +printf"%d",t->c); - } + voidInorder (Node *t) {//Middle Sequence Traversal A if(t->lchild!=NULL) { atPostorder (t->lchild); - } -printf"%d",t->c); - if(t->rchild!=NULL) { -Postorder (t->rchild); - } in } - voidPreorder (Node *t) {//First Order Traversal toprintf"%d",t->c); + if(t->lchild!=NULL) { -Postorder (t->lchild); the } * if(t->rchild!=NULL) { $Postorder (t->rchild);Panax Notoginseng } - the } +Node *insert (node *t,intx) { A if(t==NULL) { thet=Create (); +T->c=x; - returnT; $ } $ Else if(x<t->c) { -T->lchild=insert (t->lchild,x); - } the Else if(x>t->c) { -T->rchild=insert (t->rchild,x);Wuyi } the returnT; - } Wu intMain () { - intN; About while(SCANF ("%d", &n)! =EOF) { $Loc=0; -Node *t=NULL; - for(intI=0; i<n;i++){ - intx; Ascanf"%d",&x); +t=Insert (t,x); the } - preorder (T); $printf"\ n"); the inorder (T); theprintf"\ n"); the Postorder (T); theprintf"\ n"); - } in return 0; the}
Binary sort Tree