软基作业——先序遍历法生成二叉树
2013-10-30 17:23
218 查看
题目为:
程序如下:
运行效果如下:
程序如下:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct bnode_type { char data; struct bnode_type *leftchild; struct bnode_type *rightchild; }bnodetype; void input(bnodetype **root); bool memorytest(bnodetype *node); void preorder(bnodetype *root); int main(void) { bnodetype *root; root= NULL; input(&root); printf("The tree is\n"); preorder(root); printf("\n"); return 0; } bool memorytest(bnodetype *node) { if (node == NULL) exit(EXIT_FAILURE); return true; } void input(bnodetype **root) { char data; if (*root == NULL) { data = getchar(); if (data == ' ' || data == '\n') return ; *root = (bnodetype *)malloc(sizeof(bnodetype)); memorytest(*root); (*root)->data = data; (*root)->leftchild = NULL; (*root)->rightchild = NULL; } if ((*root)->leftchild == NULL) input(&(*root)->leftchild); if ((*root)->rightchild == NULL) input(&(*root)->rightchild); } void preorder(bnodetype *root) { if (root != NULL) { printf("%c", root->data); preorder(root->leftchild); preorder(root->rightchild); } }
运行效果如下:
相关文章推荐
- 水一个:组合数学生成作业的程序,算法很垃圾,有兴趣的帮我改一下啊
- 用Java生成随机检查作业的程序
- 生成二叉树以及遍历(简单易懂)
- 利用C++模板生成随机二叉树
- 数据结构作业——brothers(二叉树)
- 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序
- 根据二叉树的前序数组和中序数组生成二叉树
- 中缀表达式生成二叉树
- 证明SJF(Shortest Job First:最短作业优先算法)策略生成的调度方案理论最优
- 层序生成二叉树
- (修改)个人作业1——四则运算题目生成程序(基于控制台)
- 个人作业1--四则运算题目生成程序
- 个人作业1——四则运算题目生成程序(基于控制台)
- Java面试作业,给一个整型数组,要求算出最多相同value的sum,类似二叉树搜索;
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- C#第三次作业:在c#中导入excel,并生成html文件
- 二叉树的生成
- 作业2设计一个随机生成小学四则运算的程序
- 根据扩展二叉树前序序列生成二叉树,输入某个节点的值,查找其双亲节点的值(二叉链表)
- 二叉树——数据结构课堂作业