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的实现(二叉搜索树)的更多相关文章

  1. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. BST | 1064 完全二叉搜索树

    OJ:https://www.patest.cn/contests/pat-a-practise/1064 (一)23分(3个case未过)代码 建树的规律是我瞎猜的.首先用样例数据分析. 对数据排序 ...

  3. 二叉搜索树(BST)详解

    前言:平衡树的前置知识吧 二叉搜索树的定义: 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于或等于它的根节点的值: (2)若右子树不空,则右子 ...

  4. BST(二叉搜索树)的基本操作

    BST(二叉搜索树) 首先,我们定义树的数据结构如下: public class TreeNode { int val; TreeNode left; TreeNode right; public T ...

  5. [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 ...

  6. 二叉搜索树-php实现 插入删除查找等操作

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

  7. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...

  9. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  10. 在二叉搜索树(BST)中查找第K个大的结点之非递归实现

    一个被广泛使用的面试题: 给定一个二叉搜索树,请找出其中的第K个大的结点. PS:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...

随机推荐

  1. 使用java语言实现一个动态数组(详解)(数据结构)

    废话不多说,上代码 1.从类名开始(我真是太贴心了) public class Array<E> 首先数组类需要带有泛型,这个不多说.需要注意的是在java中,数组只能存放同一个类型的. ...

  2. PHP绕过disable_function限制(一)

    测试环境 php 5.4.5 0x01 利用系统组件绕过 1.window com组件(php 5.4)(高版本扩展要自己添加) (COM组件它最早的设计意图是,跨语言实现程序组件的复用.) 测试: ...

  3. Ubuntu16.04 安装apache+mysql+php(LAMP)

    记录下ubuntu环境下安装apache+mysql+php(LAMP)环境. 0x01安装apache sudo apt-get update sudo apt-get install apache ...

  4. vc++中输入表的免杀

    国外的杀毒软件一般会把特征码定位在PE文件的输入表函数(也就是源码里我们调用了的API函数)上, 我们对付这种查杀的方法就是在源码里对API函数进行动态调用,对一个函数动态调用之后,本来以输入 表函数 ...

  5. [NOIp2018] luogu P5020 货币系统

    还在补暑假作业. 题目描述 你有一个由 NNN 种面值的货币组成的货币系统.定义两个货币系统等价,当且仅当 ∀x∈N∗\forall x\in\N^*∀x∈N∗ 要么同时能被两个货币系统表示,要么同时 ...

  6. selenium-显式等待与隐式等待(3)

    示例代码: from selenium.webdriver.support.wait import WebDriverWait as WD def find_element(self, by, loc ...

  7. 5. Sersync实时同步

    rsync+Sersync数据的实时同步 sersync介绍 1.什么是实时同步 监控一个目录的变化, 当该目录触发事件(创建\删除\修改) 就执行动作, 这个动作可以是 rsync同步 ,也可以是其 ...

  8. sudo权限造成的故障

    公司服务器故障: [chengsir@yinwucheng ~]$ sudo mkdir /opt/nginx sudo: /usr/bin/sudo must be owned by uid 0 a ...

  9. Vue系列---理解Vue.nextTick使用及源码分析(五)

    _ 阅读目录 一. 什么是Vue.nextTick()? 二. Vue.nextTick()方法的应用场景有哪些? 2.1 更改数据后,进行节点DOM操作. 2.2 在created生命周期中进行DO ...

  10. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...