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

链表声明:

//基本结构声明

#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\n",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

 

 

 

转载于:https://www.cnblogs.com/astonc/p/9906518.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值