几乎报价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语言的更多相关文章

  1. 数据结构之线索二叉树——C语言实现

     线索二叉树操作 (1) 线索二叉树的表示:将每个节点中为空的做指针与右指针分别用于指针节点的前驱和后续,即可得到线索二叉树. (2) 分类:先序线索二叉树,中序线索二叉树,后续线索二叉树 (3) 增 ...

  2. c语言_二叉树的建立以及3种递归

    二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{    int data;    struct node* left;    struct node* ri ...

  3. 小白专场-树的同构-c语言实现.md

    目录 一.题意理解 二.求解思路 2.1 二叉树表示 2.2 程序框架搭建 2.3 如何建二叉树 2.4 如何判别两二叉树同构 更新.更全的<数据结构与算法>的更新网站,更有python. ...

  4. 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树

    一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...

  5. C语言实现二叉树-02版

    ---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...

  6. C语言实现二叉树-利用二叉树统计单词数目

    昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...

  7. C语言实现二叉树

    二叉树的重要性就不用多说啦: 我以前也学习过,但是一直没有总结: 网上找到的例子,要么是理论一大堆,然后是伪代码实现: 要么是复杂的代码,没有什么解释: 最终,还是靠FQ找到一些好的文章,参考地址我会 ...

  8. C语言实现有序二叉树(1)

    在cpp中使用的C语言 头文件 /* 有序二叉树 BsTree */ #ifndef _BT_H #define _BT_H /*节点*/ typedef struct BsTreeNode { in ...

  9. C语言实现二叉树的基本操作

    二叉树是一种非常重要的数据结构.本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历.中序遍历.后序遍历.层次遍历),二叉搜索树的构造等. 1. 二叉树的构建 二叉树的基本 ...

随机推荐

  1. Coverage报告生成

    Coverage报告生成 覆盖率 覆盖率驱动的验证方法中覆盖率报告的生成至关重要,现在介绍一下使用DVE和URG生成覆盖率报告的步骤. 使用VCS生成数据 在VCS的运行脚本中添加-cm cond+f ...

  2. VCS编译仿真警告Warning

    VCS编译仿真警告Warning 问题描述 在较大的SOC集成中,通常使用Perl脚本例化子模块到Top层,然而,有时会出现例化出来的输入端口名没有在Top层定义,而且端口的位宽为1bit,那么,ve ...

  3. iOS中OC给Category加入属性

    引: 非常多人知道能够用Category给已有的类加入一些新方法,可是不同于swift中的extension,Objective-C中的Category(类别)是不支持直接加入属性的.那假设就是须要加 ...

  4. QAtomicInt支持四种类型的操作,Relaxed、Acquired、Release、Ordered

    Memory Ordering   Background 很久很久很久以前,CPU忠厚老实,一条一条指令的执行我们给它的程序,规规矩矩的进行计算和内存的存取. 很久很久以前, CPU学会了Out-Of ...

  5. [TypeScript] Find the repeated item in an array using TypeScript

    Say you have an array that has at least one item repeated. How would you find the repeated item. Thi ...

  6. [NPM] Add comments to your npm scripts

    The need for comments in your package.json file becomes desirable the more and more npm scripts you ...

  7. CSU1323: ZZY and his little friends

    Description zzy养了一只小怪兽和N只凹凸曼,单挑的话每只凹凸曼都不是小怪兽的对手,所以必须由两只凹凸曼合作来和小怪兽战斗.凹凸曼A和凹凸曼B合作的战斗力为他们战斗力的异或值.现在由zzy ...

  8. 数据序列化之protobuf

    数据序列化之protobuf 很多时候需要将一些数据打包,就是把这些数据搞在一起,方便处理.最常见的情况就是把需要传输的数据,当然数据不止一条,打包成一个消息,然后发送出去,接收端再以一定的规则接收并 ...

  9. Everything starts with a dream(A day has only 24 hours and these things take time,所以要抓紧)

    There is the famous quote: "Everything starts with a dream" and many years ago, Michael Va ...

  10. color2gray 的实现

    无论是 rgb 还是 yuv 等三通道的颜色空间中的像素点,将其转换为单通道(pixel_depth=255.)中的像素,一般情况下都是采用的对原始颜色空间的 3 通道的像素点线性组合而得到单通道的像 ...