DS 二叉树实现

来源:互联网 发布:linux启动oracle实例 编辑:程序博客网 时间:2024/06/05 17:19

这个实现的时候由于采用了递归,思路比较清晰。出错的地方是在建立树的时候关于指针的指针容易出错,我比较菜,求助了网上的热心人才搞明白。

附上网址供大家参考http://bbs.csdn.net/topics/391008633

http://www.cnblogs.com/greatIDeas/archive/2010/11/24/1886915.html

http://blog.csdn.net/muzi9_17/article/details/6745141

http://blog.163.com/northow@126/blog/static/11814806020094257579530/

附:二叉树的基本性质

1、二叉树的第i层上最多有2^(i-1)个结点(i>=1)

2、一棵深度为k的二叉树中,最多有2^(k)-1个结点,最少有k个结点。

3、二叉树中,如果叶子结点的个数为n0,度为2的结点个数为n2,则n0=n2+1

4、具有n个结点的完全二叉树的深度为└log2(n)┘+1

/*************************************************************************     > File Name: BiTree.cpp     > Author: Shorey     > Mail: shoreybupt@gmail.com     > Created Time: 2015年04月01日 星期三 10时53分46秒  ************************************************************************/    #include<iostream>  using namespace std;  [cpp] view plain copy print?#define Max 100  struct BiNode  {      int data;      BiNode *lchild,*rchild;  };    class BiTree  {      public:          BiTree(BiNode **root);//有参构造函数          ~BiTree();           void PreOrder(BiNode *root);//前序遍历          void InOrder(BiNode *root);//中序遍历          void PostOrder(BiNode *root);//后序遍历          void LevelOrder(BiNode *root);//层序遍历          void Release(BiNode *root);//销毁树      private:          void Creat(BiNode **root);//调用析构函数  };    void BiTree::PreOrder(BiNode *root)//前序遍历  {      if(root==NULL)return;           //递归调用结束条件      else      {          cout<<root->data;            //访问根节点          PreOrder(root->lchild);      //前序递归访问左子树          PreOrder(root->rchild);      //前序访问右子树      }  }    void BiTree::InOrder(BiNode *root)//中序遍历  {      if(root==NULL)return;           //递归调用结束条件      else      {          InOrder(root->lchild);     //中序访问左子树          cout<<root->data;          //中序访问根节点          InOrder(root->rchild);     //中序访问右子树      }  }    void BiTree::PostOrder(BiNode *root)//后序遍历  {      if(root==NULL)return;      else      {          PostOrder(root->lchild);          PostOrder(root->rchild);          cout<<root->data;      }  }    void BiTree::LevelOrder(BiNode *root)//层序遍历  {      BiNode *Q[Max],*q;           //设置缓冲队列和工作指针        int front=0,rear=0;        //缓冲队列头指针和尾指针初始化      if(root==NULL)return;      Q[(++rear)%Max]=root;        //根节点入队      while(front!=rear)         //队列不空则继续      {          q=Q[(++front)%Max];      //队头出队          cout<<q->data;         //打印          if(q->lchild!=NULL)Q[(++rear)%Max]=q->lchild;//左孩子入队          if(q->rchild!=NULL)Q[(++rear)%Max]=q->rchild;//右孩子入队      }  }    BiTree::BiTree(BiNode **root)//构造函数  {      Creat(root);  }    void BiTree::Creat(BiNode **root)  {      int ch;      cin>>ch;      if(ch==-1)*root=NULL;//输入#则建立停止      else      {          *root=new BiNode;          (*root)->data=ch;          Creat(&((*root)->lchild));//递归建立左子树          Creat(&((*root)->rchild));//递归建立右子树      }  }    BiTree::~BiTree()  {    }    void BiTree::Release(BiNode *root)  {      if(root!=NULL)      {          Release(root->lchild);//释放左子树          Release(root->rchild);//释放右子树          delete root;      }  }  int main()  {      BiNode *root;      BiTree tree(&root);      cout<<endl;      tree.PreOrder(root);      cout<<endl;      tree.InOrder(root);      cout<<endl;      tree.PostOrder(root);      cout<<endl;      tree.LevelOrder(root);      cout<<endl;      tree.Release(root);        return 0;  }  


0 0
原创粉丝点击