#include <iostream>
using namespace std;
class BinaryTree {
private:
struct treenode{
int key;
treenode *left, *right;
treenode (int k, treenode *l = 0, treenode *r = 0)
: key(k), left(l), right(r) {}
}; // treenode
treenode * head;
public:
BinaryTree() : head(NULL) {}
// destructor
~BinaryTree() {
clear(head);
}
const treenode * getHead() const {
return head;
}
void clear(treenode * t) {
if (t) {
clear(t->left);
clear(t->right);
delete t;
}
}
// insert a new node with some key into the tree
void insert(int k);
// remove a node with some key
void remove(int k);
// return max key
int max() const;
// return min key
int min() const;
// search wheather there is some key existing
bool search(int k) const;
// print the key in preorder
void preorder(const treenode * t) const;
// print the key in inorder
void inorder(const treenode * t) const;
// print the key in postorder
void postorder(const treenode * t) const;
};
void BinaryTree::insert(int k) {
if (!head) {
head = new treenode(k);
return;
} // if head is null, creat a head with the key
treenode * temp = head;
while (1) { // find a proper position that is empty where we place k
if (k < temp->key) {
if (temp->left) {
temp = temp->left;
} else {
temp->left = new treenode(k, 0, 0);
break;
}
} else {
if (temp->right) {
temp = temp->right;
} else {
temp->right = new treenode(k, 0, 0);
break;
}
}
}
}
void BinaryTree::remove(int k) {
// temp is the node to be delete
treenode *temp = head, *parent = head;
while (temp && temp->key != k) {
parent = temp;
if (k < temp->key)
temp = temp->left;
else
temp = temp->right;
} // find the treenode to be delete, name it temp
if (!temp->left) { // if temp has no left treenode
if (parent == head) {
head = temp->right;
delete temp;
} else if (parent->left == temp) {
parent->left = temp->right;
} else {
parent->right = temp->right;
}
} else if (!temp->right) { // if temp has no right treenode
if (parent == head) {
head = temp->left;
delete temp;
} else if (parent->left == temp) {
parent->left = temp->left;
} else {
parent->right = temp->left;
}
} else {
// find successor and its parent
treenode *successor = temp, *p = temp;
while (successor->left) {
p = successor;
successor = successor->left;
}
// replace temp with successor
temp->key = successor->key;
// let successor's right become successor's parent's child
if (p->left == successor) {
p->left = successor->right;
} else {
p->right = successor->right;
}
delete successor;
}
}
int BinaryTree::max() const { // the rightest element is the minimum
if (head) {
treenode * temp = head;
while (temp->right) {
temp = temp->right;
}
return temp->key;
}
}
int BinaryTree::min() const { // the leftest element is the minimum
if (head) {
treenode * temp = head;
while (temp->left) {
temp = temp->left;
}
return temp->key;
}
}
bool BinaryTree::search(int k) const {
treenode * temp = head;
while (temp) {
if (k == temp->key) return true; // find it and return true
if (k < temp->key)
temp = temp->left;
else
temp = temp->right;
}
return false; // if k not exists, return false
}
void BinaryTree::preorder(const treenode * t) const {
if (t) {
cout << t->key << " ";
preorder(t->left);
preorder(t->right);
}
}
void BinaryTree::inorder(const treenode * t) const {
if (t) {
inorder(t->left);
cout << t->key << " ";
inorder(t->right);
}
}
void BinaryTree::postorder(const treenode * t) const {
if (t) {
postorder(t->left);
postorder(t->right);
cout << t->key << " ";
}
}
//main function for test
int main() {
BinaryTree test;
int arr[10] = {9, -2, 3, 10, 4, 26, -9, 0, 5, 11}; for (int i = 0; i < 10; i++) test.insert(arr[i]); cout << arr[2] << " is in the tree?" << (test.search(arr[2]) ? " Yes." : " No.") << endl; test.preorder(test.getHead()); cout << endl; test.inorder(test.getHead()); cout << endl; test.postorder(test.getHead()); cout << endl; /* 9 9 -2 10 -2 10 -9 3 11 26 -> -9 4 11 26 0 4 0 5 5 */ test.remove(arr[2]); // arr[2] == 3 cout << arr[2] << " is in the tree?" << (test.search(arr[2]) ? " Yes." : " No.") << endl; test.inorder(test.getHead()); cout << endl; test.remove(arr[7]); // arr[7] == 0 test.inorder(test.getHead()); cout << endl; return 0; }
Reasult :
3 is in the tree? Yes.
9 -2 -9 3 0 4 5 10 26 11
-9 -2 0 3 4 5 9 10 11 26
-9 0 5 4 3 -2 11 26 10 9
3 is in the tree? No.
-9 -2 0 4 5 9 10 11 26
-9 -2 4 5 9 10 11 26
Process exited after 0.01723 seconds with return value 0
请按任意键继续…