• 二叉树的建立和遍历的各种问题


    链表声明:

    //基本结构声明
    
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<cstdio>
    
    #define NoInfo 0        // 0表示没有结点 
    using namespace std;
    typedef int ElementType;
    typedef struct TNode* Position;
    typedef Position BinTree;
    //二叉树链表结构 
    struct TNode{
        ElementType Data;    //结点数据 
        BinTree Left;        //左子树 
        BinTree Right;        //右子树 
    };  
    View Code

    1.先序建立二叉树

    //先序建立二叉树
    BinTree CreateBinTree(){
        ElementType data;
        scanf("%d",&data);
        if(data==NoInfo) return NULL;
           BinTree P;
           P=new TNode;
        P->Data=data;
        P->Left=CreateBinTree();
        P->Right =CreateBinTree();
        return P;
    }
    View Code

    2.层序建立二叉树

    //层序建立二叉树
    BinTree CreateBinTree(){
        ElementType data;
        BinTree BT,T;
        queue<BinTree> q;//创建队列
        scanf("%d",&data);
        if(data!=NoInfo){
            BT=new TNode;
            BT->Data =data;
            BT->Left =BT->Right =NULL;
            q.push(BT);
        }
        else return NULL;//第一个数据为0则返回空树
        
        while(!q.empty()){
            T=q.front();//取出结点 
            q.pop();
            scanf("%d",&data);//读入T的左孩子 
            if(data==NoInfo) T->Left =NULL;
            else{
                T->Left =new TNode;
                T->Left ->Data=data;
                T->Left ->Left=T->Left ->Right=NULL;
                q.push(T->Left );
            }
            scanf("%d",&data);//读入T的右孩子 
            if(data==NoInfo) T->Right =NULL;
            else{
                T->Right =new TNode;
                T->Right ->Data=data;
                T->Right ->Left=T->Right ->Right=NULL;
                q.push(T->Right );
            }
        }
        return BT; 
    } 
    View Code

    3.递归先序遍历

    void PreorderTraversal_1(BinTree BT){
        if(BT){
            printf("%d",BT->Data );
            PreorderTraversal_1(BT->Left );
            PreorderTraversal_1(BT->Right );
        }
    }
    View Code

    4.非递归先序遍历

    void PreorderTraversal_2(BinTree BT){
        BinTree T=BT;
        stack<BinTree> s;
        while(T||!s.empty()){
            while(T){
                printf("%d",T->Data );
                s.push(T);
                T=T->Left ;
            }
            T=s.top();
            s.pop();
            T=T->Right ;
        }
    }
    View Code

    5.递归中序遍历

    void InorderTraversal_1(BinTree BT){
        if(BT){
            InorderTraversal_1(BT->Left );
            printf("%d",BT->Data );
            InorderTraversal_1(BT->Right );
        }
    }
    View Code

    6.非递归中序遍历

    //非递归中序遍历
    void InorderTraversal_2(BinTree BT){
        BinTree T=BT;
        stack<BinTree> s;
        while(T||!s.empty()){
            while(T){
                s.push(T);
                T=T->Left ;
            }
            T=s.top();
            s.pop();
            printf("%d",T->Data );
            T=T->Right ;
        }
    } 
    View Code

    7.递归后序遍历

    //递归后序遍历
    void PostorderTraversal_1(BinTree BT){
        if(BT){
            PostorderTraversal_1(BT->Left );
            PostorderTraversal_1(BT->Right );
            printf("%d",BT->Data );
        }
    }
    View Code

    8.非递归后序遍历

    //非递归后序遍历
    void PostorderTraversal_2(BinTree BT){
        BinTree T,Pre=NULL;
        stack<BinTree> s;
        s.push(BT);
        while(!s.empty()){
            T=s.top();
            if( (T->Left ==NULL&&T->Right ==NULL) || 
            (Pre!=NULL&& (T->Left==Pre ||T->Right==Pre ) ) ){
                printf("%d",T->Data );
                s.pop();
                Pre=T;
            }
            else{
                if(T->Right !=NULL) s.push(T->Right );
                if(T->Left !=NULL) s.push(T->Left );
            }
        }
    } 
    View Code

    9.层序遍历

    void LevelorderTravelsal(BinTree BT){
        queue<BTree> q;
        BinTree T;
        if(!BT) return;
        q.push(BT);
        while(!q.empty()){
            T=q.front();
            q.pop();
            printf("%d ",T->Data);
            if(T->Left ) q.push(T->Left );
            if(T->Right ) q.push(T->Right );
        }
    }
    View Code

    10.二叉树高度

    //二叉树高度
    int GetHeight(BinTree BT){
        if(BT) return max(GetHeight(BT->Left),GetHeight(BT->Right ))+1;
        else return 0;//空树高度为0 
    }
    View Code

    11.求二叉树所有结点

    //求二叉树所有结点
    int    CountNode(BinTree BT){
        if(BT)    return CountNode(BT->Left )+CountNode(BT->Right )+1;
        else return 0;
    } 
    View Code

    12.求二叉树叶子结点

    void LeaveCount(BinTree BT){
        if(BT){
            if(BT->Left ==NULL&&BT->Right ==NULL)
                count++;
            LeaveCount(BT->Left );
            LeaveCount(BT->Right );    
        }
    }
    View Code
    int LeafcountofBinTree(BinTree BT){
        if(BT){
            if(BT->Left ==NULL&&BT->Right ==NULL)
                return LeafcountofBinTree(BT->Left )+LeafcountofBinTree(BT->Right )+1;
        }
        else return 0;
    }
    View Code

    13.先序输出二叉树叶子结点 

    void PreorderPrintLeaves(BinTree BT){
        if(BT!=NULL){
            if((BT->Left==NULL)&&(BT->Right==NULL))
                printf(" %c",BT->Data);
            PreorderPrintLeaves(BT->Left);
            PreorderPrintLeaves(BT->Right);
        }
    }
    View Code

    14.镜面反转,将所有非叶结点的左右孩子对换 

    void Inversion(BinTree BT){
        if(BT){
            Inversion(BT->Left);
            Inversion(BT->Right);
            BinTree temp;
            temp=BT->Left ;
            BT->Left =BT->Right ;
            BT->Right =temp;
        }
    }
    View Code

    15.销毁二叉树

    void DestroyBinTree(BinTree BT){
        if(BT){
            DestroyBinTree(BT->Left );
            DestroyBinTree(BT->Right );
            delete BT;
            BT=NULL;
        }
    }
    View Code

    16.根据前序和中序遍历还原二叉树 

     1. 根据前序序列的第一个元素建立根结点;
    2. 在中序序列中找到该元素,确定根结点的左右子树的中序序列;
    3. 在前序序列中确定左右子树的前序序列;
    4. 由左子树的前序序列和中序序列建立左子树;
    5. 由右子树的前序序列和中序序列建立右子树。

    //根据前序和中序遍历还原二叉树 
    BinTree PreInoRestoreBinTree(int* inorder ,int* preorder, int length)
    {
      if(length==0) return NULL;
      BinTree T=new TNode;
      int rootIndex;
      T->Data  = *preorder;
      for(rootIndex=0;rootIndex < length; rootIndex++)
          if(inorder[rootIndex] == *preorder)
              break;
      T->Left = PreInoRestoreBinTree(inorder,preorder +1 , rootIndex);
      T->Right = PreInoRestoreBinTree(inorder + rootIndex + 1 ,preorder + rootIndex + 1 , length - (rootIndex + 1));
      return T;
    }
    View Code

    17.根据后序和中序遍历还原二叉树 

     1. 根据后序序列的最后一个元素建立根结点;
    2. 在中序序列中找到该元素,确定根结点的左右子树的中序序列;
    3. 在后序序列中确定左右子树的后序序列;
    4. 由左子树的后序序列和中序序列建立左子树;
    5. 由右子树的后序序列和中序序列建立右子树

    BinTree InoPosRestoreBinTree(int *inorder,int *postorder,int length)
    {
        if(length==0) return NULL;
        BinTree T;
        int rootindex;
        T =new TNode;
        T->Data=postorder[length-1];
        T->Left=T->Right=NULL;
        for(rootindex=0; rootindex < length; rootindex++)
            if(inorder[rootindex] == postorder[length-1])
                break;
        T->Left =InoPosRestoreBinTree(inorder,postorder,rootindex);
        T->Right =InoPosRestoreBinTree(inorder+rootindex+1,postorder+rootindex,length-(rootindex+1));
        return T;
    }
    View Code

     17.1 根据中序和后序输出前序

    #include<iostream>
    #include<vector>
    #include<string>
    using namespace std;
    string in, post, ans="";
    void pre(int root, int l, int r){
        if(l>r) return ;
        int i=l;
        while(in[i] != post[root]) i++;
        //printf("%d %d %d %d
    ",root,l,r,i);
        ans += post[root];
        //cout<<"L"<<endl;
        pre(root-r+i-1, l, i-1);
        //cout<<"R"<<endl;
        pre(root-1, i+1, r);
    }
    int main(){
        cin>>in>>post;
        pre(post.size()-1,0,post.size()-1);
        cout<<ans<<endl;
        return 0;
    }
    View Code

     

     

  • 相关阅读:
    OpenGL实现通用GPU计算概述
    Android Camera API/Camera2 API 相机预览及滤镜、贴纸等处理
    OpenGL中的Shader
    Android平台Camera实时滤镜实现方法探讨(三)--通过Shader实现YUV转换RBG
    GPU:并行计算利器
    双摄像头测距的OpenCV实现
    Android Camera 通过V4L2与kernel driver的完整交互过程
    图像缩放算法
    双camera景深计算 (1)
    error: ‘shared_ptr’ in namespace ‘std’ does not name a type
  • 原文地址:https://www.cnblogs.com/astonc/p/9906518.html
Copyright © 2020-2023  润新知