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. Mysql综述(1)数据是如何读存的

    引言 我们都知道,mysql中的索引,事务,锁等都是作为开发人员要重点掌握的知识面,但要想掌握理解好这些知识却并非易事. 其中原因之一就是这些概念都过于抽象,事实上如果都不懂mysql数据是以一种怎样 ...

  2. HelloWin详解

    (注意:遇到程序在弄懂之后一定要自己去敲,一定要自己去敲,一定要自己去敲) (注意:遇到程序在弄懂之后一定要自己去敲,一定要自己去敲,一定要自己去敲) (注意:遇到程序在弄懂之后一定要自己去敲,一定要 ...

  3. 渗透-简单制作过waf的中国菜刀

    0x01 简单分析 web渗透中很常见的情况,用菜刀连接免杀的一句话木马连不上,有waf 除了变形一句话木马为免杀一句话,我们还需要来制作过waf的菜刀进行连接. 这里用的一句话为 来看看菜刀连接一句 ...

  4. [网络流 24 题] luoguP4016 负载平衡问题

    [返回网络流 24 题索引] 题目描述 有成环状的 nnn 堆纸牌,现将一张纸牌移动到其邻堆称为一次操作.求使得所有堆纸牌数相等的最少移动次数. Solution 4016\text{Solution ...

  5. Shell多进程执行任务

    展示代码 #!/bin/bash trap "exec 1000>&-;exec 1000<&-;exit 0" 2 # 分别为 创建管道文件,文件操作 ...

  6. MS09-012 PR提权

     漏洞编号:MS09-012 披露日期: 2009/4/14 受影响的操作系统:Windows 2008 x64 x86;XP;Server 2003 sp1 sp2; 测试系统:windows 20 ...

  7. 巨杉Tech | SequoiaDB数据域及存储规划

    1 背景近年来,企业的各项业务发展迅猛,客户数目不断增加,后台服务系统压力也越来越大,系统的各项硬件资源也变得非常紧张.因此,在技术风险可控的基础上,希望引入大数据技术,利用大数据技术优化现有IT系统 ...

  8. 面试题-javascript-面向对象编程

    笔者在某次笔试中遇到这个题:印象很深. function ClassA() { var value=4; this.getValue= function() { return value; } thi ...

  9. 网页开发利用jq自定义鼠标右击事件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 超简单让.NET Core开发者快速拥有CI/CD的能力-Docker版本

    超简单让.NET Core开发者快速拥有CI/CD的能力-Docker版本 前言 上一篇自动化测试,全面且详细的介绍了从零开始到发布版本的步骤,这是传统的方式,本次为大家带来的是如何在5分钟内使用上d ...