在cpp中使用的C语言

头文件

 /* 有序二叉树 BsTree */
#ifndef _BT_H
#define _BT_H
/*节点*/
typedef struct BsTreeNode
{
int data;/* 数据 */
struct BsTreeNode* left;/* 左子树 */
struct BsTreeNode* right;/* 右子树 */
}BSTREE_NODE;
/* 二叉树 */
typedef struct BsTree
{
BSTREE_NODE* root;/* 树根 */
size_t size;/* 大小 */
}BSTREE;
/* 初始化为空树 */
void bstree_init(BSTREE* bstree);
/* 释放剩余节点并恢复到初始状态 */
void bstree_deinit(BSTREE* bstree);
/* 插入 */
void bstree_insert(BSTREE* bstree, int data);
/* 删除 */
bool bstree_erase(BSTREE* bstree, int data);
/* 删除所有匹配数据 */
void bstree_remove(BSTREE* bstree, int data);
/* 清空 */
void bstree_clear(BSTREE* bstree);
/* 更新 */
void bstree_update(BSTREE* bstree, int _old, int _new);
/* 判断是否存在 */
bool bstree_exist(BSTREE* bstree, int data);
/* 中序遍历 */
void bstree_travel(BSTREE* bstree);
/* 大小 */
size_t bstree_size(BSTREE* bstree);
/* 高度 */
size_t bstree_height(BSTREE* bstree);
#endif /*_BT_H*/

实现

 /* 有序二叉树 */
#include <stdio.h>
#include <stdlib.h>
#include "bt.h" /*********************** 内 部 接 口 ********************************/
/* 创建节点 */
static BSTREE_NODE* create_node(int data)
{
BSTREE_NODE* node = (BSTREE_NODE*)malloc(sizeof(BSTREE_NODE));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
/* 销毁节点 */
static void destory_node(BSTREE_NODE* node)
{
free(node);
}
/* 将node节点插入到以root为根的子树中 */
static void insert(BSTREE_NODE* node, BSTREE_NODE** root)
{
if ( NULL == *root )
*root = node;
else if (node)
{
if ( node->data < (*root)->data )
insert(node, &((*root)->left));
else
insert(node, &((*root)->right));
}
}
/* 返回以参数root为根的子树中,数据与参数data匹配的节点的父节点中
* 指向该节点的指针型成员变量的地址
* 目的是在删除节点后,要将删除节点的子节点接到此变量的地址上。
*/
static BSTREE_NODE** find(int data, BSTREE_NODE** root)
{
if (NULL == *root)
return root;
if (data < (*root)->data)
return find(data, &(*root)->left);
if (data > (*root)->data)
return find(data, &(*root)->right);
return root;
}
/* 删除以参数root为根的子树 */
static void clear(BSTREE_NODE** root)
{
if (*root)
{
clear(&(*root)->left);
clear(&(*root)->right);
destory_node(*root);
*root = NULL;
}
}
/* 中序遍历以参数root为根的子树 */
static void travel(BSTREE_NODE* root)
{
if (root)
{
travel(root->left);
printf("%d ", root->data);
travel(root->right);
}
}
/* 返回以参数root为根的子树的高度 */
static size_t height(BSTREE_NODE* root)
{
if (root)
{
size_t lh = height(root->left);
size_t rh = height(root->right);
return (((lh > rh) ? lh : rh) + );
}
return ;
}
/*********************** 外 部 接 口 ********************************/
/* 初始化为空树 */
void bstree_init(BSTREE* bstree)
{
bstree->root = NULL;
bstree->size = ;
}
/* 释放剩余节点并恢复到初始状态 */
void bstree_deinit(BSTREE* bstree)
{
clear(&bstree->root);
bstree->size = ;
}
/* 插入 */
void bstree_insert(BSTREE* bstree, int data)
{
insert(create_node(data), &bstree->root);
++bstree->size;
}
/* 删除 */
bool bstree_erase(BSTREE* bstree, int data)
{
BSTREE_NODE** node = find(data, &bstree->root);
if (*node)
{
/* 将匹配节点的左子树插入其右子树 */
insert((*node)->left, &(*node)->right);
BSTREE_NODE* tmp = *node;
/* 用匹配节点的右子树的根节点取代匹配节点 */
*node = (*node)->right;
/* 销毁匹配节点 */
destory_node(tmp);
--bstree->size;
return true;
}
return false;
}
/* 删除所有匹配数据 */
void bstree_remove(BSTREE* bstree, int data)
{
while(bstree_erase(bstree, data));
}
/* 清空 */
void bstree_clear(BSTREE* bstree)
{
bstree_deinit(bstree);
}
/* 更新 */
void bstree_update(BSTREE* bstree, int _old, int _new)
{
while(bstree_erase(bstree, _old))
bstree_insert(bstree, _new);
}
/* 判断是否存在 */
bool bstree_exist(BSTREE* bstree, int data)
{
return *find(data, &bstree->root) != NULL;
}
/* 中序遍历 */
void bstree_travel(BSTREE* bstree)
{
travel(bstree->root);
printf("\n");
}
/* 大小 */
size_t bstree_size(BSTREE* bstree)
{
return bstree->size;
}
/* 高度 */
size_t bstree_height(BSTREE* bstree)
{
return height(bstree->root);
}

测试用例

 /* 有序二叉树 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bt.h" int main ()
{
srand((unsigned)time(NULL));
BSTREE bstree;
bstree_init(&bstree);
int i;
for (i = ; i < ; ++i)
bstree_insert(&bstree, rand()%); bstree_travel(&bstree);
bstree_clear(&bstree);
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_travel(&bstree);
printf("大小:%u\n树高:%u\n",
bstree_size(&bstree), bstree_height(&bstree)); bstree_erase(&bstree, );
bstree_travel(&bstree);
bstree_insert(&bstree, );
bstree_insert(&bstree, );
bstree_travel(&bstree);
bstree_update(&bstree, , );
bstree_travel(&bstree);
bstree_remove(&bstree, );
bstree_travel(&bstree);
if (bstree_exist(&bstree, /**/))
printf("有!\n");
else
printf("没有!\n"); bstree_deinit(&bstree);
system("pause");
return ;
}

