严蔚敏 数据结构习题6.62
2015-11-25 13:05
357 查看
对以孩子-兄弟链表表示的树编写计算深度的算法。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <malloc.h> using namespace std; typedef struct tnode { int data; struct tnode *child,*brother; }tnode,*bitree; void creattree(bitree &bt) { char ch; scanf("%c",&ch); getchar(); if(ch=='#') bt=NULL; else { bt=(tnode*)malloc(sizeof(tnode)); bt->data=ch; printf("输入%c的孩子结点:",ch); creattree(bt->child); printf("输入%c的兄弟结点:",ch); creattree(bt->brother); } } int tdepth(bitree t) { int h1,h2; if(!t) { return 0; } else { h1=1+tdepth(t->child); h2=tdepth(t->brother); return h1>h2?h1:h2; } } int main() { bitree bt; int k; bt=(tnode*)malloc(sizeof(tnode)); printf("输入根节点:"); creattree(bt); k=tdepth(bt); printf("%d\n",k); return 0; }
相关文章推荐
- 严蔚敏 数据结构习题 6.52
- 严蔚敏 数据结构习题 6.49
- 严蔚敏 数据结构习题6.47
- 严蔚敏 数据结构习题 6.41
- 数据结构 — 7.有向图的创建及出入度的计算
- 线索二叉树的遍历(数据结构)
- 模式-算法-数据结构
- 《数据结构与算法》学习之入门篇
- 数据结构实验之图论六:村村通公路
- javascript数据结构之二叉搜索树实现方法
- javascript数据结构之双链表插入排序实例详解
- 数据结构实验之链表八:Farey序列
- 数据结构实验之串三:KMP应用
- 数据结构--红黑树(java)
- 【数据结构与算法】排序算法之六:快速排序
- 【数据结构与算法】排序算法之六:快速排序
- 【数据结构与算法】排序算法之五:归并排序
- 【数据结构与算法】排序算法之五:归并排序
- 数据结构?
- 转载:java集合类数据结构分析