二叉搜索树
#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;
}
void buildArr(int* a, node* root) {
if (root != NULL) {
buildArr(a, root->left);
a[num++] = root->value;
buildArr(a, root->right);
}
}
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;
}
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;
}
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;
}
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;
}
}