java二叉树凹入表_二叉树的建立及凹入表打印.doc

本文介绍了如何使用Java构建二叉树,通过先序递归方式建立二叉链表结构,并提供了计算叶子节点数量的递归算法。此外,还详细讲解了如何实现凹入表方式打印二叉树,便于理解和可视化二叉树结构。
摘要由CSDN通过智能技术生成

二叉树的建立及凹入表打印

先序递归建立二叉树

班级2012211313

姓名:胡卓 学号:2012211468

姓名:郭志刚 学号:2012211475

分工情况:

郭志刚BiTree CreateBiTree(BiTree& T)//先序递归创建二叉树 int main() 胡卓 void Print(BiTree& T,int t) int LEAF(BiTree& T)//计算叶子节点

完成日期:2013-11-6

问题描述:

1)用先序递归过程建立二叉树 (存储结构:二叉链表)

输入数据按先序遍历所得序列输入,当某结点左子树或右子树为空时,输入‘*’号,如输入abc**d**e**得到的二叉树为:

e

a d

b

c

(选做:由二叉树的先序序列和中序序列建立一棵二叉树。)

2)编写递归算法,计算二叉树中叶子结点的数目。

3)按凹入表方式输出该二叉树。

算法思想:

定义二叉树的数据结构类型

typedef struct BiTNode//

{

char data;

struct BiTNode*lchild,*rchild;//左右子树

}BiTNode,*BiTree;

1.先序递归创建二叉树

BiTree CreateBiTree(BiTree& T)

T=(BiTree)malloc(sizeof(BiTNode));

T->data='\0';

char ch;

scanf("%c",&ch);

if(ch!='*')

{

T->data=ch;

T->lchild=CreateBiTree(T->lchild);

T->rchild=CreateBiTree(T->rchild);

}

else if(ch=='*')

return NULL;

if((T->lchild==NULL&&T->rchild==NULL)||(T->lchild==NULL&&T->rchild->data!='\0')||(T->lchild->data!='\0'&&T->rchild==NULL)||(T->lchild->data!='\0'&&T->rchild->data!='\0'))

return T;

}

计算叶子节点

int LEAF(BiTree& T)

{

if(T->lchild==NULL&&T->rchild==NULL)

return 1;

else if(T->lchild==NULL&&T->rchild->data!='\0')

return LEAF(T->rchild);

else if(T->lchild->data!='\0'&&T->rchild==NULL)

return LEAF(T->lchild);

else

return (LEAF(T->lchild)+LEAF(T->rchild));

}

3.凹入表打印二叉树

void Print(BiTree& T,int t)

{

int i;

if(T)

{

Print(T->rchild,t+5);

for(i=0;i

{

printf(" ");

}

printf("%5c\n",T->data);

Print(T->lchild,t+5);

}

}

4.主函数

int main()

{

BiTree T;

printf("*******************************************\n");

printf("****************欢迎打印二叉树*************\n");

printf("*******************************************\n");

printf("请按先序遍历所得数据输入(当某节点是叶子节点时用'*'表示):\n")

以下是实现二叉树凹入表形式横向打印的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` 函数用于创建二叉树,这里使用了一个示例二叉树进行测试,你可以根据需要修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值