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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#include <iostream>
#include <fstream>
using namespace std;
class BST
{
private:
int id;
BST *left;
BST *right;
BST *tree;
int value;
public:
BST();
~BST();
void destroyTree(BST * tree);
void insert(BST * &tree, int id);
void searchToDelete(BST * &tree, int id);
void deleteNode(BST * &tree);
void getPredecessor(BST * &tree, int &id);
void retrieveItem(int &id);
void retrieve(BST * tree, int &id);
void printInOrder(BST * tree);
void printPreOrder(BST * tree);
void debugPrint(BST * tree, unsigned depth = 0);
void printPostOrder(BST * tree);
int lengthIs(BST * tree, int id, int level);
unsigned int countNodes(BST * root);
};
BST::BST()
{
tree = NULL;
id = 0;
}
void
BST::destroyTree(BST * tree)
{
if (tree != NULL) {
destroyTree(tree->left);
destroyTree(tree->right);
delete tree;
}
}
BST::~BST()
{
destroyTree(tree);
}
void
BST::insert(BST * &tree, int id)
{
if (tree == NULL) {
tree = new BST;
tree->right = NULL;
tree->left = NULL;
tree->id = id;
} else if (id < tree->id)
insert(tree->left, id);
else
insert(tree->right, id);
}
void
BST::searchToDelete(BST * &tree, int id)
{
if (id < tree->id)
searchToDelete(tree->left, id);
else if (id > tree->id)
searchToDelete(tree->right, id);
else
deleteNode(tree);
}
void
BST::deleteNode(BST * &tree)
{
int id;
BST *temp;
temp = tree;
if (tree->left == NULL) {
tree = tree->right;
delete temp;
} else if (tree->right == NULL) {
tree = tree->left;
delete temp;
} else {
getPredecessor(tree->left, id);
tree->id = id;
deleteNode(tree->left);
}
}
void
BST::getPredecessor(BST * &tree, int &id)
{
while (tree->right != NULL)
tree = tree->right;
id = tree->id;
}
void
BST::retrieveItem(int &id)
{
retrieve(tree, id);
}
void
BST::retrieve(BST * tree, int &id)
{
if (tree == NULL) {
cout << "Not found\n";
} else if (id < tree->id)
retrieve(tree->left, id);
else if (id > tree->id)
retrieve(tree->right, id);
else
cout << "Found " << id << " in tree.\n";
}
void
BST::debugPrint(BST * tree, unsigned depth)
{
if (tree == NULL)
return;
debugPrint(tree->left, depth + 1);
for (unsigned i = 0; i < depth; ++i) {
cout << " ";
}
cout << tree->id << '\n';
debugPrint(tree->right, depth + 1);
}
void
BST::printInOrder(BST * tree)
{
if (tree != NULL) {
printInOrder(tree->left);
cout << tree->id << " --> ";
printInOrder(tree->right);
}
}
void
BST::printPreOrder(BST * tree)
{
if (tree) {
cout << tree->value << " ";
printPreOrder(tree->left);
printPreOrder(tree->right);
}
}
void
BST::printPostOrder(BST * tree)
{
if (tree) {
printPostOrder(tree->left);
printPostOrder(tree->right);
cout << tree->value << " ";
}
}
int
BST::lengthIs(BST * tree, int id, int level)
{
{
if (tree == NULL)
return 0;
if (tree->id == id)
return level;
int downlevel = lengthIs(tree->left, id, level + 1);
if (downlevel != 0)
return downlevel;
downlevel = lengthIs(tree->right, id, level + 1);
return downlevel;
}
}
unsigned int
BST::countNodes(BST * root)
{
if (root == NULL) {
return 0;
}
int res = (1 // for the root node
+ countNodes(root->left) // left tree
+ countNodes(root->right)); // right tree
return res;
}
int
main()
{
BST bst;
BST *root = NULL;
//insert
cout << "Insert numbers\n";
bst.insert(root, 7);
bst.insert(root, 5);
bst.insert(root, 9);
bst.insert(root, 8);
bst.insert(root, 2);
bst.insert(root, 3);
bst.debugPrint(root);
//print
// bst.printInOrder(root);
cout << "\n\nDelete 8\n";
bst.searchToDelete(root, 8);
bst.debugPrint(root);
// bst.printInOrder(root);
//retrieve
int id = 4;
cout << "\n\nRetrieve 4\n";
bst.retrieve(root, id);
cout << endl << endl;
cout << "Length to " << id << " is " << bst.lengthIs(root, id, 0) << '\n';
cout << "CountNodes() returned " << bst.countNodes(root) << '\n';
cout << "we made it this far";
return 0;
}
| |