二叉链表

#include<iostream>  
#include<string>  
using namespace std;  
  
struct Node  
{  
    string data;  
    Node *Lchild,*Rchild;  
};  
  
  
class BiTree  
{  
public:  
    BiTree(){root=Create(root);}   //构造函数,建立一颗二叉树  
    ~BiTree(){Release(root);}     //析构函数,释放各节点的存储空间  
    void Preorder(){Preorder(root);}   //前序  
    void Inorder(){Inorder(root);}     //中序  
    void Postorder(){Postorder(root);} //后序  
    void Leverorder();     //层序  
    void print(){print(root);}  
    void print_family(){print_family(root);}  
  
  
private:  
    Node *root;     //指向根的头  
    Node *Create(Node *bt);   //构造调用  
    void Release(Node *bt);   //析构调用  
    void Preorder(Node *bt);  //前序  
    void Inorder(Node *bt);   //中序  
    void Postorder(Node *bt); //后序  
    void PrintParent(Node *bt);   //输出双亲  
    void print(Node *bt);  
    void print_family(Node *bt);   //输出亲戚  
};  


Node*BiTree::Create(Node *bt)  
{  
   string s;  
   cin>>s;  
   if(s=="none")  
       bt=NULL;  
   else{  
       bt=new Node;  
       bt->data=s;  
       bt->Lchild=Create(bt->Lchild);  
       bt->Rchild=Create(bt->Rchild);  
    }  
   return bt;  
}  
  
void BiTree::Release(Node *bt)  
{  
    if(bt!=NULL){  
       Release(bt->Lchild);  
       Release(bt->Rchild);  
       delete bt;  
    }  
}  
  
void BiTree::Preorder(Node *bt)  
{  
  if(bt==NULL) return;  
  else{  
    cout<<bt->data<<'\t';  
    Preorder(bt->Lchild);  
    Preorder(bt->Rchild);  
  }  
}  
  
void BiTree::Inorder(Node *bt)  
{  
  if(bt==NULL)  
      return;  
  else{  
     Inorder(bt->Lchild);  
     cout<<bt->data<<'\t';  
     Inorder(bt->Rchild);  
  }  
}  
  
void BiTree::Postorder(Node *bt)  
{  
   if(bt==NULL)  
       return;  
   else{  
      Postorder(bt->Lchild);  
      Postorder(bt->Rchild);  
      cout<<bt->data<<'\t';  
   }  
}  
  
void BiTree::Leverorder()  
{  
   Node *Q[10];  
   int front,rear;  
   front=rear=-1;  
   if(root==NULL)  
       return;  
   Q[++rear]=root;  
   while(front!=rear)  
   {  
     Node *q;  
     q=Q[++front];  
     cout<<q->data<<'\t';  
     if(q->Lchild!=NULL)  
         Q[++rear]=q->Lchild;  
     if(q->Rchild!=NULL)  
         Q[++rear]=q->Rchild;  
   }  
}  
void BiTree::print(Node *bt)  
{  
   if(bt!=NULL)  
   {  
      if(!bt->Lchild&&!bt->Rchild)  
          cout<<bt->data<<'\t';  
      print(bt->Lchild);  
      print(bt->Rchild);  
   }  
}  
  
void BiTree::print_family(Node *bt)  
{  
  if(bt==NULL) return;  
  else{  
    cout<<'\n'<<endl;  
    cout<<"我是:"<<bt->data<<",";  
    if(bt->Lchild!=NULL&&bt->Lchild->data!="none")  
    cout<<"我的左孩子是:"<<bt->Lchild->data<<",";  
    else  
    cout<<"我没有左孩子"<<",";  
  
    if(bt->Rchild!=NULL&&bt->Rchild->data!="none")  
    cout<<"我的右孩子是:"<<bt->Rchild->data<<";"<<endl;  
    else  
    cout<<"我没有右孩子"<<endl;  
    print_family(bt->Lchild);  
    print_family(bt->Rchild);  
  }  
}  


int main()  
{  
   cout<<"请输入结点数据,none表示空"<<'\t';  
   BiTree one;  
   cout<<"\n"<<"***************"<<endl;  
   cout<<"前序遍历:"<<'\t';  
   one.Preorder();  
   cout<<'\n'<<"中序遍历:"<<'\t';  
   one.Inorder();  
   cout<<'\n'<<"后序遍历:"<<'\t';  
   one.Postorder();  
   cout<<'\n'<<"层序遍历:"<<'\t';  
   one.Leverorder();  
   cout<<'\n'<<"叶子结点为:"<<'\t';  
   one.print();  
   cout<<'\n'<<endl;  
   cout<<"输出家人:"<<endl;  
   one.print_family();  
   return 0;  
}  

猜你喜欢

转载自blog.csdn.net/guangnianaaa/article/details/80454465