前置说明
结点类型结构体:
typedef struct node {
int info;
struct node * left;
struct node * right;
}bstree;
中序遍历:
// inordertraversal
void print_bstree(bstree * T) {
if(!T) return;
print_bstree(T->left);
cout << T->info << " ";
print_bstree(T->right);
}
insert_node(二级指针)
void insert_node(int val, bstree** root) {
if (*root == NULL) {
bstree* newnode = new bstree;
newnode->info = val;
newnode->left = NULL;
newnode->right = NULL;
*root = newnode;
return;
}
if ((*root)->info > val) insert_node(val, &((*root)->left));
else insert_node(val, &((*root)->right));
}
delete_node(二级指针)
void delete_node(int val, bstree** root) {
if (*root == NULL) return;
if ((*root)->info > val) delete_node(val, &((*root)->left));
else if ((*root)->info < val) delete_node(val, &((*root)->right));
else {
if ((*root)->left == NULL) {
bstree* temp = (*root)->right;
delete (*root);
*root = temp;
}
else if ((*root)->right == NULL) {
bstree* temp = (*root)->left;
delete (*root);
*root = temp;
}
else {
bstree* p = (*root)->right;
while (p->left) p = p->left;
(*root)->info = p->info;
int min = p->info;
delete_node(min, &((*root)->right));
}
}
}
predecessor(前驱节点)
int predecessor(int val, bstree* root) {
if (root->info < val) {
MAX = root->info;
if (root->right == NULL) return MAX;
return predecessor(val, root->right);
}
else {
if (root->left == NULL) return MAX;
return predecessor(val, root->left);
}
}
successor(后继节点)
int successor(int val, bstree* root) {
if (root->info < val) {
if (root->right == NULL) return MIN;
return successor(val, root->right);
}
else {
MIN = root->info;
if (root->left == NULL) return MIN;
return successor(val, root->left);
}
}
search(搜索节点)
bstree* search(int val, bstree* T) {
if (T == NULL) return NULL;
if (val < T->info) return search(val, T->left);
else if (val > T->info) return search(val, T->right);
else return T;
}
search_max(最大值)
int search_max(bstree* T) {
if (T->right == NULL) return T->info;
return search_max(T->right);
}
search_min(最小值)
int search_min(bstree* T) {
if (T->left == NULL) return T->info;
return search_min(T->left);
}
关于二级指针在插入删除操作中的作用
上图展示了二级指针在删除节点时的具体过程。
完整代码
#include <iostream>
using namespace std;
typedef struct node {
int info;
struct node* left;
struct node* right;
}bstree;
int MAX = -1; // predecessor
int MIN = 100; // sucessor
void insert_node(int val, bstree** root) {
if (*root == NULL) {
bstree* newnode = new bstree;
newnode->info = val;
newnode->left = NULL;
newnode->right = NULL;
*root = newnode;
return;
}
if ((*root)->info > val) insert_node(val, &((*root)->left));
else insert_node(val, &((*root)->right));
}
void delete_node(int val, bstree** root) {
if (*root == NULL) return;
if ((*root)->info > val) delete_node(val, &((*root)->left));
else if ((*root)->info < val) delete_node(val, &((*root)->right));
else {
if ((*root)->left == NULL) {
bstree* temp = (*root)->right;
delete (*root);
*root = temp;
}
else if ((*root)->right == NULL) {
bstree* temp = (*root)->left;
delete (*root);
*root = temp;
}
else {
bstree* p = (*root)->right;
while (p->left) p = p->left;
(*root)->info = p->info;
int min = p->info;
delete_node(min, &((*root)->right));
}
}
}
void print_bstree(bstree* T) {
if (T == NULL) return;
print_bstree(T->left);
cout << T->info << " ";
print_bstree(T->right);
}
int predecessor(int val, bstree* root) {
if (root->info < val) {
MAX = root->info;
if (root->right == NULL) return MAX;
return predecessor(val, root->right);
}
else {
if (root->left == NULL) return MAX;
return predecessor(val, root->left);
}
}
int successor(int val, bstree* root) {
if (root->info < val) {
if (root->right == NULL) return MIN;
return successor(val, root->right);
}
else {
MIN = root->info;
if (root->left == NULL) return MIN;
return successor(val, root->left);
}
}
bstree* search(int val, bstree* T) {
if (T == NULL) return NULL;
if (val < T->info) return search(val, T->left);
else if (val > T->info) return search(val, T->right);
else return T;
}
int search_max(bstree* T) {
if (T->right == NULL) return T->info;
return search_max(T->right);
}
int search_min(bstree* T) {
if (T->left == NULL) return T->info;
return search_min(T->left);
}
int main () {
bstree* T = NULL;
insert_node(10, &T);
insert_node(7, &T);
insert_node(6, &T);
insert_node(8, &T);
insert_node(9, &T);
insert_node(11, &T);
insert_node(15, &T);
print_bstree(T);
cout << endl;
//delete_node(10, &T);
//print_bstree(T);
//cout << endl;
//cout << "NEW ROOT:" << T->info << endl;
cout << predecessor(0, T) << endl;
// 每次调用前都要重置
MAX = -1;
cout << predecessor(100, T) << endl;
cout << successor(0, T) << endl;
// 每次调用前都要重置
MIN = 100;
cout << successor(20, T) << endl;
int v = 11;
if (!search(v, T))
cout << "The value (" << v << ") doesn't exist" << endl;
else cout << "The address of the value (" << v << "):" << search(v, T) << endl;
cout << "The max value of the BST:" << search_max(T) << endl;
cout << "The min value of the BST:" << search_min(T) << endl;
return 0;
}