在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. ASP.NET State Service

    本文来自:http://www.cnblogs.com/jhxk/articles/1648194.html 这一段就是配置应用程序是如何存储Session信息的了.我们以下的各种操作主要是针对这一段 ...

  2. jquery获取父窗口的元素[转]

    $("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementById ...

  3. ffmpeg+SDl+ 播放器 -01

    最近因公司项目需要,打算自己在LINUX平台整一个播放器,来学习和研究音频编解码. 项目需求: 支持下列格式文件播放. 1> WMA 硬件解码,但需要软件分析ASF格式,提取Payload数据 ...

  4. ORACLE表空间管理方式segment和extent

    A permanent tablespace contains persistent schema objects. Objects in permanent tablespaces are stor ...

  5. 【网络流】【HDU3081】Marriage Match II

    得出正解前的思考: 1.我该如何处理朋友关系?消去朋友关系 ,直接由朋友关系得出情人关系的连线? 2.我该如何保证每次源点给1-N 平均分配1点流? 又可耻的看了题解,答案让我醍醐灌顶

  6. linux下的DNS

    Linux下设置DNS的位置主要是, 1网卡设置配置文件里面DNS服务器地址设置;2 hosts文件指定 3.系统默认DNS服务器地址设置/etc/resolv.conf文件修改 生效顺序是: 1 h ...

  7. zTouch-移动端触屏开发利器(zepto touch扩展)

    * Zepto.js v1.0.1 touch extend (Zepto.js v1.0.1 的swipe touch扩展)js-处理手机移动端web触屏手势动作. Zepto.js v1.0.1版 ...

  8. Android学习自定义Dialog

    Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...

  9. CSS中字体尺寸总结

    下面是我总结的css中关于字体尺寸的知识,欢迎高手拍砖! 前端开发过程中,我们经常会遇到设置某个div固定显示几行文本:这时我们需要精确计算每个字号字体的宽度和高度. 下面是w3school中描述的尺 ...

  10. js删除数组里的某个元素

    首先可以给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) { for (var i = ...