头文件——————————————————————————————
#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实现)的更多相关文章

  1. 二叉搜索树(Java实现)

    二叉搜索树基本操作 求树中的结点个数 判断节点是否为空 向树中插入新结点key-value 树中是否存在key 返回树中key对应的value值 先序遍历 中序遍历 后续遍历 层序遍历 求树中key最 ...

  2. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...

  3. BinarySearchTree(二叉搜索树)原理及C++代码实现

    BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...

  4. javascript数据结构——写一个二叉搜索树

    二叉搜索树就是左侧子节点值比根节点值小,右侧子节点值比根节点值大的二叉树. 照着书敲了一遍. function BinarySearchTree(){ var Node = function(key) ...

  5. Java二叉搜索树实现

    树集合了数组(查找速度快)和链表(插入.删除速度快)的优点 二叉树是一种特殊的树,即:树中的每个节点最多只能有两个子节点 二叉搜索树是一种特殊的二叉树,即:节点的左子节点的值都小于这个节点的值,节点的 ...

  6. 数据结构-二叉树(应用篇)-之二叉搜索树 C和C++的实现

    一.概念 二叉搜索树(Binary Sort Tree/Binary Search Tree...),是二叉树的一种特殊扩展.也是一种动态查找表. 在二叉搜索树中,左子树上所有节点的均小于根节点,右子 ...

  7. Java与算法之(13) - 二叉搜索树

    查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...

  8. [数据结构]P2.1 二叉搜索树

    二叉树就是每个节点最多有两个分叉的树.这里我们写一写一个典型的例子二叉搜索树,它存在的实际意义是什么呢? 在P1.1链表中,我们清楚了链表的优势是善于删除添加节点,但是其取值很慢:数组的优势是善于取值 ...

  9. 【IT笔试面试题整理】二叉搜索树转换为双向链表

    [试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...

随机推荐

  1. HTML5骨骼动画Demo | 使用min2d、createjs、pixi播放spine动画

    Spine做骨骼动画是比较流行的,使用起来可能相对复杂,但功能毕竟强大,所以市场占有率较大. 在unity.cocos2d.starling中使用spine已经很成熟了,而HTML5这一块可能刚刚起步 ...

  2. python BeautifulSoup实例测验

  3. DES加密 java与.net可以相互加密解密的方法

    我.net程序员.今天和java的童鞋交互,单点登录的操作.采用了如下的加密和解密的方式.经过验证,完美结合.通过这个方法可以实现java和C#相互加密与解密 并能保持解密出来一致. 废话少说,上代码 ...

  4. sqlserver数据库安全函数、配置函数、游标函数、行级函数、排名函数、元数据函数、系统统计函数 、文本和图像函数--收藏着有用

    行级函数:下列行集函数将返回一个可用于代替 Transact-SQL 语句中表引用的对象. CONTAINSTABLE 返回具有零行.一行或多行的表,这些行的列中包含的基于字符类型的数据是单个词语和短 ...

  5. 软件设计之UML—UML的构成[上]

      UML是一种通用的建模语言,其表达能力相当的强,不仅可以用于软件系统的建模,而且可用于业务建模以及其它非软件系统建模.UML综合了各种面向对象方法与表示法的优点,至提出之日起就受到了广泛的重视并得 ...

  6. 上海邮政EMS海关清关(个人) 流程

    最近雾埋越来越严重,上个星期买了一个tacx骑行台,不料运气欠佳,被税了.那就去乖乖缴税吧. 拿着EMS的通知单(没有通知单就不要去了),到通知单指定的地址(上海有两处,我的是武定路458号)清关提货 ...

  7. Scala 深入浅出实战经典 第46讲: ClassTag 、Manifest、ClasMainifest TagType实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  8. python数据结构之图的实现

    python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...

  9. asp.net mvc 动态显示不同的部分视图

    首先是AJAX请求 //第一次打开 默认s单行文本 $.ajax({ type: "GET", url: "/Admin/Field/ChoiceType4Edit&qu ...

  10. 阿里云 Redis 服务遇到的问题

    ERR unknown command eval 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: St ...