BST(Binary Search Tree)
原文链接:http://blog.csdn.net/jarily/article/details/8679280
/******************************************
数据结构:
BST(Binary Search Tree),二叉查找树; 性质:
若结点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若结点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
该结点的左、右子树也分别为二叉查找树; 遍历:
对于一个已知的二叉查找树,从小到大输出其节点的值;
只需对其进行二叉树的中序遍历即可;
即递归地先输出其左子树,再输出其本身,然后输出其右子树;
遍历的时间复杂度为O(n); 查找:
对于一个已知的二叉查找树x;
在其中查找特定的值k,函数Search返回指向值为k的节点指针;
若找不到则返回0,算法时间复杂度为O(h),h为树的高度;
理想情况下时间复杂度为lgn; 最大值和最小值:
要查找二叉查找树中具有最小值的元素;
只要从根节点开始,沿着左子树找到最左边的节点就可以了;
反之沿着右子树查找则可以求最大值; 插入:
从根节点开始插入;
如果要插入的值小于等于当前节点的值,在当前节点的左子树中插入;
如果要插入的值大于当前节点的值,在当前节点的右子树中插入;
如果当前节点为空节点,在此建立新的节点,该节点的值为要插入的值,左右子树为空,插入成功; 删除:
如果该没有子女,直接删除;
如果该结点只有一个子女,则删除它,将其子女的父亲改为它的父亲;
如果该结点有两个子女,先用其后继替换该节点,其后继的数据一并加在其后;
*******************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<climits>
#include<algorithm>
using namespace std; const int N = ;
int key[N], l[N], r[N], p[N];
int u, node; int Search(int x, int k)//查询
{
if(x == || k == key[x])
return x;
if(k < key[x])
return Search(l[x], k);
else
return Search(r[x], k);
} int Iterative_Search(int x, int k)//非递归版本的查询
{
while(x != && k != key[x])
if(k < key[x])
x = l[x];
else
x = r[x];
return x;
} int Minimum(int x)
{
while(l[x] != )
x = l[x];
return x;
} int Maximum(int x)
{
while(r[x] != )
x = r[x];
return x;
} int Successor(int x)
{
if(r[x] != )
return Minimum(r[x]);
int y = p[x];
while(y != && x == r[y])
{
x = y;
y = p[y];
}
return y;
} int Predecessor(int x)
{
if(l[x] != )
return Maximum(l[x]);
int y = p[x];
while(y != && x == l[y])
{
x = y;
y = p[y];
}
return y;
} void Insert(int &T, int v)//插入结点
{
if(T == )
key[T = ++node] = v;
else if(v <= key[T])
{
p[l[T]] = T;
Insert(l[T], v);
}
else
{
p[r[T]] = T;
Insert(r[T], v);
}
} void Iterative_Insert(int T, int v)//非递归版本插入结点
{
int y = ;
int x = T;
int z = ++node;
key[z] = v;
while(x != )
{
y = x;
if(key[z] < key[x])
x = l[x];
else
x = r[x];
}
p[z] = y;
if(y == )
key[T] = z;
else if(key[z] < key[y])
l[y] = z;
else
r[y] = z;
} void Transplant(int T, int u, int v)//移植过程;
//把一棵子树u归并到另一棵子树v中,u的父亲变为v的父亲,u的父亲就有了v作为其孩子。
{
if(p[u] == )
T = v;
else if(u == l[p[u]])
l[p[u]] = v;
else
r[p[u]] = v;
if(v != )
p[v] = p[u];
} void Delete(int T, int z)//删除结点
{
if(l[z] == )
Transplant(T, z, r[z]);
else if(r[z] == )
Transplant(T, z, l[z]);
else
{
int y = Minimum(r[z]);
if(p[y] != z)
{
Transplant(T, y, r[y]);
r[y] = r[z];
p[r[y]] = y;
}
Transplant(T, z, y);
l[y] = l[z];
p[l[y]] = y;
}
} int main()
{
int n;
scanf("%d",&n);
for(int i=; i<n; i++)
{
int k;
scanf("%d",&k);
Insert(u, k);
}
Delete(u, Search(u, ));
printf("%d\n",Search(u,));
printf("%d\n",Maximum(u));
return ;
}
BST(Binary Search Tree)的更多相关文章
- 二叉搜索树BST(Binary Search Tree)
		
二叉搜索树(Binary Search Tree)也叫二叉排序树或二叉查找树.它满足以下性质: 1.非空左子树的所有键值小于其根结点的键值: 2.非空右子树的所有键值大于其根结点的键值: 3.左右子树 ...
 - 数据结构-二叉搜索树(BST binary search tree)
		
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...
 - 【题解】【BST】【Leetcode】Validate Binary Search Tree
		
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
 - Lowest Common Ancestor of a Binary Search Tree (BST)
		
Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...
 - 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
		
题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...
 - (BST 递归) leetcode98. Validate Binary Search Tree
		
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
 - UVA 1264 - Binary Search Tree(BST+计数)
		
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
 - PAT 1099 Build A Binary Search Tree[BST性质]
		
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
 - 501. Find Mode in Binary Search Tree查找BST中的众数
		
[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...
 
随机推荐
- 乐在其中设计模式(C#) - 外观模式(Facade Pattern)
			
原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...
 - ArcPad 10 的安装部署
			
ArcPad是安装在手持设备或者移动终端的一个外业ArcGIS产品,也就是说ArcPad是Esri的一款软件产品,而不是硬件设备哦.尽管不比ArcGIS Desktop功能复杂缤纷,可是对于野外作业. ...
 - JS call与apply
			
JS的call与apply call和apply是JS中比较重要的两个方法, 一般在框架和组件设计中用的较多,比如jQuery Code. 那么这两个方法是做什么的呢,下面我们通过代码来了解: 1 f ...
 - [精华]Hadoop,HBase分布式集群和solr环境搭建
			
1. 机器准备(这里做測试用,目的准备5台CentOS的linux系统) 1.1 准备了2台机器,安装win7系统(64位) 两台windows物理主机: 192.168.131.44 adminis ...
 - 【原创】构建高性能ASP.NET站点之二 优化HTTP请求(前端)
			
原文:[原创]构建高性能ASP.NET站点之二 优化HTTP请求(前端) 构建高性能ASP.NET站点之二 优化HTTP请求(前端) 前言: 这段时间比较的忙,文章写不是很勤,希望大家谅解. 上一篇文 ...
 - Netty In Action中文版 - 第一章:Netty介绍
			
本章介绍 Netty介绍 为什么要使用non-blocking IO(NIO) 堵塞IO(blocking IO)和非堵塞IO(non-blocking IO)对照 Java NIO的问题和在Nett ...
 - SQLSERVER存储过程语法的具体解释
			
SQL SERVER存储过程语法: Create PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } ...
 - string 至 Color 转换演示示例:
			
string colorstr = "#FF4D4D4D";string hex = colorstr.ToString().Replace("#", &quo ...
 - 第三篇——第二部分——第二文 计划搭建SQL Server镜像
			
原文:第三篇--第二部分--第二文 计划搭建SQL Server镜像 本文紧跟上一章:SQL Server镜像简介 本文出处:http://blog.csdn.net/dba_huangzj/arti ...
 - 【转】JAVA 网络编程
			
网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...