c++データ構造二叉チェーンテーブルの実現


#include
using namespace std;

struct binode
{
    char data;
    binode*lchild, *rchild;
};

class bitree
{
public:
    bitree();
    ~bitree();
    void preorder();
    void inorder();
    void postorder();
private:
    binode*root;//       
    binode*create(binode*bt);//      
    void release(binode*bt);//      
    void preorder(binode*bt);//        
    void inorder(binode*bt);
    void postorder(binode*bt);
};

bitree::bitree()
{
    root = create(root);
}

bitree::~bitree()
{
    release(root);
}
void bitree::preorder()
{
    preorder(root);
}

void bitree::inorder()
{
    inorder(root);
}

void bitree::postorder()
{
    postorder(root);
}

binode*bitree::create(binode*bt)
{
    char ch;
    cin >> ch;
    if (ch == '#')return NULL;
    else
    {
        bt = new binode;
        bt->data = ch;
        bt->lchild = create(bt->lchild);//         
        bt->rchild = create(bt->rchild);
    }
    return bt;
}

void bitree::release(binode*bt)
{
    if (bt != NULL)
    {
        release(bt->lchild);
        release(bt->rchild);
        delete bt;
    }
}

void bitree::preorder(binode*bt)
{
    if (bt == NULL)return;
    else
    {
        cout << bt->data << " ";
        preorder(bt->lchild);
        preorder(bt->rchild);
    }
}

void bitree::inorder(binode*bt)
{
    if (bt == NULL)return;
    else
    {
        inorder(bt->lchild);
        cout << bt->data << " ";
        inorder(bt->rchild);

    }
}

void bitree::postorder(binode*bt)
{
    if (bt == NULL)return;
    else
    {
        postorder(bt->lchild);
        postorder(bt->rchild);
        cout << bt->data << " ";
    }
}

int main()
{
    bitree t;//create a tree 
    cout << "---------------     ----------------" << endl;
    t.preorder();
    cout << endl;
    cout << "---------------     ----------------" << endl;
    t.inorder();
    cout << endl;
    cout << "---------------     ----------------" << endl;
    t.postorder();
    cout << endl;
    return 0;
}