Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- typedef char type;
- struct elem{
- elem* l = nullptr;
- elem* r = nullptr;
- type atom;
- };
- void make_space(string& str){
- for (int i = 0; i < str.size(); i++){
- if (str[i + 1] != ' ' && str[i] != ' ') {
- str.insert(i + 1, " ");
- i++;
- }
- }
- }
- elem* create_left(elem* bin_tree, type atom){
- elem* push = new elem;
- bin_tree->l = push;
- bin_tree = bin_tree->l;
- bin_tree->atom = atom;
- return bin_tree;
- }
- elem* create_right(elem* bin_tree, type atom){
- elem* push = new elem;
- bin_tree->r = push;
- bin_tree = bin_tree->r;
- bin_tree->atom = atom;
- return bin_tree;
- }
- //(a (b (d #(h # #))(e # #))(c (f (i # #)(j # #))(g #(k (l # #) #))))
- elem* create_bin_tree(elem* tree, istringstream& stream, bool init) {
- if (init){
- tree = new elem;
- }
- char x;
- elem* cur = tree;
- stream >> x;
- if (x == '('){
- stream >> x;
- if (init){
- tree->atom = x;
- init = false;
- create_bin_tree(tree, stream, init);
- }else {
- tree = create_left(cur, x);
- create_bin_tree(tree, stream, init);
- }
- }
- if (x == '#'){
- stream >> x;
- if (x == '('){
- stream >> x;
- tree = create_right(cur, x);
- create_bin_tree(tree, stream, init);
- }
- }
- stream >> x;
- if (x == ')'){
- return nullptr;
- }
- if (x == '('){
- stream >> x;
- tree = create_right(cur, x);
- create_bin_tree(tree, stream, init);
- }
- stream >> x;
- return cur;
- }
- bool check_ident(elem* tree1, elem* tree2){
- if (tree1 && tree2)
- {
- if(tree1->atom == tree2->atom)
- return check_ident(tree1->l, tree2->r)&&check_ident(tree1->r, tree2->l);
- else
- return false;
- }
- else return !(tree1 == nullptr xor tree2 == nullptr);
- }
- void destroy(elem*& tree){
- if (tree != nullptr){
- destroy(tree->l);
- destroy(tree->r);
- delete tree;
- tree = nullptr;
- }
- }
- int main(){
- string str_bin_tree1;
- char x;
- elem* bin_tree1 = nullptr;
- getline(cin, str_bin_tree1);
- make_space(str_bin_tree1);
- istringstream stream1(str_bin_tree1);
- bin_tree1 = create_bin_tree(nullptr, stream1, true);
- destroy(bin_tree1);
- return 0;
- }
- //(a (b (d #(h # #))(e # #))(c (f (i # #)(j # #))(g #(k (l # #) #))))
- //(a (c (g (k #(l # #))#)(f(j # #)(i # #)))(b (e # #)(d (h # #)#)))
- /*
- *
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement