递归构建二叉树

部分功能未能实现。。。。把有关双亲的部分全部注释掉可正常使用


#include
using namespace std;
struct BiNode
{
 int data;
 BiNode *lchild,*rchild,*bt,*parent;
};
class BiTree
{
 public:
  BiTree(){root=Creat(root);}
  ~BiTree(){Release(root);}
  void PreOrder(){PreOrder(root);}
  void InOrder(){InOrder(root);}
  void PostOrder(){PostOrder(root);}
  void LeverOrder();
 private:
  BiNode *root;
  BiNode *Creat(BiNode *bt);
  void Release(BiNode *bt);
  void PreOrder(BiNode *bt);
  void InOrder(BiNode *bt);
  void PostOrder(BiNode *bt);
  int front,rear;
};  
void BiTree::PreOrder(BiNode *bt)
{
 if(bt==NULL)
   return ;
 else{
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  cout<<"自己:"<data<  PreOrder(bt->lchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  PreOrder(bt->rchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data< }
}
void BiTree::InOrder(BiNode *bt)
{
 if(bt==NULL)return ;
 else{
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  InOrder(bt->lchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  cout<<"自己:"<data<  InOrder(bt->rchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data< }
}
void BiTree::PostOrder(BiNode *bt)
{
 if (bt==NULL)return ;
 else{
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  PostOrder(bt->lchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  PostOrder(bt->rchild);
  if(bt->parent!=NULL)
  cout<<"双亲:"<<(bt->parent)->data<  cout<<"自己:"<data< }
}
BiNode *BiTree::Creat(BiNode *bt)
{
 int ch;
 cin>>ch;
 if(ch==0)bt=NULL;
 else{
  bt=new BiNode;bt->data=ch;
  bt->lchild=Creat(bt->lchild);
  bt->rchild=Creat(bt->rchild);
 } if(bt->lchild->parent==NULL)
 bt->lchild->parent=bt;
 if(bt->rchild->parent==NULL)
 bt->rchild->parent=bt;
 
 return bt;
}
void BiTree::Release(BiNode *bt)
{
 if(bt!=NULL){
  Release(bt->lchild);
  Release(bt->rchild);
  delete bt;
 }
}

int main()
{
 BiTree bi;
 cout < bi.PostOrder();
 cout< bi.InOrder();
 cout< bi.PreOrder();
 cout< return 0;
}

你可能感兴趣的:(递归构建二叉树)