-
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.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <string>5 #defineN 1096 7 structNode8 {9 intkey;Ten intLeft ; One intRight ; A }; - - intTree[n]; the Node Treenode[n]; - - voidPreintGen) { - if(Gen = =-1) { + return; - } +printf"%d", Treenode[gen].key); A Pre (treenode[gen].left); at Pre (treenode[gen].right); - - } - - voidMiddle (intGen) { - if(Gen = =-1) { in return; - } to Middle (treenode[gen].left); +printf"%d", Treenode[gen].key); - Middle (treenode[gen].right); the * } $ Panax Notoginseng voidHouintGen) { - if(Gen = =-1) { the return; + } A Hou (treenode[gen].left); the Hou (treenode[gen].right); +printf"%d", Treenode[gen].key); - } $ intMainintargcChar Const*argv[]) $ { - intN; - while(SCANF ("%d", &n)! =EOF) { the for(inti =0; I < n; i++) { -scanf"%d",&tree[i]);Wuyi } thetreenode[0].key = tree[0]; -treenode[0].left =-1; Wutreenode[0].right =-1; - for(inti =1; I < n; i++) { AboutTreenode[i].key =Tree[i]; $Treenode[i].left =-1; -Treenode[i].right =-1; - intLasttemp =-1; - inttemp =0; A BOOLDIR =false; + BOOLIsEqual =false; the while(Temp! =-1) { - if(Tree[i] <Treenode[temp].key) { $Lasttemp =temp; thetemp =Treenode[temp].left; theDIR =false; the } the Else if(Tree[i] >Treenode[temp].key) { -Lasttemp =temp; intemp =Treenode[temp].right; theDIR =true; the } About Else { theIsEqual =true; the Break; the } + } - if(isequal) { the Continue;Bayi } the if(dir = =false) { theTreenode[lasttemp].left =i; - } - Else { theTreenode[lasttemp].right =i; the } the the - } thePre0); theprintf"\ n"); theMiddle (0);94printf"\ n"); theHou0); theprintf"\ n"); the }98 return 0; About}
Nine degrees OJ Topic 1201: Two fork sort tree