种一棵链式存储的树

来源:互联网 发布:coc大本升级数据 编辑:程序博客网 时间:2024/05/21 09:54

简介:使用链式存储结构种了一棵string类型的二叉树,实现了前序、中序、后序、层序遍历结点;并查找输出所有的叶子结点信息。


类的定义:

#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[100];   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;}

接下来看看种的这棵树长什么样子吧~



程序输出是这样的~



总结:

1. 在链式存储中,二叉树的每个结点对应一个链表结点,其中存放了二叉树信息以及指示左右孩子的指针,当输入指定标识符表示空时要将指针置空,表示不存在结点。

 

2. 在二叉链表中使用了递归函数进行遍历,在时间效率上比较低;另外,该树是用二叉链表为存储结构,还没有实现查找双亲的功能。

 

3. 注意的是在层序遍历的时候要使用到顺序队列,首先要将根指针入队,遍历时,队头元素出队,访问队头数据域,若该结点存在左孩子就将左孩子的指针入队,若存在右孩子就将右孩子入队,循环直到队列为空。


注:要实现查找双亲的功能就需要在二叉链表的基础上再加一个Parent域,指向结点的双亲结点的指针。