BST的实现(二叉搜索树)
void Inorder(struct Tree *T); //中序
void Preorder(struct Tree *T); //前序
void Postorder(struct Tree *T); //后序
struct Tree * InsertTree(struct Tree * T, int z);
struct Tree * Delete(struct Tree *T, int z);
struct Tree * FindMin(struct Tree *T);
struct Tree * FindMax(struct Tree *T);
DataType Maximum(struct Tree *T);
DataType Minimum(struct Tree *T);
bool Search(struct Tree * T, int z);
就这些功能
#include <iostream>
#include <cstdlib>
using namespace std;
typedef int DataType;
struct Tree
{
//struct Tree *Parent;
struct Tree *Left;
struct Tree *Right;
DataType key;
};
void Inorder(struct Tree *T); //中序
void Preorder(struct Tree *T); //前序
void Postorder(struct Tree *T); //后序
struct Tree * InsertTree(struct Tree * T, int z);
struct Tree * Delete(struct Tree *T, int z);
struct Tree * FindMin(struct Tree *T);
struct Tree * FindMax(struct Tree *T);
DataType Maximum(struct Tree *T);
DataType Minimum(struct Tree *T);
bool Search(struct Tree * T, int z);
struct Tree *s; int main()
{
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
s=InsertTree(s, );
cout << "It's the result of Preorder tree walk.\n";
Preorder(s);
cout << "It's the result of inorder tree walk.\n";
Inorder(s);
cout << "It's the result of Postorder tree walk.\n";
Postorder(s);
cout << "The maximum element is ";
cout << Maximum(s) << endl;
cout << "The minimum element is ";
cout << Minimum(s) << endl;
Delete(s, );
cout << "It's the result of Preorder tree walk.\n";
Preorder(s);
cout << "It's the result of inorder tree walk.\n";
Inorder(s);
cout << "It's the result of Postorder tree walk.\n";
Postorder(s);
return ;
} bool Search(struct Tree * T, int z)
{ if(z<T->key)
Search(T->Left, z);
else if(z>T->key)
Search(T->Right, z);
else if(z==T->key)
return true;
else
return false;
} struct Tree * InsertTree(struct Tree * T, int z)
{
if(T==NULL)
{
T=(struct Tree *)malloc(sizeof(struct Tree*));
T->key=z;
T->Left=T->Right=NULL;
}
if(z<T->key)
T->Left=InsertTree(T->Left, z);
else if(z>T->key)
T->Right=InsertTree(T->Right, z);
return T; } void Inorder(struct Tree *T)
{
/*if(T!=NULL)
{
if(T->Left)
Inorder(T->Left);
cout << T->key << endl;
if(T->Right)
Inorder(T->Right);
}*/
if(T)
{
Inorder(T->Left);
cout << T->key << endl;
Inorder(T->Right);
}
} void Preorder(struct Tree *T)
{
if(T!=NULL)
{
cout << T->key << endl;
if(T->Left)
Preorder(T->Left);
if(T->Right)
Preorder(T->Right);
}
} void Postorder(struct Tree *T)
{
if(T!=NULL)
{
if(T->Left)
Postorder(T->Left);
if(T->Right)
Postorder(T->Right);
cout << T->key << endl;
}
} DataType Minimum(struct Tree *T)
{
while(T->Left)
T=T->Left;
return T->key;
} DataType Maximum(struct Tree *T)
{
while(T->Right)
T=T->Right;
return T->key; } struct Tree * Delete(struct Tree *T, int z)
{
struct Tree *Tmp;
if(T==NULL)
cout << "We don't have enough node to delete!\n";
else
if(z<T->key)
T->Left=Delete(T->Left, z);
else
if(z>T->key)
T->Right=Delete(T->Right, z);
else if(T->Left&&T->Right)
{
Tmp=FindMin(T->Right);
T->key=Tmp->key;
T->Right=Delete(T->Right, T->key);
}
else
{
Tmp=T;
if(T->Left==NULL)
T=T->Right;
else if(T->Right==NULL)
T=T->Left;
free(Tmp);
}
return T;
} struct Tree * FindMin(struct Tree *T)
{
while(T->Left)
T=T->Left;
return T;
} struct Tree * FindMax(struct Tree *T)
{
while(T->Right)
T=T->Right;
return T;
}
BST的实现(二叉搜索树)的更多相关文章
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- BST | 1064 完全二叉搜索树
OJ:https://www.patest.cn/contests/pat-a-practise/1064 (一)23分(3个case未过)代码 建树的规律是我瞎猜的.首先用样例数据分析. 对数据排序 ...
- 二叉搜索树(BST)详解
前言:平衡树的前置知识吧 二叉搜索树的定义: 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于或等于它的根节点的值: (2)若右子树不空,则右子 ...
- BST(二叉搜索树)的基本操作
BST(二叉搜索树) 首先,我们定义树的数据结构如下: public class TreeNode { int val; TreeNode left; TreeNode right; public T ...
- [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 二叉搜索树-php实现 插入删除查找等操作
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...
- bst 二叉搜索树简单实现
//数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...
- 在二叉搜索树(BST)中查找第K个大的结点之非递归实现
一个被广泛使用的面试题: 给定一个二叉搜索树,请找出其中的第K个大的结点. PS:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...
随机推荐
- php代码Xdebug调试使用笔记
0x01 Xdebug简介 Xdebug是一个开放源代码的PHP程序调试器 运行流程: 0x02 Xdebug配置 日志 xdebug.trace_output_dir: 日志追踪输出目录 xdeb ...
- PHP call_user_func的一些用法和注意点
版本:PHP 5.6.28 在call_user_func的调用中: 1.参数的传递过程,并不是引用传值. 1 error_reporting(E_ERROR); // 此处不是E_ALL 2 $cu ...
- [USACO10NOV]奶牛的图片Cow Photographs
题目描述 Farmer John希望给他的N(1<=N<=100,000)只奶牛拍照片,这样他就可以向他的朋友炫耀他的奶牛. 这N只奶牛被标号为1..N. 在照相的那一天,奶牛们排成了一排 ...
- Mqtt-Client
客户端选择:paho MQTT client. 使用vs2013编译成库 需要用到paho-mqtt3a库和paho-mqtt3c库.
- MariaDB数据库
MySQL作者Michael Widenius自己创办了新公司Monty Program AB,在MySQL基础上新创了MariaDB开源数据库.MariaDB带来更好的数据库管理特性,更好的自由 ...
- Ubuntu16.04搭建boost环境
下载地址:http://sourceforge.net/projects/boost/files/boost/1.58.0/boost_1_58_0.tar.bz2/download 编译前所需的库 ...
- API设计中防重放攻击
HTTPS数据加密是否可以防止重放攻击? 否,加密可以有效防止明文数据被监听,但是却防止不了重放攻击. 防重放机制 我们在设计接口的时候,最怕一个接口被用户截取用于重放攻击.重放攻击是什么呢?就是把你 ...
- 推荐一款简单易用线上引流测试工具:GoReplay
一. 引流测试产生背景 日常大部分的测试工作都是在测试环境下,通过模拟用户的行为来对系统进行验证,包括功能以及性能.在这个过程中,你可能会遇到以下问题: 用户访问行为比较复杂,模拟很难和用户行为一致, ...
- 5.分析snkrs的Android的DeviceID生产规则
分析Android的DeviceID生产 前面已经把web端的分析了一些,要想实现其实容易也难,容易是规则很容易,难是时间生成控制很难,我之前大概花了一周时间分析和梳理,然后行为测试,之前我已经讲过c ...
- fenby C语言 P6
printf=格式输出函数; printf=("两个相加的数字是:%d,%d,他们的和是:%d\n",a,b,c); %d整数方式输出; \n=Enter; int a=1; fl ...