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 input1 2 2 8 4-5Sample output2 2 2 8 , 8, 8, 5, 5-ten 5
Analysis: the topic involves the establishment of Two-fork search tree, the first sequence traversal, the sequence traversal, the Post-order traversal. The establishment of binary search tree is simply extended on the basis of the establishment of the common binary tree. The first, middle, and post-sequential traversal here is the same as the normal binary tree.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#include<cmath>#include<queue>using namespacestd;structnode{intdata; Node* lchild,*rchild;};voidInsert (node * & root,intData) { if(root==NULL) {root=Newnode; Root->data=data; Root->lchild=NULL; Root->rchild=NULL; return ; } if(data==root->Data) { return ; } Else if(data<root->Data) {insert (root-lchild,data); } Else{insert (root-rchild,data); }}voidPreorder (node *Root) { if(root!=NULL) {cout<<root->data<<" "; Preorder (root-lchild); Preorder (root-rchild); }}voidInorder (node *Root) { if(root!=NULL) {inorder (root-lchild); cout<<root->data<<" "; Inorder (root-rchild); }}voidPostorder (node *Root) { if(root!=NULL) {postorder (root-lchild); Postorder (root-rchild); cout<<root->data<<" "; }}intmain () {intn; while(cin>>N) {node* root=NULL; for(intI=0; i<n;i++) { inta; CIN>>a; Insert (root,a); } Preorder (root); cout<<endl; Inorder (root); cout<<endl; Postorder (root); cout<<endl; }}
[binary search tree] Binary sort tree