二叉搜索树

  二叉搜索树:

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

struct Node{
    int data;
    struct Node *left;
    struct Node *right;
};

typedef struct Node * BTree;
typedef BTree Position;

BTree Insert(BTree bt, int x)
{
    if(!bt){ //节点不存在,则创建一个节点,并返回
        bt = (BTree)malloc(sizeof(struct Node));
        bt->data= x;
        bt->left = bt->right = NULL;
    } else {
        if (x < bt->data) {
            bt->left = Insert(bt->left, x);
        } else if (x > bt->data) {
            bt->right = Insert(bt->right, x);
        }
    }
    return bt;
}

//递归实现二叉树搜索
Position Find_recursive(int x,BTree bt)
{
    if (!bt) {
        return NULL;
    }
    if (x > bt->data) {
        return Find_recursive(x, bt->right);
    } else if (x < bt->data) {
        return Find_recursive(x, bt->left);
    } else {
        return bt;
    }
}

//迭代实现二叉树搜索
Position Find_iterative(int x, BTree bt)
{
    while (bt) {
        if (x < bt->data) {
            bt = bt->left;
        } else if (x > bt->data){
            bt = bt->right;
        } else {
            return bt;
        }
    }
    return NULL; //未找到
}

//递归查找最小值
Position FindMin_recursive(int x, BTree bt)
{   //最小值在最左边
    if (!bt) {
        return NULL;
    }
    if (!bt->left) { //没有左孩子,证明是最左边的节点
        return bt;
    } else {
        return FindMin_recursive(x, bt->left);
    }
}

//迭代查找最小值
Position FindMin_iterative(int x, BTree bt)
{
    if (bt){
        while(bt->left){
            bt = bt->left;
        }
    }
    return bt;
}

//递归查找最大值
Position FindMax_recursive(int x, BTree bt)
{
    //最大值在最右边
    if (!bt) {
        return NULL;
    }
    if (!bt->right) {
        return bt;
    } else {
        return FindMax_recursive(x, bt->right);
    }
}

//迭代查找最大值
Position FindMax_iterative(int x, BTree bt)
{
    if (bt) {
        while (bt->right) {
            bt = bt->right;
        }
    }
    return bt;
}

//删除搜索二叉树的某个节点
BTree Delete(int x, BTree bt)
{
    Position tmp;
    if (!bt) {
        printf("未找到要查找的元素");
        return NULL;
    }
    if (x < bt->data) {
        //要删的元素可能在左边
        bt->left = Delete(x, bt->left);
    } else if (x > bt->data) {
        //要删的元素可能在右边
        bt->right = Delete(x, bt->right);
    } else {
        bool strategy = true;
        //true表示使用右子树的最小值顶替删除的节点位置
        //false表示使用左子树的最大值顶替删除的节点位置

        //找到要删的节点
        if (bt->left && bt->right) {
            //左右子树都存在
            if (strategy) {
                tmp = FindMin_iterative(x, bt->right);
                bt->data = tmp->data;
                bt->right = Delete(bt->data, bt->right);
            } else {
                tmp = FindMax_iterative(x, bt->left)
                bt->data = tmp->data;
                bt->left = Delete(bt->data, bt->left);
            }
        } else {
            //没有子树,没有左子树或右子树
            tmp = bt;
            if (!bt->left) {
                //左子树为空,则直接用右子树来顶替
                bt = bt->right;
            } else if (!bt->right){
                bt = bt->left;
            }
            free(tmp);
        }
    }
    return bt;
}

  

posted @ 2018-07-25 00:39  寻觅beyond  阅读(193)  评论(0)    收藏  举报
返回顶部