数据结构 C语言 链式二叉树
来源:互联网 发布:三菱q系列plc编程手册 编辑:程序博客网 时间:2024/05/29 17:24
【问题描述】
采用二叉链表作为二叉树的存储结构实现各项功能
【任务要求】
(1) 输入二叉树的先序序列,建立二叉树;
(2) 用程序实现二叉树的中序遍历;
(3) 编写程序求二叉树的深度;
【测试数据】
测试数据:(1)输入先序遍历-+a##*b##-c##d##/e##f##;查看其中序遍历、后序遍历和该二叉树的深度。
(2)输入先序遍历ab#d##ce###;查看其中序遍历、后序遍历和该二叉树的深度。
#include<stdio.h>#include<stdlib.h>int depth = 0;typedef struct node{ char ch; struct node *lchild, *rchild;}linkTree;linkTree *createTree(){ linkTree *t; char temp; scanf("%c", &temp); if (temp == '#') { t = NULL; } else { t = (linkTree*)malloc(sizeof(linkTree)); t->ch = temp; t->lchild = createTree(); t->rchild = createTree(); } return t;}int preOrder(linkTree *t){ if (t != NULL) { printf("%c", t->ch); preOrder(t->lchild); preOrder(t->rchild); }}int inOrder(linkTree *t){ if (t != NULL) { inOrder(t->lchild); printf("%c", t->ch); inOrder(t->rchild); }}int postOrder(linkTree *t){ if (t != NULL) { postOrder(t->lchild); postOrder(t->rchild); printf("%c", t->ch); }}int treeDeep(linkTree *t, int level){ if (t) { if (level > depth) { depth = level; } treeDeep(t->lchild, level + 1); treeDeep(t->rchild, level + 1); }}int main(){ linkTree *t = createTree(); int level = 1; printf("\n"); preOrder(t); printf("\n"); inOrder(t); printf("\n"); postOrder(t); printf("\n"); treeDeep(t, level, &depth); printf("depth:%d", depth); system("pause"); return 0;}
阅读全文
0 0
- 数据结构 C语言 链式二叉树
- 数据结构-二叉树 链式 c
- 数据结构C语言实现之二叉树链式结构
- 链式二叉树的C语言代码
- 数据结构——二叉树的链式实现(C语言)
- 数据结构--链式栈(C语言)
- 链式栈 C语言数据结构
- C语言-数据结构-二叉树
- 数据结构_6:树:C语言_链式存储
- 数据结构实现链式队列(C语言)
- 数据结构-链式栈-C语言实现
- 数据结构之---C语言实现链式队列
- c语言数据结构实现-链式队列
- 二叉树排序 --C语言数据结构
- C语言数据结构之二叉树
- (C语言)二叉树实现(数据结构十三)
- [数据结构]C语言二叉树的实现
- 数据结构-------------线索二叉树(c语言)
- 实验报告6_MPEG音频编码
- MPI基础
- 1147: 查找子数组
- JavaWeb: log4j日志框架的使用
- 究竟什么是DevOps Intelligence?
- 数据结构 C语言 链式二叉树
- Unity 动画结束事件
- 基于Node.js,Express,Socket.io创建简单聊天室
- Macos终端获root权限
- codeforces 822D
- 二分查找、二叉搜索
- JDBC技术连接数据库
- #pragma once与#ifndef两种防止头文件二次编译的区别
- 解析表字段内容为Gzip格式的数据