在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. H264源码分析(二)

    原文出自http://blog.csdn.net/xfding/article/details/5476763(转载收集) (四)图像参数集语义 pic_parameter_set_rbsp( ) { ...

  2. SQLServer中临时表与表变量的区别分析【转】

    在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候去使用临时表而不使用表变量,有时候去使用表变量而不使用临时表呢? 临时表 临时表与永 ...

  3. Stopwatch 类【转】

    一般我们想要测试使用那种方法或着那种类型效率更高,使用Stopwatch类进行测试就可以,我也是现在才知道,汗一个. 先来看个小示例,如下. 前提,先引用using System.Diagnostic ...

  4. 操作hadoop的经验积累

    操作hadoop的经验积累 Hadoop namenode –format 在执行格式化-format命令时,要避免namenode的namdespaceid与datanode的namespaceid ...

  5. HDU 3081Marriage Match II(二分法+并检查集合+网络流量的最大流量)

    职务地址:http://acm.hdu.edu.cn/showproblem.php? pid=3081 有一段时间没写最大流的题了,这题建图竟然想了好长时间... 刚開始是按着终于的最大流即是做多轮 ...

  6. MySQL之终端(Terminal)管理MySQL(转)

    前言:MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”. 现在我写MySQL的终端命令操作的文章,是想强化一下自己对于MySQL的理解,总会比使用图 ...

  7. Oracle delete input与delete all input

    oracle官方文档提示:If you had specified DELETE INPUT rather than DELETE ALL INPUT, then RMAN would have on ...

  8. .net 生成缩略图

    public static void CreateSmallImage(string minImageFullPath, System.Drawing.Image originalImage, int ...

  9. 修复CefSharp浏览器组件中文输入Bug

    概述 最近在win10上开发wpf应用,需要将CefSharp中wpf版本的浏览器组件(版本号v51.0.0)嵌入到应用中,但是发现不支持中文输入,GitHub上有这个问题的描述,参照其提到的方法可以 ...

  10. Android 测试工具集01

    Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...