资源简介
构建二叉树、输出二叉树、求树深、复制二叉树
前序遍历

代码片段和文件信息
/*
构建二叉树、输出二叉树、求树深、复制二叉树 7
*/
#include
#include
typedef int DataType;
typedef struct BinTreeNode *PBinTreeNode;
typedef struct BinTreeNode{
DataType info;
PBinTreeNode lchild;
PBinTreeNode rchild;
}*BinTree;
int hdepth;
BinTree Create();
void print(BinTree t);
int Depth(BinTree tint hint depth);
BinTree GetBinTreeNode(DataType infostruct BinTreeNode lptrstruct BinTreeNode rptr);
BinTree CopyTree(BinTree t);
int main()
{
h = 1;
depth = 1;
BinTree tcopyt;
t = Create();
print(t);
printf(“\n“);
depth = Depth(thdepth);
copyt = CopyTree(t);
print(copyt);
printf(“\n%d\n“depth);
system(“PAUSE“);
return 0;
}
BinTree Create()
{
BinTree t;
DataType data;
if(scanf(“%c“&data) && data == ‘#‘)
t = NULL;
else{
t = (BinTree)malloc(sizeof(struct BinTreeNode));
t->info = data;
t->lchild = Create();
t->rchild = Create();
}
return t;
}
void print(BinTree t)
{
if(t){
printf(“%c“t->info);
print(t->lchild);
print(t->rchild);
}
}
int Depth(BinTree tint hint depth)
{
int ab;
if(t){
if(h > depth)depth = h;
a = Depth(t->lchildh + 1depth);
b = Depth(t->rchildh + 1depth);
depth = (a > b)? a:b;
}
return depth;
}
BinTree GetBinTreeNode(DataType infoPBinTreeNode lptrPBinTreeNode rptr)
{
BinTree newtnode;
newtnode = (BinTree)malloc(sizeof(struct BinTreeNode));
newtnode->info = info;
newtnode->lchild = lptr;
newtnode->rchild = rptr;
return newtnode;
}
BinTree CopyTree(BinTree t)
{
BinTree newtnode;
BinTree newlptrnewrptr;
newlptr = (BinTree)malloc(sizeof(struct BinTreeNode));
newrptr = (BinTree)malloc(sizeof(struct BinTreeNode));
if(!t)return NULL;
else{
if(t->lchild)newlptr = CopyTree(t->lchild);
else newlptr = NULL;
if(t->rchild)newrptr = CopyTree(t->rchild);
else newrptr = NULL;
newtnode = GetBinTreeNode(t->infonewlptrnewrptr);
return newtnode;
}
}
/*
Inout
abdh###e##cf##g##
*/
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2427 2009-02-05 21:22 scanf & depth (2).cpp
文件 16801 2009-02-05 17:04 scanf & depth (2).exe
----------- --------- ---------- ----- ----
19228 2
相关资源
- C语言的嵌入式汇编基本使用.docx(2页
- fluent UDF求解圆柱涡致振动问题(VIV
- c++ 定时关机程序源码
- 算法表达式求值.cpp
- 51基本框架
- 链表的基本操作.c
- c++ 求两个数的和
- 二叉树排序树建立及平衡处理
- C++语言实现一些基本算法(两点距离
- 常用数值计算方法c++源代码实现
- 求点介数C++实现 Betweenness Centrality
- 高斯消去法求解线性方程组C/C++程序输
- C++数据结构原理与经典问题求解源代
- 现代优化设计黄金分割法和二次插值
- 使用平衡二叉树管理的学生管理系统
- 求曼德勃罗集合C语言串行并行代码
- MFC画图板,可以画一些基本图形,如
- 求解VRP问题的C++源程序
- 用“破圈法”求解带权连通无向图的
- 家族成员信息管理系统
- C++ 基本电梯
- 一元二次方程求解MFC实现
- 二叉树 VC6.0 MFC实现 数据结构
- 链表栈的基本操作(C语言
- 求AOE网络关键路径
- 求解一元四次方程(vbvcc#excelhtml)-
- MFC画图板,可以画一些基本图形,如
- C++ 数据结构 迷宫求解
- 二叉树的建立以及遍历
- Hopfield求解TSP源程序及结果C++
评论
共有 条评论