原文链接: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)的更多相关文章

  1. 二叉搜索树BST(Binary Search Tree)

    二叉搜索树(Binary Search Tree)也叫二叉排序树或二叉查找树.它满足以下性质: 1.非空左子树的所有键值小于其根结点的键值: 2.非空右子树的所有键值大于其根结点的键值: 3.左右子树 ...

  2. 数据结构-二叉搜索树(BST binary search tree)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...

  3. 【题解】【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 ...

  4. 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 ...

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

  6. (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 ...

  7. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 移动web:tab选项卡

    平常做移动端会用到tab选项卡,这和PC端有些区别,移动端是触摸滑动切换,PC端是点击.移入切换. 这里滑动切换就是一个移动端事件的应用,这里主要用到的触摸事件:touchstart.touchmov ...

  2. 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性

    原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...

  3. [LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  4. 【Android进阶】自定义控件实现底部扇形展开菜单效果

    这个项目是优化的其他人的,主要优化了界面菜单的显示,下面开始. 先看效果图 项目的总结构 下面开始贴代码,由于必要的地方都添加了注释,所以不过多讲解 anim_button.xml <?xml ...

  5. pragma message任务

    pragma message它是用来告诉程序猿,在编译的程序信息.和outputdebugstr则是告诉程序猿.程序在执行时期的信息. 以下就以一个样例来解说pragma message. 配合#if ...

  6. html/css获得第一章

    1.基本教程来学习 大概3天课余时间阅读下面的两个教程. HTML文字教程 CSS文字教程 2.练习 看完教程后.做第一练习时,总结例如以下: 1)div居中 须要设置属性:margin-left:a ...

  7. DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)

    本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...

  8. RH033读书笔记(13)-Lab 14 Network Clients

    Goal: Practice using a variety of tools to transfer files between your system and a remote system. S ...

  9. 怎么将Emeditor设置成网页查看源代码的默认编译器

    1.打开emditor: 2.在菜单栏中找到工具---->自定义,打开自定义窗口: 3.快捷方式--->更多快捷方式 5.选中“在internet explorer中通过emeditor查 ...

  10. div+js 弹出层

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...