重建二叉树

</pre><pre name="code" class="cpp">#include<iostream>
#include<queue>
using namespace std;
typedef struct  BTree{  
    int data;  
    BTree * leftchild;  
    BTree * rightchild;  
}BTNode;  
//重建树的递归算法  
BTNode * ConstructCore(int * preStart, int * preEnd, int * inStart, int * inEnd){  
    int value = preStart[0];
    BTNode * root = new BTNode();  
    root->data = value;  
    root->leftchild =NULL;
    root->rightchild=NULL; 
    if(preStart==preEnd){
        if(inStart==inEnd && *preStart==*inStart)  
            return root;
        else
            throw std::exception();  
    }
    int * rootIndex = inStart;  
    while(*rootIndex != value && rootIndex <= inEnd)  
        rootIndex++;  
    if(*rootIndex != value && rootIndex==inEnd)
        throw std::exception();
    int left_len = rootIndex - inStart;  
    int *left_pre_end = preStart + left_len;  
    if(left_len>0)//左子树  
        root->leftchild = ConstructCore(preStart+1, left_pre_end, inStart, rootIndex -1);  
    if(left_len < preEnd-preStart)//右子树  
        root->rightchild = ConstructCore(left_pre_end +1, preEnd, rootIndex+1, inEnd);  
    return root;  
}  
//重建树  
BTree * Construct(int * Pre, int * In, int length){  
    if(length<=0 || Pre==NULL || In==NULL)  
        return NULL;  
    return ConstructCore(Pre, Pre+length-1, In, In+length-1);  
}  
/*先序遍历*/
void PreviousOrder(BTree *root){
     if(root==NULL)
         return;
     cout<<root->data<<" ";
     PreviousOrder(root->leftchild);
     PreviousOrder(root->rightchild);
}
/*中序遍历*/
void MiddleOrder(BTree *root){
     if(root==NULL)
         return;
     PreviousOrder(root->leftchild);
     cout<<root->data<<" ";
     PreviousOrder(root->rightchild);
}
/*后序遍历*/  
void PostOrder(BTree *root)  
{  
    if(root == NULL)  
        return;  
    //左子树  
    if(root->leftchild != NULL)  
        PostOrder(root->leftchild);  
    //右子树  
    if(root->rightchild != NULL)  
        PostOrder(root->rightchild);  
    //根  
    cout<<root->data<<' ';  
}  
/*层序遍历*/
void LevelTraverse(BTree *root){
     if(root==NULL)
        return;
     queue<BTNode*> q;
     q.push(root);
     while(!q.empty()){
         BTNode * pNode = q.front();
         cout<<q.front()->data<<' ';
         q.pop();
         if(pNode->leftchild != NULL)
             q.push(pNode->leftchild);
         if(pNode->rightchild != NULL)
             q.push(pNode->rightchild); 
     }
     return;
} 
/*二叉树层数*/
int TreeDepth(BTree *root){
     if(root==NULL)
         return 0;
     return TreeDepth(root->leftchild) > TreeDepth(root->rightchild) ? (TreeDepth(root->leftchild)+1) : (TreeDepth(root->rightchild)+1);
}
/*叶子节点个数*/
int LeafNodeNum(BTree *root){
    if(root==NULL)
        return 0;
    if(root->leftchild==NULL && root->leftchild==NULL)
        return 1;
    int leftLeaf = LeafNodeNum(root->leftchild);//左子树叶节点个数 
    int rightLeaf= LeafNodeNum(root->rightchild);//右子树叶节点个数 
    return leftLeaf+rightLeaf;
} 
/*二叉树第K层节点个数*/
int GetKthLevelNode(BTree *root,int k){
    if(root==NULL || k < 1)
        return 0;
    if(k==1)
        return 1;
    int leftNum = GetKthLevelNode(root->leftchild,k-1);//左孩子第k-1层节点个数 
    int rightNum= GetKthLevelNode(root->rightchild,k-1);//右孩子第k-1层节点个数 
    return leftNum+rightNum;
} 
/*二叉树的镜像*/
BTree * MirrorTree(BTree *root){
      if(root==NULL)
          return NULL;
      BTree * leftMirror = MirrorTree(root->leftchild);
      BTree * rightMirror= MirrorTree(root->rightchild);
      root->leftchild = rightMirror;
      root->rightchild = leftMirror;
      return root;
} 
int main(){
    int Pre[8] = {1,2,4,7,3,5,6,8};
    int In[8] =  {4,7,2,1,5,3,8,6};
    BTree* root = Construct(Pre, In, 8);
    PostOrder(root);
    cout<<endl;
    PreviousOrder(root);
    cout<<endl;
    MiddleOrder(root);
    cout<<endl;
    LevelTraverse(root);
    cout<<endl;
    cout<<"Tree Depth is: "<<TreeDepth(root)<<endl;
    cout<<"Num of Tree's leaves are: "<<LeafNodeNum(root)<<endl;
    cout<<"第4层的节点个数为:"<<GetKthLevelNode(root, 4)<<endl; 
    MirrorTree(root);
    LevelTraverse(root);
    cout<<endl;
    PreviousOrder(root);
    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值