1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
Node(int k, string v){
key = k;
value = v;
leftchild = rightchild = NULL;
}
int key;
string value;
Node *leftchild;
Node *rightchild;
};
class BST
{
public:
BST(){
root = NULL;
}
void insert(int key, string value);
string search(int key);
int getHeight();
void delete_node(int key);
private:
Node *root;
int delete_internal(Node*& root, int key);
Node* popMinNode(Node*& root);
void insert_internal(Node* &root, Node* newptr);
string search_internal(Node* root, int key);
int getHeight_internal(Node* root);
};
void BST::delete_node(int key)
{
int del = delete_internal(root, key);
if(del == 0)
{
cout << "Key not found" << endl;
}
else
{
cout << "deletion successful" << endl;
}
}
int BST::getHeight()
{
return getHeight_internal(root);
}
int BST::getHeight_internal(Node *node)
{
if(node == NULL) return -1;
else
{
int h_left = getHeight_internal(node->leftchild);
int h_right = getHeight_internal(node->rightchild);
if(h_left >= h_right){
return h_left + 1;
}
else
{
return h_right + 1;
}
}
}
string BST::search(int key)
{
return search_internal(root, key);
}
string BST::search_internal(Node *root, int key)
{
if(root == NULL)
{
return "Not found";
}
else if(root->key == key)
{
return root->value;
}
else if(root->key > key)
{
return search_internal(root->leftchild, key);
}
else if(root->key < key)
{
return search_internal(root->rightchild, key);
}
}
int BST::delete_internal(Node*& root, int key)
{
if(root == NULL)
{
return 0;
}
else if(root->key > key)
{
return delete_internal(root->leftchild, key);
}
else if(root->key < key)
{
return delete_internal(root->rightchild, key);
}
else if(root->key == key)
{
if(root->leftchild == NULL and root->rightchild == NULL)
{
delete root;
root = NULL;
}
else if(root->leftchild == NULL and root->rightchild != NULL)
{
Node * temp = root->rightchild;
delete root;
root = temp;
}
else if(root->rightchild == NULL and root->leftchild != NULL)
{
Node * temp = root->leftchild;
delete root;
root = temp;
}
else
{
Node* min = popMinNode(root->rightchild);
cout << "Min key : " << min->key << endl;
root->key = min->key;
root->value = min->value;
}
return 1;
}
}
Node* BST::popMinNode(Node*& root)
{
if(root->leftchild == NULL)
{
Node *temp = root;
root = root->rightchild;
return temp;
}
else
{
return popMinNode(root->leftchild);
}
}
void BST::insert(int key, string value)
{
Node *newptr = new Node(key, value);
insert_internal(root, newptr);
}
void BST::insert_internal(Node* &root, Node* newptr)
{
if(root == NULL)
{
root = newptr;
}
else if(root->key > newptr->key)
{
insert_internal(root->leftchild, newptr);
}
else if(root->key < newptr->key)
{
insert_internal(root->rightchild, newptr);
}
}
int main()
{
cout << "Hello World" << endl;
BST bst;
bst.insert(1001,"Hello");
cout << "Height : " << bst.getHeight() << endl;
bst.insert(1002,"World");
cout << "Height : " << bst.getHeight() << endl;
bst.insert(1003,"What");
cout << "Height : " << bst.getHeight() << endl;
bst.insert(1004,"Where");
cout << "Height : " << bst.getHeight() << endl;
bst.insert(1000,"Which");
cout << "Height : " << bst.getHeight() << endl;
cout << bst.search(1001) << endl;
bst.delete_node(1001);
cout << bst.search(1001) << endl;
cout << "New Height : " << bst.getHeight() << endl;
return 0;
}
|