algo/c-cpp/24_binarysearchtree/binary_search_tree.cpp

268 lines
4.6 KiB
C++
Raw Normal View History

2019-03-01 09:54:16 +08:00
#include <iostream>
using namespace std;
typedef int DataType;
struct tree_node
{
DataType data;
tree_node* left=NULL;
tree_node* right=NULL;
};
class binary_search_tree{
private:
tree_node* root;
int num;
public:
binary_search_tree() :num(0)
{
root = new tree_node;
root->left = NULL;
root->right = NULL;
}
bool find(DataType it,tree_node* root)
{
if (NULL == root)return false;
if (it == root->data) {
return true;
}
else if (it > root->data)
{
return find(it, root->right);
}
else
{
return find(it, root->left);
}
}
bool find_data(DataType it)
{
return find(it, root);
/*
tree_node* p = root;
while (p != NULL)
{
if (it < p->data)p = p->left;
else if (it>p->data)p = p->right;
else return true;
}
return false;
*/
}
void insert_data(DataType it)
{
if (0==num)
{
root->data = it;
num++;
return;
}
tree_node* p = root;
while (p != NULL)
{
if (it < p->data)
{
if (NULL == p->left)
{
p->left = new tree_node;
p->left->data = it;
num++;
return;
}
p = p->left;
}
else
{
if (NULL == p->right)
{
p->right = new tree_node;
p->right->data = it;
num++;
return;
}
p = p->right;
}
}
}
void delet(DataType it)
{
if (NULL == root)return;
tree_node* p = root;
tree_node* pp = NULL;//pp<70><70>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>p<EFBFBD>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>
while (p != NULL&&p->data != it)
{
pp = p;
if (it > p->data)p = p->right;
else
p = p->left;
}
if (p == NULL)return;//û<><C3BB><EFBFBD>ҵ<EFBFBD>
//ɾ<><C9BE><EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
if (p->left != NULL&&p->right != NULL)
{
tree_node* minP = p->right;
tree_node* minPP = p;//<2F><>¼P<C2BC>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>
while (minP->left != NULL)//Ѱ<><D1B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ڵ<EFBFBD>
{
minPP = minP;
minP = minP->left;
}
p->data = minP->data;//<2F><>minP<6E><EFBFBD><E6BBBB>p<EFBFBD><70>
//<2F><>p<EFBFBD><70><EFBFBD><EFBFBD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5>ϣ<EFBFBD>ʹ<EFBFBD><CAB9>Ҷ<EFBFBD>ڵ㷽<DAB5><E3B7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
p = minP;
pp = minPP;
}
//ɾ<><C9BE><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>
tree_node* child;
if (p->left != NULL) child = p->left;
else if (p->right != NULL) child = p->right;
else child = NULL;
if (NULL == pp) root = child;//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8>ڵ<EFBFBD>
else if (p == pp->left)pp->left = child;
else pp->right = child;
}
DataType get_max()
{
if (NULL == root)return NULL;
tree_node* tmp=root;
while (tmp->right != NULL)
{
tmp = tmp->right;
}
return tmp->data;
}
DataType get_min()
{
if (NULL == root)return NULL;
tree_node* tmp=root;
while (tmp->left != NULL)
{
tmp = tmp->left;
}
return tmp->data;
}
DataType get_prenode(DataType it)
{
if (NULL == root)return NULL;
if (it == root->data) return NULL;
tree_node* p=root;
tree_node* pp=NULL;
while ((p->data != it)&&(p!=NULL))
{
pp = p;
if (p->data < it)
{
p=p->right;
}
else
{
p = p->left;
}
}
return ((NULL==p)?NULL:pp->data);
}
DataType get_postnode(DataType it)
{
if (NULL == root)return -1;
tree_node* p = root;
while ((p->data != it) && (p != NULL))
{
if (p->data < it)
{
p = p->left;
}
else
{
p = p->right;
}
}
if (NULL == p)
{
return -1;
}
else if (p->left!=NULL)
{
return p->left->data;
}
else if (p->right!=-NULL)
{
return p->right->data;
}
else
{
return NULL;
}
}
void mid_order(tree_node* rt)
{
if (NULL == rt)return;
mid_order(rt->left);
cout << rt->data;
mid_order(rt->right);
}
void order()
{
if (NULL == root)return;
return mid_order(root);
}
int get_high(tree_node* rt)
{
int lhigh = 0;
int rhigh = 0;
if (NULL == rt)return 0;
lhigh = get_high(rt->left);
rhigh = get_high(rt->right);
return ((lhigh > rhigh) ? (lhigh + 1) : (rhigh + 1));
}
int high()
{
if (NULL == root) return 1;
return get_high(root);
}
};
int main()
{
binary_search_tree my_tree;
my_tree.insert_data(5);
my_tree.insert_data(4);
my_tree.insert_data(6);
my_tree.insert_data(10);
my_tree.insert_data(3);
my_tree.insert_data(8);
my_tree.insert_data(1);
if (my_tree.find_data(3))
{
cout << "<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3" << endl;
}
else
{
cout << "û<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3" << endl;
}
my_tree.delet(4);
cout << "Max" << my_tree.get_max() << endl;
cout << "Min" << my_tree.get_min() << endl;
cout << "pre node of 5 is " <<my_tree.get_prenode(5) <<endl;
cout << "post node of 5 is " << my_tree.get_postnode(5) << endl;
my_tree.order();
cout << "high of tree is " << my_tree.high() << endl;
return 0;
}