template <typename Key,typename Value>
class BST {
private:
struct Node {
Key key;
Value value;
Node *left;
Node *right;
int count;
Node(Key key,Value value,int count) {
this->key = key;
this->value = value;
this->left = this->right = NULL;
this->count = count = 0;
}
Node(Node *node) {
this->key = key;
this->value = value;
this->left = node->left;
this->right = node->right;
}
};
Node *root;
int *count;
public:
BST() {
root = NULL;
count = 0;
}
~BST() {
destroy(root);
}
int size() {
return *count;
}
bool isEmpty() {
return count == 0;
}
void insert(Key key,Value value) {
root = insert(root,key,value);
}
bool contain(Key key) {
return contain(root,key);
}
Value* search(Node* node,Key key) {
if ( node == NULL) {
return NULL;
}
if (key == node->key) {
return &(node->value);
} else if (key < node->key) {
return search(node->left,key);
} else {
return search(node->right,key);
}
}
void preOrder() {
preOrder(root);
}
void inOrder() {
inOrder(root);
}
void postOrder() {
postOrder(root);
}
void levelOrder() {
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* node = q.front();
q.pop();
cout << node->key << endl;
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
}
Key mininum() {
assert( count!= 0);
Node* minNode = mininum(root);
return minNode->key;
}
Key maxnum() {
assert( count != 0);
Node* maxNode = maxnum(root);
return maxNode->key;
}
void removeMin() {
if (root) {
root = removeMin(root);
}
}
void removeMax() {
if (root) {
root = removeMax(root);
}
}
void remove(Key key) {
root = remove(root,key);
}
Key* floor(Key key) {
if ( count == 0 || key < mininum()) {
return NULL;
}
Node *floorNode = floor(root,key);
return &(floorNode->key);
}
Key* ceiling(Key key) {
if ( count == 0 || key > maxnum())
return NULL;
Node *ceilNode = ceil(root,key);
return &(ceilNode->key);
}
Key* select(int k) {
return select(root,k)->key;
}
Key* rank(Key key) {
return rank(key,root);
}
private:
Node* insert(Node *node,Key key,Value value) {
if (node == NULL) {
count++;
return new Node(key,value,count);
}
if (key == node->key) {
node->value = value;
} else if(key < node->key) {
node->left = insert(node->left,key,value);
} else if (key > node->key) {
node->right = insert(node->right,key,value);
}
return node;
}
bool contain(Node* node,Key key) {
if ( node == NULL) {
return false;
}
if (key == node->key) {
return true;
} else if(key < node->key){
return contain(node->left,key);
} else {
return contain(node->right,key);
}
}
void preOrder(Node* node) {
if (node != NULL) {
cout << node->key << endl;
preOrder(node->left);
preOrder(node->right);
}
}
void inOrder(Node* node) {
if (node != NULL) {
inOrder(node->left);
cout << node->key << endl;
inOrder(node->right);
}
}
void postOrder(Node* node) {
if (node != NULL) {
postOrder(node->left);
postOrder(node->right);
cout << node->key <<endl;
}
}
void destroy(Node* node) {
if (node != NULL) {
destroy(node->left);
destroy(node->right);
delete node;
count--;
}
}
Node* mininum(Node* node) {
if (node->left == NULL) {
return node;
}
return mininum(node->left);
}
Node* maxnum(Node* node) {
if (node->right == NULL) {
return node;
}
return maxnum(node->right);
}
Node* removeMin(Node* node) {
if ( node->left == NULL) {
Node* rightNode = node->right;
delete node;
count--;
return rightNode;
}
node->left = removeMin(node->left);
return node;
}
Node* removeMax(Node* node) {
if (node->right == NULL) {
Node* leftNode = node->left;
delete node;
count--;
return leftNode;
}
node->right = removeMax(node->right);
return node;
}
Node* remove(Node* node,Key key) {
if (node == NULL) {
return NULL;
}
if (key < node->key) {
node->left = remove(node->left,key);
return node;
} else if (key > node->key) {
node->right = remove(node->right,key);
return node;
} else {
if (node->left == NULL) {
Node *rightNode = node->right;
delete node;
count--;
return rightNode;
}
if (node->right == NULL) {
Node *leftNode = node->left;
delete node;
count--;
return leftNode;
}
Node *successor = new Node(mininum(node->right));
count++;
successor->right = removeMin(node->right);
successor->left = node->left;
delete node;
count--;
return successor;
}
}
Node* remove2(Node* node,Key key) {
if (node == NULL) {
return NULL;
}
if (key < node->key) {
node->left = remove(node->left,key);
} else if (key > node->key) {
node->right = remove(node->right,key);
} else {
if (node->left == NULL) {
Node *rightNode = node->right;
delete node;
count--;
return rightNode;
}
if (node->right == NULL) {
Node *leftNode = node->left;
delete node;
count--;
return leftNode;
}
Node* successor = new Node(maxnum(node->left));
count++;
successor->left = remove2(node->left,key);
successor->right = node->right;
delete node;
count--;
return successor;
}
}
Node* floor(Node* node,Key key) {
if (node ==NULL) {
return NULL;
}
if (node->key == key) {
return node;
}
if (node->key > key) {
return floor(node->left,key);
} else {
Node* t = floor(node->right,key);
if (t != NULL)
return t;
return node;
}
}
Node* ceil(Node* node,Key key) {
if (node == NULL) {
return NULL;
}
if (node->key == key) {
return node;
}
if (node->key < key) {
return ceil(node->right,key);
} else {
Node* t = floor(node->left,key);
if (t != NULL)
return t;
return node;
}
}
Node* select(Node* node,int k) {
if ( node == NULL)
return NULL;
int n = node->count;
if (n > k)
return select(node->left,k);
else if (n < k)
return select(node->right,k-n-1);
else
return node;
}
int rank(Key key,Node* node) {
if ( node == NULL)
return NULL;
if (key < node->key) {
return rank(key,node->left);
} else if (key > node->key) {
return 1 + node->count + rank(key,node->right);
} else {
return node->count;
}
}
};