二叉排序树又称为二叉查找树,二叉搜索树。二叉排序树和普通的二叉树在结构上一样,它要么是一棵空树,要么是这样的一棵二叉树:对任意结点,如果左子树不为空,则左子树上所有结点的权值都小于该结点的权值;如果右子树不为空,则右子树上所有结点的权值都大于该结点的权值;任意结点的左子树和右子树都是一棵二叉排序树;一般而言, 二叉排序树上结点的权值都是唯一的。
在二叉排序树上,对于任意结点,如果有左子树和右子树,那么其左子树上结点的权值都小于右子树上结点的权值。
如果中序遍历二叉排序树,会得到一个从小到大的序列。
二叉排序树的插入和查找效率相对较高,最坏情况下时间复杂度为 O(n),期望的时间复杂度为 O(logn),其中 n 为树上结点总个数。
刚才我们提到了二叉排序树最坏情况下时间复杂度为 O(n),这是为什么呢?最坏情况下,二叉排序树会退化成链表,从根结点往左依次递减,或者从根结点往右依次递增。那有什么方法可以解决这个问题呢?在二叉排序树的基础上可以加些优化,可以让其成为 AVL 树,红黑树,SBT,Splay 等等,这些高级的树结构解决了上面的问题,插入和查找的效率均为 O(logn)。
不完全版实现:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node *lchild, *rchild, *father;
Node(int _data, Node *_father = NULL) {
data = _data;
lchild = NULL;
rchild = NULL;
father = _father;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void insert(int value) {
if (value == data) {
return;
} else if (value > data) {
if (rchild == NULL) {
rchild = new Node(value, this);
} else {
rchild->insert(value);
}
} else {
if (lchild == NULL) {
lchild = new Node(value, this);
} else {
lchild->insert(value);
}
}
}
Node* search(int value) {
if (data == value) {
return this;
} else if (value > data) {
if (rchild == NULL) {
return NULL;
} else {
return rchild->search(value);
}
} else {
if (lchild == NULL) {
return NULL;
} else {
return lchild->search(value);
}
}
}
Node* predecessor() {
Node *temp = lchild;
while (temp != NULL && temp->rchild != NULL) {
temp = temp->rchild;
}
return temp;
}
Node* successor() {
Node *temp = rchild;
while (temp != NULL && temp->lchild != NULL) {
temp = temp->lchild;
}
return temp;
}
void remove_node(Node* delete_node){
Node* temp=NULL;
//temp是删除节点(0度/1度)的孩子,更新孩子节点的父亲指针
if(delete_node->lchild!=NULL){
temp=delete_node->lchild;
temp->father=delete_node->father;
delete_node->lchild=NULL;
}
if(delete_node->rchild!=NULL){
temp=delete_node->rchild;
temp->father=delete_node->father;
delete_node->rchild=NULL;
}
//判断删除节点是左孩子还是右孩子
//更新删除节点的孩子指针
if(delete_node->father->lchild==delete_node){
delete_node->father->lchild=temp;
}
else{
delete_node->father->rchild=temp;
}
delete delete_node;
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
if (root != NULL) {
delete root;
}
}
void insert(int value) {
if (root == NULL) {
root = new Node(value);
} else {
root->insert(value);
}
}
bool find(int value) {
if (root->search(value) == NULL) {
return false;
} else {
return true;
}
}
};
int main() {
BinaryTree binarytree;
int arr[10] = { 8, 9, 10, 3, 2, 1, 6, 4, 7, 5 };
for (int i = 0; i < 10; i++) {
binarytree.insert(arr[i]);
}
int value;
cin >> value;
if (binarytree.find(value)) {
cout << "search success!" << endl;
} else {
cout << "search failed!" << endl;
}
return 0;
}
二叉树的基本操作完整版
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node *lchild, *rchild, *father;
Node(int _data, Node *_father = NULL) {
data = _data;
lchild = NULL;
rchild = NULL;
father = _father;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void insert(int value) {
if (value == data) {
return;
} else if (value > data) {
if (rchild == NULL) {
rchild = new Node(value, this);
} else {
rchild->insert(value);
}
} else {
if (lchild == NULL) {
lchild = new Node(value, this);
} else {
lchild->insert(value);
}
}
}
Node* search(int value) {
if (data == value) {
return this;
} else if (value > data) {
if (rchild == NULL) {
return NULL;
} else {
return rchild->search(value);
}
} else {
if (lchild == NULL) {
return NULL;
} else {
return lchild->search(value);
}
}
}
Node* predecessor() {
Node *temp = lchild;
while (temp != NULL && temp->rchild != NULL) {
temp = temp->rchild;
}
return temp;
}
Node* successor() {
Node *temp = rchild;
while (temp != NULL && temp->lchild != NULL) {
temp = temp->lchild;
}
return temp;
}
void remove_node(Node *delete_node) {
Node *temp = NULL;
if (delete_node->lchild != NULL) {
temp = delete_node->lchild;
temp->father = delete_node->father;
delete_node->lchild = NULL;
}
if (delete_node->rchild != NULL) {
temp = delete_node->rchild;
temp->father = delete_node->father;
delete_node->rchild = NULL;
}
if (delete_node->father->lchild == delete_node) {
delete_node->father->lchild = temp;
} else {
delete_node->father->rchild = temp;
}
delete delete_node;
}
//完整版删除操作,考虑2度节点
bool delete_tree(int value){
Node *delete_node,*current_node;
current_node=search(value);
if(current_node==NULL){
return false;
}
if(current_node->lchild!=NULL){
//该节点有左孩子,则指向该节点前驱,后期前驱权值代替当前节点权值
delete_node=current_node->predecessor();
}
else if(current_node->rchild!=NULL){
//该节点有右孩子,则指向该节点后继,后期后继权值代替当前节点权值
delete_node=current_node->successor();
}
else{
delete_node=current_node;
}
current_node->data=delete_node->data;
remove_node(delete_node);
return true;
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
if (root != NULL) {
delete root;
}
}
void insert(int value) {
if (root == NULL) {
root = new Node(value);
} else {
root->insert(value);
}
}
bool find(int value) {
if (root->search(value) == NULL) {
return false;
} else {
return true;
}
}
bool delete_tree(int value){
return root->delete_tree(value);
}
};
int main() {
BinaryTree binarytree;
int arr[10] = { 8, 9, 10, 3, 2, 1, 6, 4, 7, 5 };
for (int i = 0; i < 10; i++) {
binarytree.insert(arr[i]);
}
int value;
cin >> value;
if (binarytree.find(value)) {
cout << "search success!" << endl;
} else {
cout << "search failed!" << endl;
}
cin>>value;
if (binarytree.delete_tree(value)) {
cout << "delete success!" << endl;
} else {
cout << "delete failed!" << endl;
}
return 0;
}