前序中序后序输出结点信息

#include<iostream.h>
struct BiNode
{
char data;
BiNode *lchild,*rchild;
};


class BiTree
{
public:
BiTree(){root=Create(root);}
~BiTree(){Release(root);}
void Preorder(){Preorder(root);}
void Inorder(){Inorder(root);}
void Postorder(){Postorder(root);}
private:
BiNode *root;
BiNode *Create(BiNode *bt);
void Release(BiNode *bt);
void Preorder(BiNode *bt);
void Inorder(BiNode *bt);
void Postorder(BiNode *bt);
};


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()
{
cout<<endl;
cout<<"*******请输入创建一棵二叉树的结点数据(链式)**********"<<endl<<endl;
BiTree T;
cout<<endl;
cout<<"**********前序遍历*********"<<endl;
T.Preorder();
cout<<endl;
cout<<"**********中序遍历*********"<<endl;
T.Inorder();
cout<<endl;
cout<<"**********后序遍历***********"<<endl;
T.Postorder();
cout<<endl;
return 0;

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页