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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
//TreeNode header file
// TreeNode.h
#ifndef __TREENODE__H__
#define __TREENODE__H__
#include <iostream>
#include <cstdlib> // for NULL
class TreeNode{
friend class BST;
friend std::ostream& operator<<(std::ostream &os,const TreeNode &tnode );
public:
TreeNode(int item, TreeNode* left = NULL, TreeNode* right = NULL);
bool rightChild(int &c);
// returns False if no right child, otherwise c gets the value of the right child
bool leftChild(int &c);
// returns False if no left child, otherwise c gets the value of the left child
private:
TreeNode(const TreeNode &tnode); // no Copy Constructor
void operator=(const TreeNode &tnode); // no assignment operator
int _item;
TreeNode* _left;
TreeNode* _right;
};
std::ostream& operator<<(std::ostream &os,const TreeNode &tnode );
//TreeNode omplemenation file
#include "TreeNode.h"
#include <cstdlib>
TreeNode::TreeNode(int item, TreeNode* left, TreeNode* right) {
_item = item;
_left = left;
_right = right;
}
bool TreeNode::leftChild(int &c) {
if (_left != NULL) {
c = _left -> _item;
return true;}
else {
return false; }
}
bool TreeNode::rightChild(int &c) {
if (_right != NULL) {
c = _right -> _item;
return true;}
else {
return false; }
}
std::ostream& operator<<(std::ostream &os,const TreeNode &tnode ){
os << " " << tnode._item << " ";
return os;
}
// BST header file
// BST.h
#ifndef __BST__H__
#define __BST__H__
#include <iostream>
#include <cstdlib> // for NULL
#include <vector>
#include "TreeNode.h"
class BST{
friend std::ostream& operator<<(std::ostream &os,const BST &tree );
public:
BST() { _root = NULL; numberOfNodes = 0; } // creates empty tree
~BST(); // destructor, uses dealloc
bool insert(const int &item); // returns False if not possible to insert (eg. duplicates are not allowed)
int getNumberOfNodes() const { return numberOfNodes;};
bool find(const int &target) const;
void Delete(int target);
private:
void _subtreeAddItems (const TreeNode *node, std::vector<int> &tmp) const; // helper for ostream operation
void dealloc(TreeNode *node); // recursuve function deallocating the TreeNodes
TreeNode *_root;
size_t _height; // height of the tree
size_t numberOfNodes; // keeps track of number of nodes in the tree
BST _subtreeDelete(TreeNode *node, int &target);
int _subtreeDeleteMax(const *node);
};
std::ostream& operator<<(std::ostream &os,const BST &tree );
// uses _subtreeAddItems
#endif
// BST implementation file
#endif
#include <cstdlib>
#include <iostream>
//#include <string>
#include <vector>
#include "BST.h"
using namespace std;
BST::~BST(){ // destructor
if (_root != NULL) {
dealloc(_root);
}
}
void BST::dealloc(TreeNode *node) // helper method
{ // deallocates all nodes in the binary search tree recursively
TreeNode *lnode, *rnode;
lnode = node->_left;
rnode = node->_right;
if (lnode != NULL) {dealloc(lnode);}
if (rnode != NULL) {dealloc(rnode);}
delete node;
}
bool BST::insert(const int &item)
{
if (_root == NULL) { // handle empty tree case
_root = new TreeNode(item);
}
else{
TreeNode *node;
node = _root; // start at root
while (true)
{
if (item == node->_item){ // found a duplicate
return false;
}
if (item < node->_item){ // item goes in the left subtree because target is less than the element in current node
if (node->_left != NULL){ // left subtree is not empty
node = node -> _left; // proceed to the left until it is None
}
else { // now is empty (we have reahed the leftmost node) subtree, insert here
node -> _left = new TreeNode(item);
break;
}
}
// our target is bigger than the element at current node, therefore, go to the right
else { // item goes in the right subtree
if (node->_right != NULL){ // left subtree is not empty
node = node -> _right;
}
else { // empty subtree, insert here
node -> _right = new TreeNode(item);
break;
}
}
}
}
++numberOfNodes;
}
bool BST::find(const int &target) const{
TreeNode *node;
node = _root;
while ((node!= NULL) && (node->_item != target)){
if (target < node->_item){
node = node->_left;
}
else {
node = node -> _right;}
}
if (node == NULL) {return false;}
else {return true;}
}
void BST::_subtreeAddItems (const TreeNode *node, vector<int> &tmp) const{
if (node != NULL) {
_subtreeAddItems(node->_left,tmp);
tmp.push_back(node->_item);
_subtreeAddItems(node->_right,tmp);
}
}
void BST::Delete(int target)
{
//TreeNode *node;
//_root = node;
int item;
item = target;
*_root;
*_root = _subtreeDelete(_root, item);
}
BST BST::_subtreeDelete(const TreeNode *node, int &target)
{
//*_root;
_root = node;
if (_root)// same as node != NULL. If it is empty.
{
return NULL;
}
if (item < _root->_item)
{
_root->_left = _subtreeDelete(_root->_left, item);
}
else if (item > _root->_item)
{
_root->_right = _subtreeDelete(_root->_right, item);
}
else
{
if (_root->_left == NULL)
{
_root = _root->_right;
}
else if (_root->_right)
{
_root = _root->_left;
}
else
{
_root->_item = _subtreeDelMax(_root);
}
return _root;
}
}
int BST::_subtreeDelMax(TreeNode *node);
{
//TreeNode *node;
_root = node;
int maxVal;
if (_root->_right == NULL)
{
this->_left = _root->_left;
return this->_item;
}
else
{
_root->_right = _root->_right;
maxVal = _subtreeDelMax(_root->_right);
return maxVal;
}
}
std::ostream& operator<<(std::ostream &os,const BST &tree ){
vector<int> tmp;
tree._subtreeAddItems(tree._root,tmp); // recursive helper method
for(vector<int>::iterator list_iter = tmp.begin(); list_iter != tmp.end(); list_iter++) {
os << *list_iter << " " ; }
os << endl;
return os;
}
// Test code contatining the main
#include "TreeNode.h"
#include <cstdlib>
TreeNode::TreeNode(int item, TreeNode* left, TreeNode* right) {
_item = item;
_left = left;
_right = right;
}
bool TreeNode::leftChild(int &c) {
if (_left != NULL) {
c = _left -> _item;
return true;}
else {
return false; }
}
bool TreeNode::rightChild(int &c) {
if (_right != NULL) {
c = _right -> _item;
return true;}
else {
return false; }
}
std::ostream& operator<<(std::ostream &os,const TreeNode &tnode ){
os << " " << tnode._item << " ";
return os;
}
|