/* 主题:二叉查找树
* 作者:PhoenixZq
* 邮箱:zhouqiang312@126.com
* 开发语言:C++
* 开发环境:VC++ 6.0
* 时间:2010.12.10
*/
代码
//BinarySearchTree.h
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H
#include <iostream>
typedef int T;
//template <class T>
class BinarySearchTree{
private:
struct BinaryNode{
T elem;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const T & theElem,BinaryNode *lt,BinaryNode *rt)
:elem(theElem),left(lt),right(rt) {}
};
public:
BinaryNode *root;
BinarySearchTree(BinaryNode *r = NULL):root(r){}//默认构造函数
BinarySearchTree(const BinarySearchTree &rhs)//拷贝构造函数
:root(rhs.root){}
const BinarySearchTree & operator=(const BinarySearchTree & rhs){
if(this != &rhs){
makeEmpty(root);
root = clone(rhs.root);
}
return *this;
}
~BinarySearchTree(){
makeEmpty(root);
}
BinaryNode * clone(BinaryNode * t)const{
if(t == NULL)
return NULL;
return new BinaryNode(t->elem,clone(t->left),clone(t->right));
}
void insert(const T & x,BinaryNode * & t);
void remove(const T & x,BinaryNode * & t);
const T findMin(BinaryNode *t)const;
const T findMax(BinaryNode *t)const;
bool contains(const T & x,BinaryNode *t)const;
void makeEmpty(BinaryNode * & t);
void printTree(BinaryNode *t)const;
// void makeEmpty();
/* const T & findMin() const;
const T & findMax() const;
bool contains(const T & x) const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const T & x);
void remove(const T & x);
*/
};
#endif
代码
#include "BinarySearchTree.h"
#include <iostream>
using namespace std;
void BinarySearchTree::makeEmpty(BinaryNode * & t){
if(t != NULL){
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
bool BinarySearchTree::contains(const T & x,BinaryNode *t)const{
if(t == NULL) return false;
else if(x < t->elem)
return contains(x,t->left);
else if(x > t->elem)
return contains(x,t->right);
else return true;
}
//递归findMin
const T BinarySearchTree::findMin(BinaryNode *t)const{
if(t == NULL) return NULL;
if(t->left == NULL) return t->elem;
return findMin(t->left);
}
//非递归findMax
//template <class T>
const T BinarySearchTree::findMax(BinaryNode *t)const{
if(t != NULL)
while(t->right != NULL)
t = t->right;
return t->elem;
}
//template <class T>
void BinarySearchTree::insert(const T & x,BinaryNode * & t){
if(t == NULL)
t = new BinaryNode(x,NULL,NULL);
else if(x < t->elem)
insert(x,t->left);
else if(x > t->elem)
insert(x,t->right);
else
;
}
//template <class T>
void BinarySearchTree::remove(const T & x,BinaryNode * & t){
if(t == NULL)
return ;
if(x < t->elem)
remove(x,t->left);
else if(x > t->elem)
remove(x,t->right);
else if(t->left != NULL && t->right != NULL){
t->elem = findMin(t->right);
remove(t->elem,t->right);
}
else{
BinaryNode *oldNode = t;
t=(t->left != NULL) ? t->left:t->right;
delete oldNode;
}
}
void BinarySearchTree::printTree(BinaryNode *t)const{
if(t != NULL){
printTree(t->left);
cout << t->elem << " ";
printTree(t->right);
}
}
int main(){
BinarySearchTree BST;
T data;
int choice;
while(true){
system("cls");
cout << "\n\n\n ---主界面---\n\n\n";
cout << " 1. 插入操作 2. 删除操作\n";
cout << " 3. 查找操作 4. 寻找最大值\n";
cout << " 5. 寻找最小值 6. 打印树\n";
cout << " 0. 退出\n";
cout << " 请选择操作: ";
cin >> choice;
switch(choice){
case 0:
return 0;
case 1:
system("cls");
cout << "请输入要插入的元素: " ;
cin >> data;
BST.insert(data,BST.root);
break;
case 2:
system("cls");
cout << "请输入要删除的元素: ";
cin >> data;
BST.remove(data,BST.root);break;
case 3:
system("cls");
cout << "请输入要查找的元素: ";
cin >> data;
BST.contains(data,BST.root);break;
case 4:
system("cls");
cout << "寻找最大值: ";
BST.findMax(BST.root);break;
case 5:
system("cls");
cout << "寻找最小值: ";
BST.findMax(BST.root);break;
case 6:
system("cls");
cout << "打印出树: ";
BST.printTree(BST.root);system("pause");break;
}
}
return 0;
}
存在问题:
1.没能把root设置为私有;
2.插入节点操作之前没有创建二叉树.