二叉搜索树BinarySearchTree(C实现)
头文件——————————————————————————————
#ifndef _BINARY_SEARCH_TREE_H_
#define _BINARY_SEARCH_TREE_H_ #include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <stack>
#include <set> #define Element int
struct TreeNode
{
Element data;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode *Position;
typedef Position BinarySearchTree; void MakeEmpty(BinarySearchTree* pbst);
Position Find(Element x, BinarySearchTree bst);
Position FindMin(BinarySearchTree bst);
Position FindMax(BinarySearchTree bst);
void Insert(Element x, BinarySearchTree* pbst);
void Delete(Element x, BinarySearchTree* pbst);
Element Retrieve(Position p); void PrintTree(BinarySearchTree bst, int Depth, int ctrl);
void PreOrder(BinarySearchTree bst);
void InOrder(BinarySearchTree bst);
void PostOrder(BinarySearchTree bst);
void PreOrderNotRecursion(BinarySearchTree bst);
void InOrderNotRecursion(BinarySearchTree bst);
void PostOrderNotRecursion(BinarySearchTree bst); #endif
源文件——————————————————————————————
#include "BinarySearchTree.h" void MakeEmpty(BinarySearchTree* pbst)
{
if(NULL != (*pbst))
{
MakeEmpty(&((*pbst)->left));
MakeEmpty(&((*pbst)->right));
free(*pbst);
*pbst = NULL;
}
return ;
}
Position Find(Element x, BinarySearchTree bst)
{
while(NULL != bst)
{
if(x < Retrieve(bst))
bst = bst->left;
else if(x > Retrieve(bst))
bst = bst->right;
else
break;
}
return bst;
}
Position FindMin(BinarySearchTree bst)
{
while(NULL != bst && NULL != bst->left)
bst = bst->left;
return bst;
}
Position FindMax(BinarySearchTree bst)
{
while(NULL != bst && NULL != bst->right)
bst = bst->right;
return bst;
}
void Insert(Element x, BinarySearchTree* pbst)
{
if(NULL == *pbst)
{
Position tmp = (Position)malloc(sizeof(struct TreeNode));
if(NULL == tmp)
return ;
tmp->data = x;
tmp->left = tmp->right = NULL;
*pbst = tmp;
}
else if(x < (*pbst)->data)
Insert(x, &((*pbst)->left));
else if(x > (*pbst)->data)
Insert(x, &((*pbst)->right));
else
return ;
}
void Delete(Element x, BinarySearchTree* pbst)
{
if(NULL == *pbst)
return ;
if(x < (*pbst)->data)//go left
Delete(x, &((*pbst)->left));
else if(x > (*pbst)->data)//go right
Delete(x, &((*pbst)->right));
else //the x is found, the *pbst points to the x
{
if(NULL != (*pbst)->left && NULL != (*pbst)->right)//the x has two children
{
Position tmp = FindMin((*pbst)->right);
(*pbst)->data = tmp->data;
Delete((*pbst)->data, &((*pbst)->right));
}
else //the x has one or none child
{
Position tmp = (*pbst);
if(NULL != (*pbst)->left) //the x has left child
(*pbst) = tmp->left;
else if(NULL != (*pbst)->right) //the x has right child
(*pbst) = tmp->right;
else //the x has none child
(*pbst) = NULL;
free(tmp);
}
}
}
Element Retrieve(Position p)
{
return p->data;
}
void PrintTree(BinarySearchTree bst, int Depth, int ctrl)//ctrl:0=root 1=left 2=right
{ if(NULL != bst)
{
std::cout<<std::setw(Depth);
if(0 == ctrl)
std::cout<<"rt:";
else if(1 == ctrl)
std::cout<<"l";
else if(2 == ctrl)
std::cout<<"r";
std::cout<<bst->data<<std::endl;
PrintTree(bst->left, Depth+3, 1);
PrintTree(bst->right, Depth+3, 2);
}
}
void PreOrder(BinarySearchTree bst)
{
if(NULL != bst)
{
std::cout<<bst->data<<"-";
PreOrder(bst->left);
PreOrder(bst->right);
}
}
void InOrder(BinarySearchTree bst)
{
if(NULL != bst)
{
InOrder(bst->left);
std::cout<<bst->data<<"-";
InOrder(bst->right);
}
}
void PostOrder(BinarySearchTree bst)
{
if(NULL != bst)
{
PostOrder(bst->left);
PostOrder(bst->right);
std::cout<<bst->data<<"-";
}
}
void InOrderNotRecursion(BinarySearchTree bst)//二叉查找数的非递归版中序遍历,利用栈来模拟函数递归调用
{
std::stack<Position> s;
while(NULL != bst)//沿着最左的方向将左儿子依次压入栈中
{
s.push(bst);
bst = bst->left;
}
while(!s.empty())
{
Position pos = s.top();
s.pop();
std::cout<<pos->data<<"-";
if(NULL != pos->right)//pos has right child
{
pos = pos->right;
while(NULL != pos)//沿着最左的方向将左儿子依次压入栈中
{
s.push(pos);
pos = pos->left;
}
}
}
}
void PreOrderNotRecursion(BinarySearchTree bst)//二叉查找数的非递归版先序遍历
{
std::stack<Position> s;
s.push(bst);
while(!s.empty())
{
Position pos = s.top();
s.pop();
std::cout<<pos->data<<"-";
if(NULL != pos->right)//如果pos有右儿子就先将其右儿子压入栈中
s.push(pos->right);
if(NULL != pos->left)//如果pos有左儿子就再将其左儿子压入栈中
s.push(pos->left);
}
}
void PostOrderNotRecursion(BinarySearchTree bst)//二叉查找数的非递归版后序遍历
{
std::stack<Position> s;
std::set<Position> rchild_visited;
while(NULL != bst)//沿着最左的方向将左儿子依次压入栈中
{
s.push(bst);
bst = bst->left;
}
while(!s.empty())
{
Position pos = s.top();
//s.pop();
if(NULL == pos->right)//pos没有右儿子因此可以直接访问pos
{
std::cout<<pos->data<<"-";
s.pop();
}
else if(rchild_visited.find(pos) == rchild_visited.end())
{//pos有右儿子我们不可以访问pos,需要先访问完其右子树后才可访问pos
//因此要进入其右儿子中递归访问右子树同时标记pos的右儿子已经被我们访问过了
rchild_visited.insert(pos);
pos = pos->right;
while(NULL != pos)//把右子树的根连同其左儿子沿着左儿子的方向依次压入栈中
{
s.push(pos);
pos = pos->left;
}
}
else if(rchild_visited.find(pos) != rchild_visited.end())
{//此时pos右儿子已经被访问过了因此我们可以直接访问pos了
std::cout<<pos->data<<"-";
rchild_visited.erase(pos);
s.pop();
}
}
}
二叉搜索树BinarySearchTree(C实现)的更多相关文章
- 二叉搜索树(Java实现)
二叉搜索树基本操作 求树中的结点个数 判断节点是否为空 向树中插入新结点key-value 树中是否存在key 返回树中key对应的value值 先序遍历 中序遍历 后续遍历 层序遍历 求树中key最 ...
- BinarySearchTree二叉搜索树的实现
/* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...
- BinarySearchTree(二叉搜索树)原理及C++代码实现
BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...
- javascript数据结构——写一个二叉搜索树
二叉搜索树就是左侧子节点值比根节点值小,右侧子节点值比根节点值大的二叉树. 照着书敲了一遍. function BinarySearchTree(){ var Node = function(key) ...
- Java二叉搜索树实现
树集合了数组(查找速度快)和链表(插入.删除速度快)的优点 二叉树是一种特殊的树,即:树中的每个节点最多只能有两个子节点 二叉搜索树是一种特殊的二叉树,即:节点的左子节点的值都小于这个节点的值,节点的 ...
- 数据结构-二叉树(应用篇)-之二叉搜索树 C和C++的实现
一.概念 二叉搜索树(Binary Sort Tree/Binary Search Tree...),是二叉树的一种特殊扩展.也是一种动态查找表. 在二叉搜索树中,左子树上所有节点的均小于根节点,右子 ...
- Java与算法之(13) - 二叉搜索树
查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...
- [数据结构]P2.1 二叉搜索树
二叉树就是每个节点最多有两个分叉的树.这里我们写一写一个典型的例子二叉搜索树,它存在的实际意义是什么呢? 在P1.1链表中,我们清楚了链表的优势是善于删除添加节点,但是其取值很慢:数组的优势是善于取值 ...
- 【IT笔试面试题整理】二叉搜索树转换为双向链表
[试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...
随机推荐
- Python错误、调试和测试
try: print('try...') r = 10 / int('a') print('result:', r) except ValueError as e: print('ValueError ...
- 设想 Docker 下部署 KVM
设想 Docker 下部署 KVM 一.安装 $ yum -y install kvm # kvm base , must $ yum -y install libvirt -y # libvirtd ...
- H608B无线路由破解方法
家里的无线路由送人了,准备重新买,今日一看,竟然自带的猫(ZTE H608B)有wifi天线和4个LAN口,想着是支持路由功能的,那必然就是从软件上做了手脚.搜索该型号发现没人刷openwrt或者to ...
- 关闭/开启 ubuntu 自动更新提示
发现vps登陆后只有apt update后才知道有多少包需要更新不是很傻么,本地的ubuntu在登录时就有很好的提示,并且还能告知系统负载情况,很有用,这里就想开起来.首先这个提示的名字叫Motd. ...
- ubuntu下安装Node.js(源码安装)
最近使用hexo的过程中出现了问题,中间载nodejs安装的时候也耽误了些许时间,所以在此记录一下安装的过程. 环境:ubuntu14.0.4LTS,安装nodejs版本node-v0.10.36.t ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- 最近读的javascript,一些文章
本帖子是记录一些javascript的一些文章: 1. 理解node.js 2.异步编程 http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF% ...
- iis7下iis安全狗不能用怎么办(安装iis6兼容性)
is7下iis安全狗不能用怎么办 | 浏览:126 | 更新:2015-05-14 17:11 1 2 3 4 5 6 分步阅读 windows系统iis安全狗是常用的一款软件,针对网站使用很方便.在 ...
- IIS网站属性中没有ASP.NET选项
打开IIS6,右击属性,居然没有发现ASP.NET的配置选项,虽然好久没搞这个了,但是印象中绝对是有一个ASP.NET选项卡的.我很奇怪,我以为是.net framework 2.0没安装,下载下来安 ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...