数据结构-----凹入表形式横向打印二叉树结构 (附代码+注释)

数据结构的学习中最最基本的实验之——打印二叉树结构

代码生成效果如下:

 

 

(一)需求分析

1.打印二叉树的程序中,输入数据的类型限定为字符型,并且以“回车符”为结束标志。用户默认以中序序列输入字符串,由程序转化为二叉树结构,并以凹入表示法来打印出二叉树。

2.演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后,由用户在键盘上输入本程序中规定的运算命令;相应的输入数据和运算结果显示在其后。

3.该程序执行的命令包括:

定义二叉链表的类型、定义二叉链表的结点结构、建立二叉树、遍历二叉树、计算二叉树的深度、打印二叉树以及主函数部分

4.测试数据:

输入:AB D CE  F

输出:  C

                F

            E

A

            D

        B

二叉树抽象数据类型定义

ADT BinaryTree{

数据对象:一个集合D,该集合中的所有元素具有相同的特性

数据关系:

若D为空集则称BinaryTree为空二叉树;若D中仅含有一个数据元素,则R为空集,R={H},H是如下的二元关系:

1.若在D中存在的唯一存在的称为根的数据元素root,它在关系H下没有前驱。

2.除root以外,D中每个节点在关系H下都且仅有一个前驱。

基本操作:

1.Initiate(BiTree):

操作结果:将bt初始化为空二叉树

2.Create(BiTree):

操作结果:创建一棵非空二叉树bt

3.Destory(BiTree):

操作结果:销毁二叉树bt

4.Empty(BiTree):

操作结果:若bt为空则返回true,否则返回false

5.Root(BiTree):

操作结果:求二叉树bt的根节点。若为空二叉树则返回null

6.Parent(BiTree,x):

操作结果:求双亲函数,求二叉树bt中结点x的双亲结点。若结点x是二叉树的根节点或无根节点,则返回null

7.Leftchild(BiTree,x):

操作结果:求左孩子

8.Rughtchild(BiTree,x):

操作结果:求右孩子

9.Traverse(BiTree):

操作结果:遍历操作

10.Clear(bt):

操作结果:清除操作

Createbitree(&T,defination)构造二叉树

Preordertraverse(T)先序遍历

Inordertraverse(T)中序遍历

Postordertraverse(T)后序遍历 

本程序主要分为六个模块

1)主程序模块

2) 定义二叉链表存储结构模块

3)建立二叉树模块

4)遍历二叉树模块

5)计算二叉树深度模块

6)打印二叉树模块

其中,模块之间的调用关系如下图:

主程序模块

定义二叉链表存储结构模块
 
建立二叉树模块
 

遍历二叉树模块
 
计算二叉树深度模块
 
打印二叉树模块

 ①定义二叉链表的存储结构

typedef struct BiTNode
{
    TElemType data;
    struct BiTNode *lchild,*rchild;
} BiTNode, *BiTree;

②先序建立二叉树

void CreateBiTree(BiTree *T)
{
    char ch;
    scanf("%c",&ch);
    if (ch==' ')
    *T=NULL;
    else
      { *T=(BiTree)malloc(s izeof(BiTNode));
       (*T)->data=ch;
        CreateBiTree(&(*T)->lchild);
        CreateBiTree(&(*T)->rchild);
        }
}

③RDL遍历二叉树

void RDLTraverse(BiTree T)
{
    if (T)
      {
          RDLTraverse(T->rchild);
          printf("%2c",T->data);
          RDLTraverse(T->lchild);
        }
}

④计算二叉树的深度

int BiTreeDepth(BiTree T)
{
    int h1,h2,h;
    if (T==NULL)
     return 0;
    else
      {
        h1=BiTreeDepth(T->lchild);
        h2=BiTreeDepth(T->rchild);
        if (h1>h2)
        h=h1+1;
        else
        h=h2+1;
        }
     return h;
}

⑤打印二叉树

void print(BiTree T,int n)
{
    int i;
    if(T)
      {
        print(T->rchild,n+1);
        for(i=0;i<n;i++)
        {
         printf("\t");
         }
         printf("%2c\n",T->data);
         print(T->lchild,n+1);
       }
}

⑥主函数

void main()
{
    BiTree T;
    printf("请按先序输入一棵二叉树(如:AB#D##CE#F###):\n"); CreateBiTree(&T);
    printf("\nRDL遍历:");   RDLTraverse(T);
    printf("\nDepth=%d",BiTreeDepth(T));
    printf("\n");
    printf("以RDL输出打印结果:\n");print(T,0);
    printf("\n");
}

