二叉树的那些事

二叉树的操作,最近写了这些代码,同时也借鉴了网上前辈的们的经验。有时间再继续完善。创建的问二叉排序树。微笑


#include<iostream>

#include<stack>

#include<vector>

#include<queue>

#include<Windows.h>

using namespacestd;

template<class Type>

class BStree;

int maxlen=0;

intdistancebetweenNode=0;

二叉树节点的定义

template<classType>

class BinaryNode

{

  friend class BStree<Type>;

public:

      BinaryNode():left(NULL),right(NULL){}

      BinaryNode(const Type&value):data(value),left(NULL),right(NULL){}

private:

      Type data;

      BinaryNode *left;

      BinaryNode *right;

      int maxleft;

      int maxright;

      //bool isFirst;

};

template<classType>

class BStree

{

private:

      BinaryNode<Type> *root;

      bool isFirst;

public:

      //int maxlen;

      BStree():root(NULL){}

      BStree(const Type & tree);

      BinaryNode<Type>*GetRoot()const{return root;}

      Type createBitree(const Type & value);

      //==================================================//

      //树的递归遍历

      //=================================================//

      void preOrder(const BinaryNode<Type>*startNode);

      void inOrder(const BinaryNode<Type>*starNode);

      void PostOrder(constBinaryNode<Type>*startNode);

 

      //=================================================//

      //树的非递归遍历

      //===============================================//

      void noRecursivePre(BinaryNode<Type>* startNode);

      void noRecursiveIn(BinaryNode<Type>*startNode);

      voidnoRecursivePost1(BinaryNode<Type>* startNode);

      voidnoRecursivePost2(BinaryNode<Type>* startNode);

      //二叉树中节点的最大距离

      void FindMaxLen(BinaryNode<Type>*startNode);

      //二叉树最低公共节点

      bool findpath(BinaryNode<Type>*startNode,vector<int> &path,int key);

      int findLCA(BinaryNode<Type>*startNode,int key1,int key2);

      //二叉树任意两个节点之间的距离

      voidFindLen(BinaryNode<Type>*startNode,int key1,int key2);

      //二叉树的深度

      inttreeDepth(BinaryNode<Type>*startNode);

      int treeDepthNoRe(BinaryNode<Type>*startNode);

      //二叉树的宽度

      inttreeWidth(BinaryNode<Type>*startNode);

      //判断平衡二叉树

      boolbalanceTree(BinaryNode<Type>*startNode);

     

};

 

创建二叉树

template<classType>

TypeBStree<Type>::createBitree(const Type &value)

{

      if(root==NULL)

      {

             root=new BinaryNode<Type>(value);

      }

      else

      {

             BinaryNode<Type> *node=root;

             while(1)

             {

                    if(value>node->data)

                    {

                           if(node->right==NULL)

                           {

                                  node->right=newBinaryNode<Type>(value);

                                  break;

                           }

                           else

                           {

                                  node=node->right;

                           }

                    }

                    else

                    {

                           if(node->left==NULL)

                           {

                                  node->left=newBinaryNode<Type>(value);

                                  break;

                           }

                           else

                           {

                                  node=node->left;

                           }

                    }

             }

      }

      return value;

}

二叉树线序遍历(递归)

template<classType>

voidBStree<Type>::preOrder(const BinaryNode<Type> *startNode)

{

      if(startNode==NULL)

             return ;

      else

      {

             cout<<startNode->data<<"";

             preOrder(startNode->left);

             preOrder(startNode->right);

 

      }

     

}

二叉树中序遍历(递归)

template<classType>

voidBStree<Type>::inOrder(const BinaryNode<Type>*startNode)

{

      if(startNode==NULL)

             return;

      else

      {

             inOrder(startNode->left);

             cout<<startNode->data<<"";

             inOrder(startNode->right);

      }

}

二叉树后序遍历(递归)

template<classType>

voidBStree<Type>::PostOrder(const BinaryNode<Type>*startNode)

{

      if(startNode==NULL)

             return;

      else

      {

             PostOrder(startNode->left);

             PostOrder(startNode->right);

             cout<<startNode->data<<"";

      }

}

二叉树先序遍历(非递归)

template<classType>

voidBStree<Type>::noRecursivePre( BinaryNode<Type>*startNode)

{

      stack<BinaryNode<Type> *>s;

      BinaryNode<Type>  *p=startNode;

      while(p!=NULL||!s.empty())

      {

             while(p!=NULL)

             {

                    cout<<p->data<<"";

                    s.push(p);

                    p=p->left;

             }

             if(!s.empty())

             {

                    p=s.top();

                    s.pop();

                    p=p->right;

             }

      }

 

}

template<classType>

二叉树中序遍历(非递归)