练习:(一般的二叉树)

已知某二叉树前序遍历的结果为:1 2 4 7 3 5 6 8

      中序遍历的结果为:4 7 2 1 5 3 8 6

编写三个函数分别用于重建二叉树、前序遍历和中序遍历。

C语言实现有序二叉树(1)的更多相关文章

  1. c++ 有序二叉树的应用

    实作:以有序二叉树记录学生签到时间及名字,然后以名字升序输出学生签到信息 stricmp,strcmpi 原型:extern int stricmp(char *s1,char * s2); 用法:# ...

  2. C语言数据结构之二叉树的实现

    本篇博文是博主在学习C语言算法与数据结构的一些应用代码实例,给出了以二叉链表的形式实现二叉树的相关操作.如创建,遍历(先序,中序后序遍历),求树的深度,树的叶子节点数,左右兄弟,父节点. 代码清单如下 ...

  3. 数据结构(C语言版)---二叉树

    1.二叉树:任意一个结点的子结点个数最多两个,且子结点的位置不可更改,二叉树的子树有左右之分. 1)分类:(1)一般二叉树(2)满二叉树:在不增加树的层数的前提下,无法再多添加一个结点的二叉树就是满二 ...

  4. c语言编程之二叉树

    利用链表建立二叉树,完成前序遍历.中序遍历.后序遍历. 建立二叉树用的是前序遍历建立二叉树: #include<stdio.h> #include<stdlib.h> #inc ...

  5. C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)

    今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...

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

    //线索二叉树,这里在二叉树的基础上增加了线索化 //杨鑫 #include <stdio.h> #include <stdlib.h> typedef char ElemTy ...

  7. c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...

  8. Leecode刷题之旅-C语言/python-101对称二叉树

    /* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...

  9. C语言递归实现二叉树的先序、中序、后序遍历

    #include <stdio.h> #include <stdlib.h> //*****二叉树的二叉链表存储表示*****// typedef struct BiNode ...

随机推荐

  1. 爱加密亮相第十八届软博会,移动App安全引关注

    2014年5月29日至31日,2014年第十八届中国国际软件博览会在北京展览馆举行,此次软博会的主题为"软件引领信息消费,助力经济转型升级",充分展示软件业在促进信息消费.提升社会 ...

  2. C#整理2——C#的输入输出及基本类型

    //输出 Console.WriteLine("摩西摩西"); Console.Write("hollo");不带回车的 注意: 1.注意大小写敏感.(快捷键操 ...

  3. 使用FileSystemWatcher捕获系统文件状态

    源代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  4. jsonsql

    http://www.trentrichardson.com/jsonsql/ 可以对json数组用sql语法进行操作,主要是查询取指定字段.条件.指定字段排序及获取多少条数据,返回值json. js ...

  5. 多线程12_张孝祥 java5读写锁技术的妙用

    package locks; import java.util.Random; import java.util.concurrent.locks.ReentrantReadWriteLock; /* ...

  6. GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟。

    GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟. 支持输出多种格式 GitBook支 ...

  7. Netty详解

    Netty详解  http://blog.csdn.net/suifeng3051/article/category/2161821

  8. 深搜基础题目 杭电 HDU 1241

    HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...

  9. Ubuntu 12.04 wireless networks : devices not ready (firmware missing)解决办法

    今天装了Ubuntu12.04之后,发现无线不能用. 用iwconfig查看,wlan0 项后面有内容,但是在本该显示无线列表的地方显示的是 “ wireless networks : devices ...

  10. Android 判断当前设备是否联网

    首先添加相关的权限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> ...