二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树:

  • 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值

  • 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值

  • 任意节点的左、右子树也分别为二叉查找树

  • 没有键值相等的节点(no duplicate nodes)

本文地址:http://www.cnblogs.com/archimedes/p/binary-search-tree.html,转载请注明源地址。

二叉查找树相比于其他数据结构的优势在于查找、插入的时间复杂度较低。为O(log n)。

二叉查找树是基础性数据结构,用于构建更为抽象的数据结构,如集合、multiset、关联数组等。

二叉查找树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉查找树的存储结构。中序遍历二叉查找树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉

查找树变成一个有序序列,构造树的过程即为对无序序列进行查找的过程。每次插入的新的结点都是二叉查找树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动

某个结点的指针,由空变为非空即可。搜索,插入,删除的复杂度等于树高,期望O(log n),最坏O(n)(数列有序,树退化成线性表).

虽然二叉查找树的最坏效率是O(n),但它支持动态查询,且有很多改进版的二叉查找树可以使树高为O(logn),如:SBT,AVL,红黑树等.故不失为一种好的动态查找方法.

基本操作实现:

1、二叉查找树声明

/*********二叉查找树声明 ********/
typedef struct tree_node *tree_prt;
struct tree_node {
element_type element;
tree_ptr left;
tree_prt right;
};
typedef tree_ptr SEARCH_TREE;

2、查找操作

思路:若根结点的关键字等于查找的关键字,查找成功;否则,若小于根结点的关键字的值,递归查找左子树,否则若大于根结点的关键字的值,递归查找右子树,若子树为空,则查找不成功

/*********查找算法 ********/
tree_ptr find(element_type x, SEARCH_TREE T)
{
if(T ==NULL)
return NULL;
if(x < T->element)
return (find(x, T->left));
else if(x > T->element)
return (find(x, T->right));
else
return T;
}

3、查找最大最小结点

/*********查找最大最小结点 ********/
tree_ptr find_min(SEARCH_TREE T) //递归
{
if(T == NULL)
return NULL;
else if(T->left == NULL)
return T;
else
return find_min(T->left);
}
tree_ptr find_max(SEARCH_TREE T) //非递归
{
if(T != NULL) {
while(T->right != NULL) {
T = T->right;
}
}
return T;
}

4、插入操作

思路:首先执行查找算法,找出被插入结点的父结点,判断被查结点是父结点的左孩子还是右孩子,将被插结点作为叶子结点插入,若二叉树为空,则首先单独生成根结点

/*********插入结点1 ********/
void insert(element_type x, SEARCH_TREE *T)
{
if(*T == NULL) { /* 空树 */
*T = (SEARCH_TREE)malloc(sizeof(struct tree_node));
if(*T == NULL) {
printf("Out of space!!!");
return;
} else {
(*T)->element = x;
(*T)->left = (*T)->right = NULL;
}
} else if(x < (*T)->element) {
insert(x, &((*T)->left));
} else {
insert(x, &((*T)->right));
}
}

当然也可以使用返回插入结点的方式:

/*********插入结点2 ********/
tree_ptr insert(element_type x, SEARCH_TREE T)
{
if(T == NULL) { /* 空树 */
T = (SEARCH_TREE)malloc(sizeof(struct tree_node));
if(T == NULL) {
printf("Out of space!!!");
return;
} else {
T->element = x;
T->left = T->right = NULL;
}
} else if(x < T->element) {
T->left = insert(x, T->left));
} else {
T->right = insert(x, T->right));
}
return T;
}

5、删除操作

在二叉查找树删去一个结点,分三种情况讨论:

①  若p是叶子结点: 直接删除p,如图(b)所示。

②  若p只有一棵子树(左子树或右子树):直接用p的左子树(或右子树)取代p的位置而成为f的一棵子树。即原来p是f的左子树,则p的子树成为f的左子树;原来p是f的右子树,则p的子树成为f的右子树,如图(c)、 (d)所示。

③ 若p既有左子树又有右子树 :处理方法有以下两种,可以任选其中一种。

◆  用p的直接前驱结点代替p。即从p的左子树中选择值最大的结点s放在p的位置(用结点s的内容替换结点p内容),然后删除结点s。s是p的左子树中的最右边的结点且没有右子树,对s的删除同②,如图(e)所示。

◆ 用p的直接后继结点代替p。即从p的右子树中选择值最小的结点s放在p的位置(用结点s的内容替换结点p内容),然后删除结点s。s是p的右子树中的最左边的结点且没有左子树,对s的删除同②。

