[ create a new paste ] login | about

Link: http://codepad.org/cfR6Q5LL    [ raw code | output | fork | 1 comment ]

yhl20001210 - C++, pasted on Nov 5:
//bin_tree-DEMO.cpp
//A binary tree and operations on it.
//Foreword: For something to submit to a algorithm competition with some difficulty as this, it seems too complicated.

#include <iostream>
#include <string>

namespace bt {
    struct node {
        char data;
        node* left_child, right_child;
        node(char data='\0') :data(data), left_child(NULL), right_child(NULL) {}
    };
    const bool PRE=true;
    const bool POST=false;
    const bool LEFT=true;
    const bool RIGHT=false;
    node* create(bool emul=true);
    //node* create(string strp, string strm);
    void remove(node* t);
    string preorder(node* t);
    string inorder(node* t);
    string postorder(node* t);
}

using std::cin;
using std::cout;
using std::string;
using bt::create; //?
using bt::remove;
using bt::preorder;
using bt::inorder;
using bt::postorder;
using bt::node;

int main() {
    node* root=create();
    cout<<"A Binary Tree - DEMO\n"
        <<preorder(root)<<"\n"
        <<inorder(root)<<"\n"
        <<postorder(root)<<"\n";
    remove(root->left_child);
    cout<<preorder(root)<<"\n";
    return 0;
}

node* bt::create(bool emul=true) {
    if (emul) {
    node* root=new node('A'),
          B=new node('B'),
          C=new node('C'),
          D=new node('D'),
          E=new node('E'),
          F=new node('F'),
          G=new node('G'),
          H=new node('H'),
          I=new node('I');
    root->left_child=B;
    root->right_child=C;
    B->left_child=D;
    B->right_child=E;
    C->right_child=F;
    D->left_child=G;
    D->right_child=H;
    F->left_child=I;
    return root;
    }
    else {
    } //Customized creations. -COMING SOON
}

/*
node* bt::create(const string& strp, const string& stri, bool mode=bt::PRE;) {
    //The edge of recursion: ...
    //ERROR HANDLING: ...
    int rootp=mode?0:(strp.length()-1);
    int rooti=str.find(strp[rootp])-1;
    node* root=new node(strp[rootp]);
    string strpt="",
           strit=stri.substr(mode?0:rooti, mode?rooti:stri.length()-1);
    findnodes(&strpt, strit, bt::LEFT);
    root->left_child=create(strpt, strit, mode);
    strit=stri.substr(mode?rooti:stri.length()-1, mode?0:rooti);
    findnodes(&strpt, strit, bt::RIGHT);
    root->right_child=create(strpy, strit, mode);
    return root;
} //A mess for names of variables!
*/

void bt::remove(node* t) {
    if (t==NULL) return;
    remove(t->left_child);
    remove(t->right_child);
    delete t;
}

string bt::preorder(node* t) {
    string str="";
    if (t==NULL) return str;
    str=str+t.data;
    str=str+preorder(t->left_child);
    str=str+preorder(t->right_child);
    return str;
}

string bt::inorder(node* t) {
    string str="";
    if (t->left_child!=NULL)
        str=str+inorder(t->left_child);
    str=str+t.data;
    if (t->right_child!=NULL)
        str=str+inorder(t->right_child);
    return str;
} //Reserved my original idea but there is a bug: what if the first call is to NULL?

string bt::postorder(node* t) {
    string str="";
    if (t==NULL) return str;
    str=str+postorder(t->left_child);
    str=str+postorder(t->right_child);
    str=str+t.data;
    return str;
}


Output:
1
2
Line 11: error: field 'right_child' has incomplete type
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments:
posted by yhl20001210 on Nov 11
A flag turned the warning into the error?
reply