voidBStree<Type>::noRecursiveIn(BinaryNode<Type>* startNode)

{

      stack<BinaryNode<Type> *>s;

      BinaryNode<Type>*p=startNode;

      while(p!=NULL||!s.empty())

      {

             while(p!=NULL)

             {

                    s.push(p);

                    p=p->left;

             }

             if(!s.empty())

             {

                    p=s.top();

                    s.pop();

                    cout<<p->data<<"";

                    p=p->right;

 

             }

      }

 

}

二叉树后序遍历(非递归)

template<classType>

voidBStree<Type>::noRecursivePost1(BinaryNode<Type>* startNode)

{

      stack<BStree<Type> *>s;

      BinaryNode<Type> *p=startNode;

      BStree<Type>*temp;

      while(p!=NULL||!s.empty())

      {

             while(p!=NULL)

             {

                    BStree<Type>*btn=(BStree *)malloc(sizeof(BStree));

                    btn->root=p;

                    btn->isFirst=true;

                    s.push(btn);

                    p=p->left;

             }

             if(!s.empty())

             {

                    temp=s.top();

                    s.pop();

                    if(temp->isFirst==true)//表示第一次访问

                    {

                           temp->isFirst=false;

                           s.push(temp);

                           p=temp->root->right;

                    }

                    else

                    {

                           cout<<temp->root->data<<"";

                           p=NULL;

                    }

             }

      }

 

 

}

二叉树后序遍历2(非递归)

template<classType>

voidBStree<Type>::noRecursivePost2(BinaryNode<Type>* startNode)

{

      stack<BinaryNode<Type> *>s;

      BinaryNode<Type> *cur;//当前节点

      BinaryNode<Type> *pre=NULL;//前一次被访问的节点

      s.push(startNode);

      while(!s.empty())

      {

             cur=s.top();

             if((cur->left==NULL&&cur->right==NULL)||(pre!=NULL&&(pre==cur->left||pre==cur->right)))

             {

                    cout<<cur->data<<"";

                    s.pop();

                    pre=cur;

             }

             else

             {

                    if(cur->right!=NULL)

                           s.push(cur->right);

                    if(cur->left!=NULL)

                           s.push(cur->left);

             }

      }

}

二叉树而节点之间的最大距离

template<class Type>

voidBStree<Type>::FindMaxLen(BinaryNode<Type>* startNode)

{

      int tmp;

      if(startNode==NULL)return;

    if(startNode->left==NULL)startNode->maxleft=0;

      if(startNode->right==NULL)startNode->maxright=0;

      if(startNode->left!=NULL)

             FindMaxLen(startNode->left);

      if(startNode->right!=NULL)

             FindMaxLen(startNode->right);

      if(startNode->left!=NULL)

      {

             tmp=(startNode->left->maxleft>startNode->left->maxright)?(startNode->left->maxleft):(startNode->left->maxright);

             startNode->maxleft=tmp+1;

      }

      if(startNode->right!=NULL)

      {

             tmp=(startNode->right->maxleft>startNode->right->maxright)?(startNode->right->maxleft):(startNode->right->maxright);

             startNode->maxright=tmp+1;

      }

     

      maxlen=(startNode->maxleft+startNode->maxright)>(maxlen)?(startNode->maxleft+startNode->maxright):maxlen;

 

}

获得遍历序列

template<classType>

boolBStree<Type>::findpath(BinaryNode<Type> *startNode,vector<int>&path, int key)

{

      if(startNode==NULL)return false;

      path.push_back(startNode->data);

      if(startNode->data==key) return true;

      boolfind=(findpath(startNode->left,path,key)||findpath(startNode->right,path,key));

      if(find) return true;

      path.pop_back();

      return false;

}

获得二叉树两个节点之间的最低公共节点

template<classType>

intBStree<Type>::findLCA(BinaryNode<Type>*startNode,int key1,int key2)

{

      vector<int> path1,path2;

      bool find1=findpath(startNode,path1,key1);

      bool find2=findpath(startNode,path2,key2);

      if(find1&&find2)

      {

             int ans;

             for(int i=0;i<path1.size();i++)

             {

                    if(path1[i]!=path2[i])

                           break;

                    else

                           ans=path1[i];

             }

             return ans;

      }

      return -1;

}

template<classType>

voidBStree<Type>::FindLen(BinaryNode<Type>*startNode,int key1,int key2)

