扩展先序遍历序列创建二叉链表与三种遍历

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DataType char
#define MAXSIZE 1000
int count = 0;  //统计节点数

typedef struct Node
{
    DataType data;
    struct Node * Lchild;
    struct Node * Rchild;
    // struct Node * Parent;
} BiTNode,* BiTree;
 
 void CreateBiTree(BiTree * root)
 {
     char ch;
     ch = getchar();
     if(ch == ' ')          //空格代表为空
        * root = NULL;
     else
     {
        *root = (BiTree)malloc(sizeof(BiTNode));
        (*root)->data = ch;
        CreateBiTree(&((*root)->Lchild));
        CreateBiTree(&((*root)->Rchild));
     }
 }
 
 void PreOrder(BiTree root) //先序
 {
     if(root)
     {
         putchar(root->data);
         PreOrder(root->Lchild);
         PreOrder(root->Rchild);
     }
 }
 void InOrder(BiTree root)  //中序
 {
     if(root)
     {
         InOrder(root->Lchild);
         putchar(root->data);
         InOrder(root->Rchild);
     }
 }
 void PostOrder(BiTree root)    //后序
 {
     if(root)
     {
         PostOrder(root->Lchild);
         PostOrder(root->Rchild);
         putchar(root->data);
         count++;       //后序遍历同时计节点数
     }
 }
int leaf(BiTree root)  //输出叶子节点和统计叶子节点个数
{
    if(root == NULL) return 0;
    if(!root->Lchild && !root->Rchild) 
    {
        putchar(root->data);
        return 1;    //当前节点无孩子,为叶子节点
    }
    return leaf(root->Lchild) + leaf(root->Rchild);
}
int TreeDepth(BiTree root)  //求树高度
{
    int h,lh,rh;
    if(root == NULL) return 0;
    lh = TreeDepth(root->Rchild);
    rh = TreeDepth(root->Lchild);
    h = (lh>rh ? lh : rh) + 1;
    return h;
}
void PrintfTree(BiTree root, int h) //按树状打印二叉树,先右后左中序遍历树
{                                   //结点的横向位置有结点在树中的层次决定,h表示结点的层次,控制结点输出左右位置
    if(root == NULL) return;
    PrintfTree(root->Rchild,h+4);   //每个层次相隔4个空格
    for (int i = 0; i < h; i++) 
        printf(" ");
    printf("%c\n",root->data);
    PrintfTree(root->Lchild,h+4);
}

int main(int argc, char **argv) {

    int LeafNum,Depth;
    BiTree Tree;

    printf("按先序输入建立树:");
    CreateBiTree(&Tree);
    
    printf("先序:");
    PreOrder(Tree);
    
    printf("\n中序:");
    InOrder(Tree);
    
    printf("\n后序:");
    PostOrder(Tree);
    
    printf("\n叶子节点:");
    LeafNum = leaf(Tree);
    printf("\n节点数:%d",count);
    printf("    叶子节点数为%d",LeafNum);
    
    Depth = TreeDepth(Tree);
    printf("\n树高度为:%d\n",Depth);
    
    PrintfTree(Tree,1);
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值