void delete(SEARCH_TREE *p)
{
SEARCH_TREE q, s;
if((*p)->right == NULL) {
q = *p;
*p = (*p)->left;
free(q);
} else if((*p)->left == NULL) {
q = *p;
*p = (*p)->right;
free(q);
} else {
q = *p;
s = (*p)->left;
while(s->right != NULL) {
q = s;
s = s->right;
}
(*p)->element = s->element;
if(q != p) {
q->right = s->left;
} else {
q ->left = s->left;
}
}
free(s);
}
void deleteBST(SEARCH_TREE *T, element_type key)
{
if(!(*T)) {
return;
} else if ((*T)->element == key) {
free(*T);
} else if((*T)->element > key) {
deleteBST((*)T->left, key);
} else {
deleteBST((*)T->right, key);
}
}

编程实践

poj2418 Hardwood Species

#include<stdio.h>
#include<stdlib.h>
#include<string.h> struct node {
char name[];
struct node *lchild, *rchild;
int count;
}tree;
struct node *root;
int n = ;
void mid_cal(struct node *root)
{
if(root != NULL) {
mid_cal(root->lchild);
printf("%s %.4lf\n", root->name, ((double)(root->count) / (double)n) * 100.0);
mid_cal(root->rchild);
}
} void insertBST(struct node** root, char *s)
{
if(*root == NULL) {
struct node *p = (struct node*)malloc(sizeof(tree));
strcpy(p->name, s);
p->lchild = p->rchild = NULL;
p->count = ;
*root = p;
} else {
if(strcmp(s, (*root)->name) == ) {
((*root)->count)++;
return;
} else if(strcmp(s, (*root)->name) < ) {
insertBST(&((*root)->lchild), s);
} else {
insertBST(&((*root)->rchild), s);
}
}
} int main()
{
char s[];
while(gets(s)) {
insertBST(&root, s);
n++;
}
mid_cal(root);
return ;
}

参考资料

1、Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein(潘金贵等译)《算法导论》. 机械工业出版社.

2、ACM/ICPC 算法训练教程

3、《数据结构》严蔚敏、吴伟民

4、维基百科

二叉查找树(binary search tree)详解的更多相关文章

  1. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  2. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  3. 【数据结构与算法Python版学习笔记】树——二叉查找树 Binary Search Tree

    二叉搜索树,它是映射的另一种实现 映射抽象数据类型前面两种实现,它们分别是列表二分搜索和散列表. 操作 Map()新建一个空的映射. put(key, val)往映射中加入一个新的键-值对.如果键已经 ...

  4. 数据结构之Binary Search Tree (Java)

    二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...

  5. 二叉搜索树(Binary Search Tree)(Java实现)

    @ 目录 1.二叉搜索树 1.1. 基本概念 1.2.树的节点(BinaryNode) 1.3.构造器和成员变量 1.3.公共方法(public method) 1.4.比较函数 1.5.contai ...

  6. 【二叉查找树】03验证是否为二叉查找树【Validate Binary Search Tree】

    本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  7. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  8. 【LeetCode】Validate Binary Search Tree 二叉查找树的推断

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...

  9. Python3解leetcode 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 ...

随机推荐

  1. SQL SERVER 使用select和union插入多条数据

    insert into A(A) select '2' union select '3' union select '100' go select * from A

  2. Android抽象布局——include、merge 、ViewStub

    http://blog.csdn.net/xyz_lmn/article/details/14524567

  3. Lua中的协同程序 coroutine

    Lua中的协程和多线程很相似,每一个协程有自己的堆栈,自己的局部变量,可以通过yield-resume实现在协程间的切换.不同之处是:Lua协程是非抢占式的多线程,必须手动在不同的协程间切换,且同一时 ...

  4. Oracle实例和服务知识点

    shutdown是对实例而言  service是启动的,根本不代表instance就是启动的. 启动数据库基本可分为三个过程: 1,nomount(即只启动instance,而不加载数据库) 2,mo ...

  5. 2014 网选 5007 Post Robot(暴力或者AC_自动机(有点小题大作了))

    //暴力,从每一行的开始处开始寻找要查询的字符 #include<iostream> #include<cstdio> #include<cstring> #inc ...

  6. iOS-微信-分享

    一.微信原生的分享--准备工作. 1. 需要申请微信AppId. 2. 导入系统架包. SDK文件包括 libWeChatSDK.a,WXApi.h,WXApiObject.h,WechatAuthS ...

  7. node log4js包

    http://blog.csdn.net/heiantianshi1/article/details/43984601

  8. Configuring Service Broker for Asynchronous Processing

    Configuring Service Broker for Asynchronous Processing --create a database and enable the database f ...

  9. ChartDirector应用笔记(二)

    关于Simple Bar Chart Simple bar chart是XYChart大类中的Bar chart类型中的最简单的例子.Bar chart的表现形式简单直观,在数据量较少.数据维度简单等 ...

  10. 微信公众平台入门开发教程.Net(C#)框架

    一.序言 一直在想第一次写博客,应该写点什么好?正好最近在研究微信公众平台开发,索性就记录下,分享下自己的心得,也分享下本人简单模仿asp.net运行机制所写的通用的微信公众平台开发.Net(c#)框 ...