{

      vector<int> path1,path2;

      int len=0;

      bool find1=findpath(startNode,path1,key1);

      bool find2=findpath(startNode,path2,key2);

      if(find1&&find2)

      {

             if(path1.size()>=path2.size())

             {

                    len=path1.size()-path2.size();

                    for(int i=path1.size()-len-1,j=path2.size()-1;j>=0&&i>=0;i--,j--)

                    {

                           if(path1[i]!=path2[j])

                                  distancebetweenNode=distancebetweenNode+1;//74 10 1 5 -1 2 9 13 12 11 14 s

                           else

                                  break;

                    }

             }

             else

             {

                    len=path2.size()-path1.size();

            for(inti=path2.size()-len-1,j=path1.size()-1;j>=0&&i>=0;j--,i--)

                    {

                           if(path1[j]!=path2[i])

                                  distancebetweenNode++;

                           else

                                  break;

                    }

             }

 

             distancebetweenNode=2*distancebetweenNode+len;

 

      }

}

二叉树的深度

template<classType>

intBStree<Type>::treeDepth(BinaryNode<Type>*startNode)

{

      if(startNode==NULL)

             return 0;

      returntreeDepth(startNode->left)>treeDepth(startNode->right)?(treeDepth(startNode->left)+1):(treeDepth(startNode->right)+1);

}

二叉树深度非递归

template<classType>

intBStree<Type>::treeDepthNoRe(BinaryNode<Type>*startNode)

{

 

}

二叉树宽度

template<classType>

intBStree<Type>::treeWidth(BinaryNode<Type>*startNode)

{

      if(startNode==NULL)

             return 0;

      int nLastLevelWidth=0;

      int nTempLastLevelWidth;

      int nCurLevelWidth;

      int nWidth=1;

      queue<BinaryNode<Type>*>myqueue;

      myqueue.push(startNode);//将根节点入队

      nLastLevelWidth=1;

      BinaryNode<Type>*pCur=NULL;

      while(!myqueue.empty())

      {

             nTempLastLevelWidth=nLastLevelWidth;

             while(nTempLastLevelWidth!=0)

             {

                    pCur=myqueue.front();//取出队列头元素

                    myqueue.pop();

                    if(pCur->left!=NULL)

                    {

                           myqueue.push(pCur->left);

                    }

                    if(pCur->right!=NULL)

                    {

                           myqueue.push(pCur->right);

                    }

                    nTempLastLevelWidth--;

             }

             nCurLevelWidth=myqueue.size();

             nWidth=nCurLevelWidth>nWidth?nCurLevelWidth:nWidth;

             nLastLevelWidth=nCurLevelWidth;

     

      }

      return nWidth;

}

判断平衡二叉树

template<classType>

boolBStree<Type>::balanceTree(BinaryNode<Type>*startNode)

{

      int diff,left,right;

 

      if(startNode==NULL)

             return true;

      left=treeDepth(startNode->left);

      right=treeDepth(startNode->right);

      diff=left-right;

      if(diff>1||diff<-1)

             return false;

      else

             returnbalanceTree(startNode->left)&&balanceTree(startNode->right);

 

}

 

int main()

{

 

      BStree<int> tree;

      int num;

      while(cin>>num)

      tree.createBitree(num);

      cout<<"输出递归先序遍历的序列"<<endl;

      tree.preOrder(tree.GetRoot());

      cout<<endl;

      cout<<"输出递归中序遍历的序列"<<endl;

      tree.inOrder(tree.GetRoot());

      cout<<endl;

      cout<<"输出递归后序遍历的序列"<<endl;

      tree.PostOrder(tree.GetRoot());

      cout<<endl;

      cout<<"输出非递归先序遍历的序列"<<endl;

      tree.noRecursivePre(tree.GetRoot());

      cout<<endl;

      cout<<"输出非递归中序遍历的序列"<<endl;

      tree.noRecursiveIn(tree.GetRoot());

      cout<<endl;

      cout<<"输出非递归后序遍历的序列"<<endl;

      tree.noRecursivePost1(tree.GetRoot());

      cout<<endl;

      cout<<"输出非递归后序遍历的序列2"<<endl;

      tree.noRecursivePost2(tree.GetRoot());

      cout<<endl;

      cout<<"输出二叉树节点之间的最大距离:"<<endl;

      tree.FindMaxLen(tree.GetRoot());

      cout<<maxlen<<endl;

      cout<<"输出二叉树两个节点之间的最低公共节点"<<endl;

      cout<<tree.findLCA(tree.GetRoot(),5,9)<<endl;

      cout<<"输出二叉树任意两个节点之间的距离"<<endl;

      tree.FindLen(tree.GetRoot(),11,14);

      cout<<distancebetweenNode<<endl;

      cout<<"输出二叉树的深度"<<endl;

   cout<<tree.treeDepth(tree.GetRoot())<<endl;

      cout<<"输出二叉树的宽度"<<endl;

      cout<<tree.treeWidth(tree.GetRoot())<<endl;

      cout<<"判断是否为平衡二叉树"<<endl;

      cout<<tree.balanceTree(tree.GetRoot())<<endl;

 

    system("pause");

      return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值