二叉树的链式结构

#ifndef Tree_H      
#define Tree_H
const int Max=20;        
                         
struct TNode
{
char data;             
TNode *leftchild,*rightchild;
};




class Tree               
{
public:
Tree(){root=Creat(root);}
~Tree(){Release(root);}
void PreOrder(){PreOrder(root);}
void InOrder(){InOrder(root);}
void PostOrder(){PostOrder(root);}
private:
TNode *root;
TNode *Creat(TNode *bt);
void Release(TNode *bt);         
void PreOrder(TNode *bt);        
void InOrder(TNode *bt);
void PostOrder(TNode *bt);       


};

#endif



#include               
using namespace std;               
#include "LinklistTree.h"                    
                                     
TNode *Tree::Creat(TNode *bt)
{
char ch;
cout<<"请输入创建一颗二叉树的结点数据"< cin>>ch;
if(ch=='#')return NULL;
else{
bt=new TNode;
bt->data=ch;
bt->leftchild=Creat(bt->leftchild);
bt->rightchild=Creat(bt->rightchild);
}
return bt;
}


void Tree::Release(TNode *bt)
{
if (bt==NULL) return;           
else
{
Release(bt->leftchild);               
Release(bt->rightchild);               
delete bt;
}
}
void Tree::PreOrder(TNode *bt)
{
if(bt==NULL) return;          
else 
{
cout<data;
PreOrder(bt->leftchild);
PreOrder(bt->rightchild);
}
}


void Tree::InOrder(TNode *bt)
{
if(bt==NULL)return;
else{
InOrder(bt->leftchild);
cout<data<<" ";
InOrder(bt->rightchild);
}
}


void Tree::PostOrder(TNode *bt)
{
if(bt==NULL) return;
else
{
PostOrder(bt->leftchild);           
PostOrder(bt->rightchild);             
cout<data;                    
}
}

#include
using namespace std;
#include"LinklistTree.h"


int main()
{


Tree t1;
cout<<"---前序遍历---"< t1.PreOrder();
cout< cout<<"---中序遍历---"< t1.InOrder();
cout< cout<<"---后序遍历---"< t1.PostOrder();
cout<   
return 0;
}







你可能感兴趣的:(二叉树的链式结构)