二叉树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. 二叉树的构建 二叉树的基本 ...
随机推荐
- 编译pano13的一些注意事项
作者:朱金灿 来源:error C2037: "jmpbuf"的左侧部分指定未定义的结构/联合"png_struct_def"e:\src\Test\libpa ...
- 1.2.4 Java Annotation 提要
(本文是介绍依赖注入容器Spring和分析JUnit源码的准备知识) Java Annotation(标注) java.lang.annotation.Annotation是全部Java标注的父接口. ...
- [Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2
A @Directive is used to add behavior to elements and components in your application. This makes @Dir ...
- <Linux> xm 命令
xm console <域ID> ctrl+ ] 退出虚拟机到宿主 xm reboot <域ID> xm pause <域I ...
- Cisco IOS images
Cisco IOS images for Dynamips - GNS3http://docs.gns3.com/1-kBrTplBltp9P3P-AigoMzlDO-ISyL1h3bYpOl5Q8m ...
- Rational Rose2007无法正常启动解决方式
安装完Rational Rose发现无法正常启动,我遇到了下面两个问题,希望能帮到同样经历的同学. 问题一: 安装完Rational Rose后不能用,提演示样例如以下:无法启动此程序,由于计算机中丢 ...
- 【codeforces 750D】New Year and Fireworks
time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...
- python request爬取百度贴吧
import requests import os import shutil import time class PostBarSpider(object): def __init__(self, ...
- Java与C#的语法区别
1.作用域 在java中 { { int a=1; } int a=2;//以上a作用域外的以下,再声明同名的变量,是允许的: } 在C#中,以上是不允许的[只要在同一个作用域内,以上或以下的代码中 ...
- 远程ssh执行命令时提示找不到命令
最开始的时候碰到这种问题,是在hadoop003上配置了jdk1.8, 在hadoop002上执行ssh hadoop003 java -version提示没有命令,先ssh hadoop003然后执 ...