Binary sort Tree
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:6039
Resolution:2538
-
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:
-
51 6 5) 9 8
-
Sample output:
-
-
-
Tips:
-
There may be duplicate elements in the input, but the output of the two-tree traversal sequence repeats elements without output.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespaceStd;typedefstructnode{Node*l, *R; intv; Node () {L= NULL; R =NULL;}}*Tree, Node;tree build (tree p,intv) { if(p = =NULL) {P=NewNode (); P->v =v; returnp; } if(V < p->v) P->l = Build (p->L, v); Else if(V > p->v) P->r = Build (p->R, v); Else returnp; returnp;}voidpreorder (tree p) {if(p = = NULL)return; printf ("%d", p->v); Preorder (P-M); Preorder (P-r);}voidinorder (tree p) {if(p = = NULL)return; Inorder (P-l); printf ("%d", p->v); Inorder (P-r);}voidpostorder (tree p) {if(p = = NULL)return; Postorder (P-l); Postorder (P-R); printf ("%d", p->v);}intMain () {intN; while(~SCANF ("%d", &N)) {tree p; P=NULL; intv; for(inti =0; i < N; i++) {scanf ("%d", &v); P=Build (P, v); } preorder (P); Puts (""); Inorder (P); Puts (""); Postorder (P); Puts (""); } return 0;}
Binary sort tree (three-order output)