二叉查找树(binary search tree)详解

简介:

二叉查找树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[31];
    struct node *lchild, *rchild;
    int count;
}tree;
struct node *root;
int n = 0;
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 = 1;
        *root = p;
    } else {
        if(strcmp(s, (*root)->name) == 0) {
            ((*root)->count)++;
            return;
        } else if(strcmp(s, (*root)->name) < 0) {
            insertBST(&((*root)->lchild), s);
        } else {
            insertBST(&((*root)->rchild), s);
        }
    }
}

int main()
{
    char s[31];
    while(gets(s)) {
        insertBST(&root, s);
        n++;
    }
    mid_cal(root);
    return 0;
}
目录
相关文章
|
6月前
|
Python
二叉搜索树(Binary Search Tree
二叉搜索树(Binary Search Tree,简称 BST)是一种特殊的二叉树结构,它的每个节点具有以下性质:
54 4
|
8月前
|
JavaScript
使用JS 实现二叉查找树(Binary Search Tree)
使用JS 实现二叉查找树(Binary Search Tree)
46 0
|
5月前
树(Tree)和二叉树(Binary Tree)——(代码篇)
树(Tree)和二叉树(Binary Tree)——(代码篇)
41 0
|
5月前
|
存储 分布式数据库
树(Tree)和二叉树(Binary Tree)——(概念篇)
树(Tree)和二叉树(Binary Tree)——(概念篇)
35 0
|
Python
[Leetcode][python]Unique Binary Search Trees/不同的二叉查找树
题目大意 给出一个n,求1-n能够得到的所有二叉搜索树
80 0
|
存储 Python
数据结构(二):二叉搜索树(Binary Search Tree)
二分法猜数字的游戏应该每个人都知道,通过对猜测数字“大了”、“小了”的情况判断,来猜出最终的数字。序列范围为 的集合,复杂度为 ,即最多需要 次可以猜到最终数字。
1585 0
对于B-Tree B+Tree 红黑二叉树我的理解
B-Tree和B+Tree主要区别就是B+Tree的非叶子节点不存储数据,只有叶子节点存储数据, 主要参考文章:容易看懂的B-Tree文章 百度百科-B-Tree百度百科-B+Tree
1813 0