完整代码+注释(可自取,但未经同意请勿转载)

#include <stdio.h>
#include <stdlib.h>
/*定义二叉链表*/
typedef char TElemType;
/*定义二叉链表的存储结构*/
typedef struct BiTNode
{
    TElemType data;
    struct BiTNode *lchild,*rchild;
} BiTNode, *BiTree;
/*先序建立二叉树*/
void CreateBiTree(BiTree *T)
{
    char ch;
    scanf("%c",&ch);
    if (ch==' ')
    *T=NULL;
    else
      { *T=(BiTree)malloc(sizeof(BiTNode));
       (*T)->data=ch;
        CreateBiTree(&(*T)->lchild);
        CreateBiTree(&(*T)->rchild);
        }
}
/*RDL遍历二叉树*/
void RDLTraverse(BiTree T)
{
    if (T)
      {
          RDLTraverse(T->rchild);
          printf("%2c",T->data);
          RDLTraverse(T->lchild);
        }
}
/*计算二叉树的深度*/
int BiTreeDepth(BiTree T)
{
    int h1,h2,h;
    if (T==NULL)
     return 0;
    else
      {
        h1=BiTreeDepth(T->lchild);
        h2=BiTreeDepth(T->rchild);
        if (h1>h2)
        h=h1+1;
        else
        h=h2+1;
        }
     return h;
}
/*打印二叉树*/
void print(BiTree T,int n)
{
    int i;
    if(T)
      {
        print(T->rchild,n+1);
        for(i=0;i<n;i++)
        {
         printf("\t");
         }
         printf("%2c\n",T->data);
         print(T->lchild,n+1);
       }
}
void main()
{
    BiTree T;
printf("请按先序输入一棵二叉树(如:AB#D##CE#F###):\n");
CreateBiTree(&T);
    printf("\nRDL遍历:");   RDLTraverse(T);
    printf("\nDepth=%d",BiTreeDepth(T));
    printf("\n");
    printf("以RDL输出打印结果:\n");print(T,0);
    printf("\n");
}

程序编译运行后,根据提示输入各结点信息(一定要按先序输入!)

注意叶子结点都要有,如果没有,须添加一些虚结点,用#代替

打印二叉树到这里就结束啦~如果对你有帮助,记得点赞赞噢~

  • 33
    点赞
  • 165
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
以下是实现二叉树凹入表形式横向打印的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_DEPTH 100 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 计算树的深度 int calcDepth(TreeNode *root) { if (root == NULL) { return 0; } int leftDepth = calcDepth(root->left); int rightDepth = calcDepth(root->right); return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; } // 按凹入表形式打印二叉树 void printTree(TreeNode *root) { if (root == NULL) { return; } int depth = calcDepth(root); int maxWidth = (1 << depth) - 1; char **res = (char **)malloc(sizeof(char *) * depth); for (int i = 0; i < depth; i++) { res[i] = (char *)malloc(sizeof(char) * (maxWidth + 1)); memset(res[i], ' ', maxWidth); res[i][maxWidth] = '\0'; } // 递归填充凹入表 void fillTable(TreeNode *node, int depth, int pos, int offset) { if (node == NULL) { return; } int idx = pos + offset; res[depth][idx] = node->val + '0'; fillTable(node->left, depth + 1, pos, offset / 2); fillTable(node->right, depth + 1, idx + 1, offset / 2); } fillTable(root, 0, 0, maxWidth / 2); // 打印凹入表 for (int i = 0; i < depth; i++) { printf("%s\n", res[i]); free(res[i]); } free(res); } // 创建二叉树 TreeNode *createTree(int *arr, int size, int pos) { if (pos >= size || arr[pos] == 0) { return NULL; } TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = arr[pos]; root->left = createTree(arr, size, pos * 2 + 1); root->right = createTree(arr, size, pos * 2 + 2); return root; } int main() { int arr[] = {1, 2, 3, 0, 5, 6, 7}; // 示例二叉树 int size = sizeof(arr) / sizeof(arr[0]); TreeNode *root = createTree(arr, size, 0); printTree(root); // 按凹入表形式打印二叉树 return 0; } ``` 其中,`calcDepth` 函数计算树的深度,`printTree` 函数按凹入表形式打印二叉树,`fillTable` 函数递归填充凹入表。`createTree` 函数用于创建二叉树,这里使用了一个示例二叉树进行测试,你可以根据需要修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q小Q琪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值