二叉树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. 二叉树的构建 二叉树的基本 ...
随机推荐
- Instruments性能优化-Core Animation
简书地址:http://www.jianshu.com/users/6cb2622d5eac/latest_articles 当App发展到一定的规模.性能优化就成为不可缺少的一点.可是非常多人,又对 ...
- 终端复用工具tmux的使用
tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...
- 访问sharepoint站点自动使用当前用户名和密码登录
https://blog.csdn.net/zw_2011/article/details/7417123 1.把sharepoint站点添入可信站点. 点击菜单栏“工具”——〉“Internet ...
- 手动打war包进行部署测试
有的时候项目跑不起来但是又不知道tomcat问题还是其他问题,往往新建个项目,打成war进行部署.今天找到个好方法,手动打成war,然后进行部署测试. image.png image.png 创建一个 ...
- php中如何获取数组长度
php获取数组的长度的方法 一.总结 一句话总结:count方法和sizeof方法 二.php获取数组的长度的方法 php获取数组长度的方法: 一). 获取一维数组的方法: 1.count.sizeo ...
- 工具类与工具函数 —— NextPrime
求大于某数的下一个素数: static int NextPrime (int N) { if (N % 2 == 0) ++N; int i; for (; ; N += 2){ for (i = 3 ...
- 数据结构与算法——常用数据结构及其Java实现
前言 仿佛一下子,2017年就快过去一半了,研一马上就要成为过去式了,我打算抓住研一的尾巴,好好梳理一下数据结构与算法,毕竟这些基础知识是很重要的嘛.所以准备在这里搞一个系列的文章,以期透彻. 本系列 ...
- [Postgres] Update and Delete records in Postgres
Delete example: DELETE FROM movies ; Update example: UPDATE movies ;
- 机器学习 Softmax classifier (一个隐含层)
程序实现 softmax classifier, 含有一个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...
- oracle2c-r2(12.2.0.1) 的镜像
docker- 构建 oracle2c-r2(12.2.0.1) 的镜像 需求 由于公司的数据库需要使用新的oracle版本(12c-r2 -->12.2.0.1),从之前的oracle11 ...