^_^ 好些天木有写博客了,惭愧啊,不过不代表我没有继续学习哦~
还是要向大神学习,每天坚持学习,每天写博客~加油!!!
#ifndef BINARY_SEARCHTREE_J
#define BINARY_SEARCHTREE_J
#include <iostream>
using std::cout;
#ifndef NULL
#define NULL 0
#endif
template <typename _Tp>
class BinarySearchTree {
public:
struct TreeNode {
_Tp element;
TreeNode *left_child;
TreeNode *right_child;
TreeNode(const _Tp ¶m = 0) : element(param), left_child(0),right_child(0) {}
};
public:
BinarySearchTree() : root(NULL) {}
BinarySearchTree(const BinarySearchTree<_Tp> &rhs);
BinarySearchTree &operator=(const BinarySearchTree<_Tp> &rhs);
~BinarySearchTree() {
makeEmpty();
}
//==========================================
TreeNode *findMax() const {
return findMax(root);
}
TreeNode *findMin() const {
return findMin(root);
}
bool contains(const _Tp &rhs) const {
return contains(rhs, root);
}
bool empty() const {
return root == NULL;
}
void printTree() const {
printTree(root);
}
void makeEmpty() {
makeEmpty(root);
}
void insert(const _Tp &rhs) {
insert(rhs, root);
}
void remove(const _Tp &rhs) {
remove(rhs, root);
}
//==========================================
private:
TreeNode *root;
TreeNode *findMax(TreeNode *itr) const {
if (itr == NULL) return NULL;
if (itr->right_child == NULL) return itr;
return findMax(itr->right_child);
}
TreeNode *findMin(TreeNode *itr) const {
if (itr == NULL) return NULL;
if (itr->left_child == NULL) return itr;
return findMin(itr->left_child);
}
void insert(const _Tp &rhs, TreeNode *&itr) {
if (itr == NULL) {
itr = new TreeNode(rhs);
} else if ( rhs < itr->element ) {
insert(rhs, itr->left_child);
} else if ( rhs > itr->element ) {
insert(rhs, itr->right_child);
}
}
bool contains(const _Tp &rhs, TreeNode *itr) const {
if (itr == NULL) return false;
if (itr->element == rhs) return true;
else if (rhs < itr->element ) return contains(rhs, itr->left_child);
return contains(rhs, itr->right_child);
}
void remove(const _Tp &rhs, TreeNode *&itr) {
if (itr == NULL) return;
if (rhs < itr->element) remove(rhs, itr->left_child);
else if (rhs > itr->element) remove(rhs, itr->right_child);
else if (itr->left_child != NULL && itr->right_child != NULL) {
itr->element = findMin(itr->right_child)->element;
remove(itr->element, itr->right_child);
} else {
TreeNode *temp = itr;
itr = (itr->left_child != NULL) ? itr->left_child : itr->right_child;
delete temp;
}
}
void makeEmpty(TreeNode *&itr) {
if (itr == NULL) return;
makeEmpty(itr->left_child);
makeEmpty(itr->right_child);
delete itr;
itr = NULL;
}
void printTree(TreeNode *itr) const {
if (itr == NULL) return;
cout <<" " << itr->element << " ";
printTree(itr->left_child);
printTree(itr->right_child);
}
void clone(TreeNode *&lhs,const TreeNode *rhs) {
if (rhs == NULL) return;
lhs = new TreeNode(rhs->element);
clone(lhs->left_child, rhs->left_child);
clone(lhs->right_child, rhs->right_child);
}
};
template <typename _Tp>
BinarySearchTree<_Tp>::BinarySearchTree(const BinarySearchTree<_Tp> &rhs) {
clone(root, rhs.root);
}
template <typename _Tp>
BinarySearchTree<_Tp> &BinarySearchTree<_Tp>::operator=(const BinarySearchTree<_Tp> &rhs) {
if (this != &rhs) {
makeEmpty();
clone(root, rhs.root);
}
return *this;
}
#endif