Binary sort Tree Time limit:1000ms Memory limit:65536k Title Description
The binary sort tree is defined as either an empty tree or a two-fork tree with the following properties: If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node, and if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node, and its left and right subtrees are also two-fork sorting trees. Today we are going to judge whether the two sequences are the same one or two-fork sort tree
The input begins with a number n, (1<=n<=20) indicates that there are n needs to be judged, and the input ends when n= 0. The next line is a sequence, the sequence length is less than 10, contains (0~9) number, there is no repetition number, according to this sequence can construct a binary sorting tree. The next n rows have n sequences, and each sequence format is the same as the first sequence, so please determine whether the two sequences can form the same binary sort tree. (Data guarantee does not have empty tree) output sample input
21234567899876543214321567890
Sample output
YESNO
The sequence of ordered binary trees is: two binary tree sequence!
You only need to set up the two-fork sort tree for the first and post-order traversal, to see if the sequence sequence is one of the two, if it is "yes"; else "NO";
Code:
#include <iostream> #include <iomanip> #include <string> #include <string.h> #include < stdio.h> #include <algorithm> #include <queue> #include <vector>using namespace std;typedef struct node{char data; struct node *ll; struct node *rr;} Binode, *bitree;void creat_sort_bitree (bitree &root, int key) {if (root==null) {root=new binode; root->ll=null; root->rr=null; root->data=key; return; } else {if (Key < Root->data) {Creat_sort_bitree (root->ll, key); } else {Creat_sort_bitree (ROOT->RR, key); }}}char S1[100],e=0;char s2[100];void pre_order (Bitree p) {if (p) {s1[e++]=p->data; Pre_order (P->LL); Pre_order (P->RR); }}void Post_order (Bitree p) {if (p) {Post_order (P->LL); Post_order (P->RR); s2[e++]=p->data; }}int Main () { int n, DD; int i; Bitree Root; Char a[15]; while (cin>>n) {if (n==0) break; scanf ("%s", a); int Len=strlen (a); Root=null; for (i=0; i<len; i++) {creat_sort_bitree (root, a[i]); } e=0; Pre_order (root); s1[e]= ' + '; e=0; Post_order (root); s2[e]= ' + '; Char s[50]; for (i=0; i<n; i++) {scanf ("%s", s); if (strcmp (S1, s) ==0 | | strcmp (s2, s) ==0) {cout<< "yes\n"; } else {cout<< "no\n"; }}} return 0;}
Binary tree structure of data structure practice---binary sorting tree