二叉树C语言
几乎报价http://blog.csdn.net/hopeyouknow/article/details/6740616。为了这细微的地方进行了修改。他能够执行。
bitree.h
typedef int Item;
typedef struct node
{
struct node *lchild;
struct node *rchild;
Item data;
}BiTNode, *BiTree; BiTree InitBiTree(BiTNode *root); BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild); void FreeNode(BiTNode *pnode); void ClearBiTree(BiTree tree); void DestroyBiTree(BiTree tree); int IsEmpty(BiTree tree); int GetDepth(BiTree tree); BiTree GetRoot(BiTree tree); Item GetItem(BiTNode *pnode); void SetItem(BiTNode *pnode, Item item); BiTree SetLChild(BiTree parent, BiTree lchild); BiTree SetRChild(BiTree parent, BiTree rchild); BiTree GetLchild(BiTree tree); BiTree GetRchild(BiTree tree); BiTree InsertChild(BiTree parent, int lr, BiTree child); void DeleteChild(BiTree parent, int lr); void PreOrderTraverse(BiTree tree, void (*visit)()); void InOrderTraverse(BiTree tree, void (*visit)()); void PostOrderTraverse(BiTree tree, void (*visit)());
bitree.c
#include "bitree.h"
#include <malloc.h>
#include <stdlib.h> BiTree InitBiTree(BiTNode *root)
{
BiTree tree = root;
return tree;
} BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild)
{
BiTNode *pnode = (BiTNode *)malloc(sizeof(BiTNode));
if(pnode){
pnode->data = item;
pnode->lchild = lchild;
pnode->rchild = rchild;
}
return pnode;
} void FreeNode(BiTNode *pnode)
{
if(pnode != NULL)
free(pnode);
} void ClearBiTree(BiTree tree)
{
BiTNode *pnode = tree;
if(pnode->lchild != NULL)
ClearBiTree(pnode->lchild); if(pnode->rchild != NULL)
ClearBiTree(pnode->rchild); FreeNode(pnode);
} void DestroyBiTree(BiTree tree)
{
if(tree)
ClearBiTree(tree);
} IsEmpty(BiTree tree)
{
if(tree == NULL)
return 0;
else
return 1;
} int GetDepth(BiTree tree)
{
int cd, ld, rd;
cd = ld = rd = 0;
if(tree){
ld = GetDepth(tree->lchild);
rd = GetDepth(tree->rchild);
cd = (ld > rd ? ld : rd);
return cd+1;
}else
return 0;
} BiTree GetRoor(BiTree tree)
{
return tree;
} Item GetItem(BiTNode *pnode)
{
return pnode->data;
} void SetItem(BiTNode *pnode, Item item)
{
pnode->data = item;
} BiTree SetLChild(BiTree parent, BiTree lchild)
{
parent->lchild = lchild;
return lchild;
} BiTree SetRChild(BiTree parent, BiTree rchild)
{
parent->rchild = rchild;
return rchild;
} BiTree GetLchild(BiTree tree)
{
if(tree)
return tree->lchild;
return NULL;
} BiTree GetRchild(BiTree tree)
{
if(tree)
return tree->rchild;
return NULL;
} BiTree InsertChild(BiTree parent, int lr, BiTree child)
{
if(parent){
if(lr == 0 && parent->lchild == NULL){
parent->lchild = child;
return child;
}
if(lr == 1 && parent->lchild == NULL){
parent->rchild = child;
return child;
}
}
return NULL;
} void DeleteChild(BiTree parent, int lr)
{
if(parent){
if(lr == 0 && parent->lchild != NULL){
parent->lchild = NULL;
FreeNode(parent->lchild);
}
if(lr == 1 && parent->rchild != NULL){
parent->rchild = NULL;
FreeNode(parent->rchild);
}
}
} void PreOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
visit(pnode->data);
PreOrderTraverse(pnode->lchild, visit);
PreOrderTraverse(pnode->rchild, visit);
}
} void InOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
InOrderTraverse(pnode->lchild, visit);
visit(pnode->data);
InOrderTraverse(pnode->lchild, visit);
}
} void PostOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
PostOrderTraverse(pnode->lchild, visit);
PostOrderTraverse(pnode->lchild, visit);
visit(pnode->data);
}
}
test.c
#include "bitree.h"
#include <stdio.h> void print(Item item)
{
printf("%d ", item);
} main()
{
BiTNode *n1 = MakeNode(10, NULL, NULL);
BiTNode *n2 = MakeNode(20, NULL, NULL);
BiTNode *n3 = MakeNode(30, n1, n2);
BiTNode *n4 = MakeNode(40, NULL, NULL);
BiTNode *n5 = MakeNode(50, NULL, NULL);
BiTNode *n6 = MakeNode(60, n4, n5);
BiTNode *n7 = MakeNode(70, NULL, NULL); BiTree tree = InitBiTree(n7);
SetLChild(tree, n3);
SetRChild(tree, n6); printf("the depth of the tree is %d.\n", GetDepth(tree)); printf("preorderTraverse:\n");
PreOrderTraverse(tree, print);
putchar('\n'); printf("inorderTraverse:\n");
InOrderTraverse(tree, print);
putchar('\n'); printf("postorderTraverse:\n");
PostOrderTraverse(tree, print);
putchar('\n'); DeleteChild(tree, 1);
printf("postorderTraverse:\n");
PostOrderTraverse(tree, print); DestroyBiTree(tree);
if(IsEmpty(tree))
printf("the tree is empty!\n"); return 0; }
编译:gcc test.c bitree.c
执行:./a.out
执行结果:
the depth of the tree is 3.
preorderTraverse:
70 30 10 20 60 40 50
inorderTraverse:
10 30 10 70 10 30 10
postorderTraverse:
10 10 30 10 10 30 70
postorderTraverse:
10 10 30 10 10 30 70
the tree is empty!
版权声明:本文博客原创文章,博客,未经同意,不得转载。
二叉树C语言的更多相关文章
- 数据结构之线索二叉树——C语言实现
线索二叉树操作 (1) 线索二叉树的表示:将每个节点中为空的做指针与右指针分别用于指针节点的前驱和后续,即可得到线索二叉树. (2) 分类:先序线索二叉树,中序线索二叉树,后续线索二叉树 (3) 增 ...
- c语言_二叉树的建立以及3种递归
二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{ int data; struct node* left; struct node* ri ...
- 小白专场-树的同构-c语言实现.md
目录 一.题意理解 二.求解思路 2.1 二叉树表示 2.2 程序框架搭建 2.3 如何建二叉树 2.4 如何判别两二叉树同构 更新.更全的<数据结构与算法>的更新网站,更有python. ...
- 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树
一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...
- C语言实现二叉树-02版
---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...
- C语言实现二叉树-利用二叉树统计单词数目
昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...
- C语言实现二叉树
二叉树的重要性就不用多说啦: 我以前也学习过,但是一直没有总结: 网上找到的例子,要么是理论一大堆,然后是伪代码实现: 要么是复杂的代码,没有什么解释: 最终,还是靠FQ找到一些好的文章,参考地址我会 ...
- C语言实现有序二叉树(1)
在cpp中使用的C语言 头文件 /* 有序二叉树 BsTree */ #ifndef _BT_H #define _BT_H /*节点*/ typedef struct BsTreeNode { in ...
- C语言实现二叉树的基本操作
二叉树是一种非常重要的数据结构.本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历.中序遍历.后序遍历.层次遍历),二叉搜索树的构造等. 1. 二叉树的构建 二叉树的基本 ...
随机推荐
- angular表单经验分享
原文 https://www.jianshu.com/p/af359bd04f32 大纲 1.表单的名字不可以和传入的值的名字相同 2.在模板驱动表单的时候要使用angular表单的验证功能,需要将n ...
- Booth算法
Booth算法 算法描述(载自维基百科) 对于N位乘数Y,布斯算法检查其2的补码形式的最后一位和一个隐含的低位,命名为y-1,初始值为0.对于yi, i = 0, 1, ..., N - 1,考察yi ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- 【53.61%】【BZOJ 2302】[HAOI2011]Problem c
Time Limit: 30 Sec Memory Limit: 256 MB Submit: 526 Solved: 282 [Submit][Status][Discuss] Descriptio ...
- Android 软键盘监听事件
Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入. 那么,弹出软键盘后必然会造成原有布局高度的减少 ...
- JavaEE 三层架构的浅谈
三层架构 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL).区分层次的目的即为了“高内聚,低 ...
- 基于 Android NDK 的学习之旅-----Android.mk 介绍
一个Android.mk file用来向编译系统描述你的源代码.具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次.你可以在每一个Android.mk file中定义一个 ...
- 批量解决 word/wps 中公式和文字不对齐的问题
完美解决Word或wps中中公式和文字对不齐的问题 在 word 的各个版本中,当公式和字符同时出现时,尤其是发生公式的拷贝粘贴时,公式往往会出现上飘或下移的情况,这里给出一个简单易行的解决方案: 全 ...
- 安装 Visual Studio,连接中国区 Azure
中国数据中心 目前,中国区 Azure 有两个数据中心,在位置字段中显示为“中国北部”和“中国东部”. 在 Azure 上创建应用程序的区别 在中国区 Azure 上开发应用程序与在境外 Azure ...
- golang快速入门(练习)
1.打包和工具链 1.1 包 所有 Go 语言的程序都会组织成若干组文件,每组文件被称为一个包. ? 1 2 3 4 5 6 7 8 9 10 net/http/ cgi/ cooki ...