二叉搜索树

二叉搜索树

#include <iostream>

using namespace std;

#define length 10
#define errorValue -1

int num;

struct node {
    node* left;
    node* right;
    int value;
    node(int a = errorValue, node* n1 = NULL, node* n2 = NULL) :value(a), left(n1), right(n2) {}
};

node* searchNode(node* root, int value) {
    if (root == NULL)
        return NULL;
    else if (root->value == value) {
        return root;
    }
    else if (root->value < value) {
        return searchNode(root->right, value);
    }
    else {
        return searchNode(root->left, value);
    }
}

//要注意使用引用
void insertTree(node*& root, int value) {
    if (root == NULL) {
        root = new node(value);
    }
    else if (root->value < value) {
        insertTree(root->right, value);
    }
    else if (root->value > value) {
        insertTree(root->left, value);
    }
    //重复则不做处理
}

void inOrderPrint(node* root) {
    if (root != NULL) {
        inOrderPrint(root->left);
        cout << root->value << ' ';
        inOrderPrint(root->right);
    }
}

void preOrderPrint(node* root) {
    if (root != NULL) {
        cout << root->value << ' ';
        preOrderPrint(root->left);
        preOrderPrint(root->right);
    }
}

void postOrderPrint(node* root) {
    if (root != NULL) {
        postOrderPrint(root->left);
        postOrderPrint(root->right);
        cout << root->value << ' ';
    }
}
//返回最大的值
int maxValue(node* root) {
    if (root == NULL)
        return errorValue;
    while (root->right != NULL) {
        root = root->right;
    }
    return root->value;
}
//返回最大的节点
node*& maxNode(node*& root) {
    while (root->right != NULL) {
        root = root->right;
    }
    return root;
}
//返回最小的值
int minValue(node* root) {
    if (root == NULL)
        return errorValue;
    while (root->left != NULL) {
        root = root->left;
    }
    return root->value;
}
//返回最小的节点
node*& minNode(node*& root) {
    while (root->left != NULL) {
        root = root->left;
    }
    return root;
}

//build a sorted array used to find value
void buildArr(int* a, node* root) {
    if (root != NULL) {
        buildArr(a, root->left);
        a[num++] = root->value;
        buildArr(a, root->right);
    }
}

//返回大于value的第一个值
int moreThanValue(node* root, int value) {
    int a[length] = { 0 };
    num = 0;
    buildArr(a, root);
    for (int i = 0; i < length; i++)
    {
        if (a[i] > value) {
            return a[i];
        }
    }
    return errorValue;
}


//返回小于value的第一个值
int smallerThanValue(node* root, int value) {
    int a[length] = { 0 };
    num = 0;
    buildArr(a, root);
    for (int i = 0; i < length; i++)
    {
        if (a[i] > value) {
            return i == 0 ? errorValue : a[i - 1];
        }
    }
    return errorValue;
}

//返回小于value节点的第一个值
int predecessor(node* root, int value) {
    node* temp = searchNode(root, value);
    if (temp == NULL)
        return errorValue;
    if (temp->left != NULL)
        return maxValue(temp->left);
    else
        return errorValue;
}

//返回大于value节点的第一个值
int successor(node* root, int value) {
    node* temp = searchNode(root, value);
    if (temp == NULL)
        return errorValue;
    if (temp->right != NULL)
        return minValue(temp->right);
    else
        return errorValue;
}

bool deleteNode(node*& root, int value) {
    if (root == NULL)
        return false;
    else if (root->value < value)
        return deleteNode(root->right, value);
    else if (root->value > value)
        return deleteNode(root->left, value);
    else {
        if (root->left == NULL && root->right == NULL)
        {
            delete root;
            root = NULL;
        }
        else if (root->left != NULL && root->right != NULL)
        {
            //删除节点右边最小值的点,将其移至上方
            root->value = minValue(root->right);
            deleteNode(root->right, root->value);
        }
        else {
            node* temp = root;
            root = root->left == NULL ? root->right : root->left;
            delete temp;
            temp = NULL;
        }
        return true;
    }
}

void emptyTree(node*& root) {
    if (root == NULL)
        return;
    else {
        emptyTree(root->right);
        emptyTree(root->left);
        delete root;
        root = NULL;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值