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. Stack Overflow 上 250W 浏览量的一个问题:你对象丢了

    在逛 Stack Overflow 的时候,发现最火的问题竟然是:什么是 NullPointerException(java.lang.NullPointerException),它是由什么原因导致的 ...

  2. 本地向服务器上传文件的方式-FTP工具上传

    笔者负责的一个研究生会的项目,向服务器端传项目代码,用到了FTP工具,这里总结下: FTP方式的步骤: 1,服务器端配置好FTP,(若没有,可网上下载一个服务器端安装的FTP).停止服务后,可以配置账 ...

  3. 理解JavaScript中的堆和栈

    这里先说两个概念:1.堆(heap)2.栈(stack)堆 是堆内存的简称.栈 是栈内存的简称.说到堆栈,我们讲的就是内存的使用和分配了,没有寄存器的事,也没有硬盘的事.各种语言在处理堆栈的原理上都大 ...

  4. 数据结构2_java---栈,括号匹配

    package Main; import java.util.Scanner; import javax.swing.text.html.HTMLDocument.HTMLReader.Isindex ...

  5. 手机号码格式化显示javascript

    /*手机代码格式化一般与Object.oninput=function(){}连用*/ function phoneFormat(phone){ if(phone.length <= 3 ){ ...

  6. 如何在CentOS6.4系统上安装KVM虚拟机

    CentOS6.4系统上安装KVM虚拟机   备注:以下操作说明是经过实验验证后总结出来的笔录,有需要的朋友可以进行参考,以下是基于VMware12.5.2虚拟机版本上安装的实验环境. 一.安装KVM ...

  7. spring在IoC容器中装配Bean详解

    1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...

  8. Java8新特性 - Stream API

    Stream是Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找.过滤和映射数据等操作.使用Stream API对集合进行操作,就类似与使用SQL执行的数据库 ...

  9. 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(一)

    MySQL的行(记录)的操作(一) 1. 增(insert) insert into 表名 value((字段1,字段2...); # 只能增加一行记录 insert into 表名 values(字 ...

  10. 存储路径与文件目录操作ZT

    转自:https://www.cnblogs.com/zrr-notes/p/5953445.html (一)基本存储位置 我们的app在手机中存放的路径是:/var/mobile/Applicati ...