根据二叉树的前中后序序列构造二叉树

#include<stdio.h>  
#include <iostream>  
#include <fstream>
#include <string>

struct TreeNode
{
  struct TreeNode* left;
  struct TreeNode* right;
  char  elem;
};

//根据前序和中序构造二叉树
void  BinaryTreeFromOrderings(char* inorder, char* preorder, int length)
{
  if(length == 0)
    {
          return;
    }
  TreeNode* node = new TreeNode;//Noice that [new] should be written out.
  node->elem = *preorder;
  int rootIndex = 0;
  for(;rootIndex <length; rootIndex++)
    {
      if(inorder[rootIndex] == *preorder)
      break;
    }
 
  BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);
  //Right
  BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));
  printf("%c", node->elem);    
  return;
}
//根据中序和后序构造二叉树
TreeNode*  BinaryTreeFromPostOrderings(char* inorder, char* aftorder, int length)
{
  if(length == 0)
    {
        return NULL;
    }
    TreeNode* node = new TreeNode;//Noice that [new] should be written out.
    node->elem = *(aftorder+length-1);
   // std::cout<<node->elem<<std::endl;
    printf("%c", node->elem);
    int rootIndex = 0;
    for(;rootIndex < length; rootIndex++)//a variation of the loop
    {
        if(inorder[rootIndex] ==  *(aftorder+length-1))
            break;
    }
    node->left = BinaryTreeFromPostOrderings(inorder, aftorder , rootIndex);
    node->right = BinaryTreeFromPostOrderings(inorder + rootIndex + 1, aftorder + rootIndex , length - (rootIndex + 1));
    
    return node;
}

//统计叶子节点个数
void countLeaf(TreeNode* root,int &count){
    if(root!=NULL){

        if(root->left==NULL&&root->right==NULL){
             count++;
        }
        countLeaf(root->left,count);
        countLeaf(root->right,count);

    }
}

//判断二叉树是否为排序二叉树
bool judge(TreeNode* root){
    if(root==NULL){
        return true;
    }

    TreeNode* tleft=root->left;
    TreeNode* tright=root->right;
    bool leftf=false,rightf=false,rootleft=false,rootright=false;
    if(tleft==NULL||(tleft&&tleft->elem<=root->elem)){
        rootleft=true;
    }

    if(tright==NULL||(tright&&tright->elem<=root->elem)){
        rootright=true;
    }

    leftf=judge(tleft);
    rightf=judge(tright);

    return (leftf && rightf && rootleft && rootright);
}

int main()
{
   
    char* pr="GDAFEMHZ";
    char* in="ADEFGHMZ";
    char* post="AEFDHZMG";
    printf("前序:%s\n",pr);
    printf("中序:%s\n",in);
    printf("根据前序和中序序列输出后序序列\n");
    BinaryTreeFromOrderings(in, pr, 8);
    printf("\n");

    printf("中序:%s\n",in);
    printf("后序:%s\n",post);
    printf("根据中序和后序序列输出后序序列\n");
   TreeNode *root= BinaryTreeFromPostOrderings(in,post,8);
    printf("\n");

    int count=0;
    countLeaf(root,count);
    printf("叶节点个数:%d \n",count);

    bool f=judge(root);
    if (f){
        printf("该二叉树是否为排序二叉树:yes \n");
    }else printf("该二叉树是否为排序二叉树:no \n");


    return 0;
}

转载于:https://my.oschina.net/ye39zi/blog/714741

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值