Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=2175
Type of topic: data structure, two fork tree
The main effect of the topic:
Under normal circumstances, are infix operators, such as X+y. Then the topic gives the form of a suffix operator, which becomes X y +. The suffix can be used to simulate the stack, using Push,pop, and the procedure is similar to the classic "bracket matching". Then we are asked to convert the queue to the way the queue is pushed and pop (the difference between the queue and the stack).
The idea of disintegration:
At first there was no thought, and later I felt that I was going to make achievements. This is the first two-tree problem I wrote.
The most critical part of the topic is to make a binary tree, as well as the hierarchical traversal of the output, there is the use of stack "bracket matching" idea. The basic structure of the binary tree is that the parent node is an operator and the child nodes are numbers. For the given sequence, traverse from left to right, and the lowercase that represents the number to build a tree without a son, then put the root node pointer into the stack, encountered the capital letter representing the operator, then popped two root nodes from the stack, and then set up a capital letter as the root, pop-up two operands for the left and right son of the tree, Then put the root node pointer of the new tree into the stack. So the cycle goes on. Finally, the pointer at the top of the stack is the root node of the tree that was last built. Then the tree to the level of traversal to remove the letter, the final reverse output can be.
Sample input:
2
Xypzwim
Abcabdefgcdef
Sample output:
Wzyxipm
Gfcecbddaaebf
Code:
1. Array version
10278049 11234 expressions accepted c++ 1.512 2012-07-01 12:59:01
#include <iostream> #include <cstdio> #include <cctype> #include <cstring> #include <stack&
Gt
#include <queue> using namespace std;
Class node{Public:char data;
int left;
int right;
};
stack<int>st;
queue<int>qu;
Node arr[10005];
Char str[10005];
int result[10005], resultindex;
A wide search hierarchy traversal void bfs (int root) {while (!qu.empty ()) Qu.pop ();
Qu.push (root);
Result[resultindex++]=arr[root].data;
while (!qu.empty ()) {int t = Qu.front ();
Qu.pop ();
if (Arr[t].left!=-1) {result[resultindex++] = Arr[arr[t].left].data;
Qu.push (Arr[t].left);
} if (Arr[t].right!=-1) {result[resultindex++] = Arr[arr[t].right].data;
Qu.push (Arr[t].right);
}} void Solve () {while (!st.empty ()) St.pop (); for (int i=0; I<strlen (str);
++i) {if (Islower (str[i))) {St.push (i);
Arr[i].data = Str[i];
Arr[i].left =-1;
Arr[i].right =-1;
} else{int right = St.top ();
St.pop ();
int left = St.top ();
St.pop ();
Arr[i].data = Str[i];
Arr[i].left = left;
Arr[i].right = right;
St.push (i);
}///level traversal, the letter exists on a stack (in order to reverse output), and then output resultindex = 0;
BFS (St.top ());
Output for (int i=resultindex-1; i>=0;-i) printf ("%c", result[i) in reverse order of wide search results;
printf ("\ n");
int main () {freopen ("Input.txt", "R", stdin);
int T;
scanf ("%d", &t);
while (t--) {scanf ("%s", str);
Solve ();
return 0; }
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45534.htm
2. Pointer dynamic memory allocation version
#include <iostream> #include <cstdio> #include <cctype> #include <cstring> #include <stack&
Gt
#include <queue> using namespace std;
Class node{Public:char data;
Node* left;
node* right;
};
stack<node*>st;
queue<node*>qu;
Char str[10005];
int result[10005], resultindex;
Conduct a wide search level traversal void BFs (node* root) {while (!qu.empty ()) Qu.pop ();
Qu.push (root);
result[resultindex++]=root->data;
while (!qu.empty ()) {node* t = Qu.front ();
Qu.pop ();
if (t->left!= NULL) {result[resultindex++] = t->left->data;
Qu.push (T->left);
} if (T->right!= NULL) {result[resultindex++] = t->right->data;
Qu.push (T->right);
}} void Solve () {while (!st.empty ()) St.pop (); for (int i=0; I<strlen (str);
++i) {if (Islower (Str[i])) {Node *temp = new node;
Temp->data = Str[i];
Temp->left = NULL;
Temp->right = NULL;
St.push (temp);
} else{node* right = St.top ();
St.pop ();
Node* left = St.top ();
St.pop ();
node* parent = new Node;
Parent->data = Str[i];
Parent->left = left;
Parent->right = right;
St.push (parent);
}///level traversal, the letter exists on a stack (in order to reverse output), and then output resultindex = 0;
BFS (St.top ());
Output for (int i=resultindex-1; i>=0;-i) printf ("%c", result[i) in reverse order of wide search results;
printf ("\ n");
int main () {freopen ("Input.txt", "R", stdin);
int T;
scanf ("%d", &t);
while (t--) {scanf ("%s", str);
Solve ();
return 0; }