首页 > 其他 > 详细

二叉搜索树

时间:2018-03-16 17:55:26      阅读:171      评论:0      收藏:0      [点我收藏+]

一、什么是二叉搜索树

1.二叉搜索树,对任何结点x,其左子树中的关键字最大不超过x.key,其右子树中的关键字最小不小于x.key。
2.一个二叉搜索数第i层的最大结点数为 2^(i-1),i>=1
3.深度为k的二叉搜索数的最大结点数为 2^k - 1, k>=1
4.对任意非空的二叉树T,若n0表示叶结点的个数,n2表示树中度为2的非叶结点个数,则n0 = n2 + 1

二、如何存储一个二叉搜索树

1.顺序存储结构:

三、查询二叉搜索树

一、考虑三种情况
1. 要删除的是叶结点:直接删除,修改父结点的指针置为Null
2. 要删除的结点只有一个子结点:将其父结点的指针指向其子结点
3. 要删除的结点有两个子结点:用其左子树的最大元素或者右子树的最小元素替换掉要删除的结点。注意都是叶结点。

四、二叉搜索树的特点

二叉树搜索树能够保证树的操作是O(lgN)

五、二叉搜索树的实现

#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<stdbool.h>

typedef int elementType;

typedef struct treeNode
{
    elementType element;
    struct treeNode *left;
    struct treeNode *right;
} *position, *searchTree;

bool isEmpty(searchTree t);
searchTree makeEmpty(searchTree t);
position find(elementType x, searchTree t);
position findMin(searchTree t);
position findmax(searchTree t);
searchTree insert(elementType x, searchTree t);
searchTree delete(elementType x, searchTree t);
elementType retrieve(position p);
void preOrderBST(searchTree t);
void inOrderBST(searchTree t);
int main()
{
    searchTree t = NULL;
    int data[10] = {1, 3, 4, 2, 0, 8, 13, -2, -5, 18};
    for (int i = 0; i < 10; i++) {
        t = insert(data[i], t);
    }
    //position node = find(8, t);
    //printf("%p", node);
    inOrderBST(t);
    delete(8, t);
    inOrderBST(t);
}

searchTree makeEmpty(searchTree t)
{
    if (t != NULL) {
        makeEmpty(t->left);
        makeEmpty(t->right);
        free(t);
    }
    return NULL;
}

position find(elementType x, searchTree t)
{
    while(t != NULL && t->element != x) {
        if (x < t->element) {
            t = t->left;
        } else if (x > t->element) {
            t = t->right;
        }
    }
    return t;
}

bool isEmpty(searchTree t)
{
    return t == NULL;
}

position findMin(searchTree t)
{
    if (t != NULL) {
        while (t->left != NULL) {
            t = t->left;
        }
    }
    return t;
}

position findMax(searchTree t)
{
    if (t != NULL) {
        while (t->right != NULL) {
            t = t->right;
        }
    }
    return t;
}

/**
 * 插入单个值
 */
searchTree insert(elementType x, searchTree t)
{
    if (t == NULL) {
        t = malloc(sizeof(struct treeNode));
        if (t == NULL) {
            exit(-1);
        }
        t->left = t->right = NULL;
        t->element = x;
    } else {
        if (x < t->element) {
            t->left = insert(x, t->left);
        } else if(x > t->element) {
            t->right = insert(x, t->right);
        } else {
        }
    }
    return t;
}

/**
 * 删除某个值
 */
searchTree delete(elementType x, searchTree t)
{
    if (t == NULL) {
        exit(-1);
    }
    position tmpCell;
    if (x < t->element) {
        t->left = delete(x, t->left);
    } else if (x > t->element) {
        t->right = delete(x, t->right);
    } else {
        // 替换为右子树的最小值,并递归删除该值
        if (t->left && t->right) {
            tmpCell = findMin(t->right);
            t->element = tmpCell->element;
            t->right = delete(t->element, t->right);
        } else {
            tmpCell = t;
            if (t->left == NULL) {
                t = t->right;
            } else if (t->right == NULL) {
                t = t->left;
            }
            free(tmpCell);
        }
    }
    return t;
}

/**
 * 先序输出
 */
void preOrderBST(searchTree t)
{
    if (isEmpty(t)) {
        printf("树为空");
        return;
    }
    printf("%d\n", t->element);
    preOrderBST(t->left);
    preOrderBST(t->right);
}

/**
 * 中序输出
 */
void inOrderBST(searchTree t)
{
    if (isEmpty(t)) {
        return;
    }
    inOrderBST(t->left);
    printf("%d\n", t->element);
    inOrderBST(t->right);
}

二叉搜索树

原文:https://www.cnblogs.com/yuanlairlj/p/8583713.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!