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:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...
随机推荐
- 【Medium 万赞好文】ViewModel 和 LIveData:模式 + 反模式
原文作者: Jose Alcérreca 原文地址: ViewModels and LiveData: Patterns + AntiPatterns 译者:秉心说 View 和 ViewModel ...
- SDL建设-三方依赖库扫描
说明: 本文首发自 https://www.secpulse.com/archives/73373.html,转载请注明出处. 文章综述 本文主要介绍Dependency-Check工具的工作原理和使 ...
- 初识AutoIt
1.定义 AutoIt 目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作.它利用模拟键盘按键,鼠标移动和窗口/控件的组 ...
- postman-windows下newman的使用
一.newman的安装 1.安装node.js下载https://nodejs.org/en/ C:\Users\iphauser>node -vv10.16.1C:\Users\iphause ...
- {每日一题}:tcp协议实现简单的文件下载器(单任务版)
文件下载器客户端 这个版本的只是为了方便回顾一下TCP客服端,服务端的创建流程,缺点就是 服务器一次只能让一个人访问下载,过两个写个使用面向对象写一个多线程版的强化一下. from socket i ...
- 5. Sersync实时同步
rsync+Sersync数据的实时同步 sersync介绍 1.什么是实时同步 监控一个目录的变化, 当该目录触发事件(创建\删除\修改) 就执行动作, 这个动作可以是 rsync同步 ,也可以是其 ...
- C语言1作业004
这个作业属于哪个课程 C语言作业004 我在这个课程的目标是 理解和掌握for语句的基本操作 这个作业在哪个具体方面帮助我实现目标 循环结构的应用,数学函数基本问题 参考文献 C语言程序设计(第3版) ...
- 百万年薪python之路 -- 基础数据类型的补充
基础数据类型的补充 str: 首字母大写 name = 'alexdasx' new_name = name.capitalize() print(new_name) 通过元素查找下标 从左到右 只查 ...
- FormData交互以及Node multiparty插件的使用
一.FormData FormData是ajax2.0里面添加的新特性. FormData的主要用途有两个: (1).将form表单元素的name与value进行组合,实现表单数据的序列化,从而减少表 ...
- Python装饰器基础
一.Python装饰器引入 讲 Python 装饰器前,我想先举个例子,虽有点污,但跟装饰器这个话题很贴切. 每个人都有的内裤主要功能是用来遮羞,但是到了冬天它没法为我们防风御寒,咋办?我们想到的一个 ...