BinarySortTree.h
#ifndef BINARYSORTTREE_H
#define BINARYSORTTREE_H
#include<iostream>
//binary sort tree node
struct binary_sort_tree_node{
int data;
binary_sort_tree_node* lchild;
binary_sort_tree_node* rchild;
binary_sort_tree_node() :lchild(NULL), rchild(NULL){}
binary_sort_tree_node(int n) :data(n), lchild(NULL), rchild(NULL){}
};
//BinarySortTree
class BinarySortTree{
public:
typedef binary_sort_tree_node node_value;
typedef binary_sort_tree_node* node_pointer;
BinarySortTree();
~BinarySortTree();
node_pointer get_root();
bool insert_bst(node_pointer* p, int key);
bool delete_bst(node_pointer* p, int key);
bool del(node_pointer* p);
bool search(node_pointer p, int key, node_pointer f, node_pointer* q);
void order(node_pointer p);
private:
void destroy(node_pointer p);
private:
node_pointer root;
};
BinarySortTree::BinarySortTree() :root(NULL){}
BinarySortTree::~BinarySortTree(){
destroy(root);
root = NULL;
}
BinarySortTree::node_pointer BinarySortTree::get_root(){
return root;
}
/* 当二叉排序树p中不存在关键字等于key的数据元素时,
插入key并返回true,否则返回false */
bool BinarySortTree::insert_bst(node_pointer* p, int key){
node_pointer q, s;
if (!search(*p, key, NULL, &q)){ //查找不成功
s = new node_value(key);
if (q == NULL)
*p = s; //插入s为新的根结点
else if (key < q->data)
q->lchild = s; //插入s为左孩子
else
q->rchild = s; //插入s为右孩子
return true;
}
else
return false;
}
/* 若二叉排序树p中存在关键字等于key的数据元素时,则删除该数据元素结点,
并返回true;否则返回false */
bool BinarySortTree::delete_bst(node_pointer* p, int key){
if (*p == NULL)//不存在关键字等于key的数据元素
return false;
else{
if (key == (*p)->data)//找到关键字等于key的数据元素
return del(p);
else if (key < (*p)->data)
return delete_bst(&(*p)->lchild, key);
else
return delete_bst(&(*p)->rchild, key);
}
}
/* 从二叉排序树中删除结点p,并重接它的左或右子树。 */
bool BinarySortTree::del(node_pointer* p){
node_pointer q, s;
if ((*p)->rchild == NULL){//右子树空只需重接左子树
q = *p;
*p = (*p)->lchild;
delete q;
}
else if ((*p)->lchild == NULL){//左子树空只需重接右子树
q = *p;
*p = (*p)->rchild;
delete q;
}
else{
q = *p;
s = (*p)->lchild;
while (s->rchild){ //转左,然后向右到尽头
q = s;
s = s->rchild;
}
(*p)->data = s->data;
if (q != *p)//重接q的右子树
q->rchild = s->rchild;
else//重接q的左子树
q->lchild = s->lchild;
delete s;
}
return true;
}
/* 递归查找二叉排序树p中是否存在key,
指针f指向p的双亲,其初始调用值为NULL
若查找成功,则指针q指向该数据元素结点,并返回true
否则指针q指向查找路径上访问的最后一个结点并返回false */
bool BinarySortTree::search(node_pointer p, int key, node_pointer f, node_pointer* q){
if (p == NULL){
*q = f;
return false;
}
else if (key == p->data){
*q = p;
return true;
}
else if (key < p->data)
return search(p->lchild, key, p, q);
else
return search(p->rchild, key, p, q);
}
void BinarySortTree::destroy(node_pointer p){
if (p == NULL)
return;
destroy(p->lchild);
destroy(p->rchild);
delete p;
}
/* 中序遍历就能有序输出 */
void BinarySortTree::order(node_pointer p){
if (p){
order(p->lchild);
std::cout << p->data << " ";
order(p->rchild);
}
}
#endif
main.cpp
#include"BinarySortTree.h"
using namespace std;
void menu(){
cout << "----------------------------------------------------------------" << endl;
cout << "1:建立二叉排序树" << endl;
cout << "2:二叉排序树的插入操作" << endl;
cout << "3:二叉排序树有序输出" << endl;
cout << "4:二叉排序树的删除操作" << endl;
cout << "0:退出菜单" << endl;
cout << "----------------------------------------------------------------" << endl;
}
int main(){
int choose;
int num;
bool flag = true;
int a[10] = { 45, 56, 24, 89, 67, 84, 56, 39, 64, 55 };
BinarySortTree binary_sort_tree;
binary_sort_tree_node* root = binary_sort_tree.get_root();
while (flag){
menu();
cout << "请选择要进行的操作:" << endl;
cin >> choose;
switch (choose){
case 1:
int i;
for (int i = 0; i < 10; i++)
binary_sort_tree.insert_bst(&root, a[i]);
break;
case 2:
cout << "请输入要插入的数:" << endl;
cin >> num;
if (binary_sort_tree.insert_bst(&root, num))
cout << "插入数据成功" << endl;
else
cout << "要插入的数据已经存在" << endl;;
break;
case 3:
binary_sort_tree.order(root);
cout << endl;
break;
case 4:
cout << "请输入删除的数:" << endl;;
cin >> num;
if (binary_sort_tree.delete_bst(&root, num))
cout << "删除成功" << endl;
else
cout << "不存在关键字等于key的数据元素" << endl;
break;
case 0:
flag = false;
break;
}
}
return 0;
}