这个很简单吗,给你段代码,是我最近刚编的二叉树程序,已经在vc++6.0和devc++上调试过了。
其中包括一个前序的创建;前序,中序,后序的输出;还有一个前序,中序输入一棵树,确定后序 ,我是用队列作的,你可以先注释掉,先解决主要问题;另外就是一些诸如求高度,节点数的小方法。我这个是整形数的,你只要改成字符串类型的就可以了,别告诉我这你都不会,那我就帮不了你了。
#include
using namespace std;
//binarytree node class
class Node{
friend class BinaryTree;
friend class Queue;
public:
Node(){leftChild=rightChild=0;}
private:
int data;
Node *leftChild,*rightChild;
};
//queuenode
class QueueNode{
friend class Queue;
private:
int number;
QueueNode *link;
};
class Queue{
public:
friend class BinaryTree;
Queue(){front=rear=0;}
bool Empty() const {
return ((front)?false:true);}
int First() const;
Queue& Add( int &x);
Queue& Delete(int &x);
private:
QueueNode *front,*rear;
};
//binarytree class
class BinaryTree{
public:
BinaryTree(){root=0; nodes= 0;}
void make(){
cout<<"Please input the VALUE of the node\n";
root=Create();}
~BinaryTree(){};
bool Empty() const{
return ((root)? false:true);
}
int Nodes(){ int count=nodes; nodes=0;return count;}
int Height(){ return height(root);}
void Preorder( ) {
PreOrder( root);
cout<<"\n";
}
void Midorder(){
MidOrder(root);
cout<<"\n";
}
void Postorder(){
PostOrder(root);
cout<<"\n";
}
void Identify(){
Queue qpre,qmid;
cout<<"please enter the number of nodes of the tree .\n";
int number;
cin>>number;
int x,y;
int count=number;
cout<<"preorder:\n";
while(count>0){
cin>>x;
qpre.Add(x);
count--;
}
count=number;
cout<<"inorder:\n";
while(count>0){
cin>>y;
qmid.Add(y);
count--;
}
identify(qpre,qmid,root);
}
private:
Node* Create() ;
void PreOrder (Node *r );
void MidOrder(Node *r);
void PostOrder(Node *r);
void identify(Queue &qpre,Queue &qmid,Node *it);
int height(Node *it)const{
if(!it){ return 0;}
int hl=height(it->leftChild);
int hr=height(it->rightChild);
if(hl>hr) return ++hl;
else return ++hr;
}
Node *root;
int nodes ;
};
// create a tree by preorder
Node* BinaryTree::Create(){
Node *bnode=new Node();
int a;
cin>>a ;
if(a<1){bnode=0;
}
else{
nodes++;
bnode->data=a;
cout<<" the left child of: "< bnode->leftChild=Create();
cout<<" the right child of: "< bnode->rightChild=Create();
}
return bnode;
}
//previous order visit a tree
void BinaryTree::PreOrder(Node *r ){
if(r){
cout<
PreOrder( r->leftChild);
PreOrder( r->rightChild);
}
}
//middle order visit a tree
void BinaryTree::MidOrder(Node *r){
if(r){
MidOrder(r->leftChild);
cout<
MidOrder(r->rightChild);
}
}
//post level traveral
void BinaryTree::PostOrder(Node *r){
if(r){
PostOrder(r->leftChild);
PostOrder(r->rightChild);
cout<
}
}
//identify a tree by preorder and midorder
void BinaryTree::identify(Queue &qpre,Queue &qmid,Node *it){
it=new Node();
int x,y;
Queue qprel,qmidl;
qpre.Delete(it->data);
qmid.Delete(y);
while(y!=it->data){
qpre.Delete(x);
qprel.Add(x);
qmidl.Add(y);
qmid.Delete(y);
}
if(qprel.Empty()) {it->leftChild=0;}
else{ identify(qprel,qmidl,it->leftChild);}
if(qpre.Empty()) {it->rightChild=0;}
else{identify(qpre,qmid,it->rightChild);}
cout<
}
int Queue::First() const{
return front->number;
}
Queue& Queue::Add( int &x){
QueueNode *next=new QueueNode();
next->number=x;
next->link=0;
if(front==0){
front=next;
rear=front;
}
else {
rear->link=next;
rear=next;
}
return *this;
}
Queue& Queue::Delete(int &x){
if( Empty()) cout<<"is empty\n";
else{QueueNode *next;
x=front->number;
next=front;
front=front->link;
delete next;}
return *this;
}
//main function
main( ){
BinaryTree it ;
BinaryTree good;
it.make();
cout<<"inorder: "; it.Midorder();
cout<<"postorder: "; it.Postorder();
cout<<"preOrder: "; it.Preorder();
cout<<"the nodes of the tree: "<
good.Identify();
good.Midorder();
cout<<"\nok\n";
cout<<"please enter a charter to termital the programme:";
int ends;
cin>>ends;
}