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
|
#ifndef tree_hpp
#define tree_hpp
#include "node.hpp"
#include <iostream>
//left is less than
//right is greater than
template<typename T>
class tree
{
private:
node<T>* root{nullptr};
T nodeToRemoveKey;
//function to remove the root of the tree and replace it
void RemoveRootMatch()
{
if(root != nullptr)
{
node<T>* delPtr = root;
int rootvalue = root->m_val;
int smallestInRightSubtree;
//case 0 - 0 children
if(root->m_left == nullptr && root->m_right == nullptr)
{
root = nullptr;
delete delPtr;
}
//case 1 - 1 child
else if(root->m_left == nullptr && root->m_right != nullptr)
{
root = root->m_right;
delPtr->m_right = nullptr;
delete delPtr;
std::cout << "root with value " << rootvalue << " was deleted" << std::endl;
std::cout << "the new root contains " << root->m_val << std::endl;
}
else if(root->m_left != nullptr && root->m_right == nullptr)
{
root = root->m_left;
delPtr->m_left = nullptr;
delete delPtr;
std::cout << "root with value " << rootvalue << " was deleted" << std::endl;
std::cout << "the new root contains " << root->m_val << std::endl;
}
//case 2 - 2 children
else
{
smallestInRightSubtree = FindSmallestPrivate(root->m_right);
RemoveNodePrivate(smallestInRightSubtree, root);
root->m_val = smallestInRightSubtree;
std::cout << "the root key conating " << rootvalue <<
" was overwritten with " << root->m_val << std::endl;
}
}
else
{
std::cout << "can not remove root - tree empty" << std::endl;
}
}//end RmoveRootMatch function
void RemoveMatch(node<T>* parent, node<T>* match, bool left)
{
if(root != nullptr)
{
node<T>* delPtr;
int match_value = match->m_val;
int smallestInRightSubtree;
//case 0 - 0 children
if(match->m_left == nullptr && match->m_right == nullptr)
{
delPtr = match;
//is node to delete on the left or right of parent
//then set that pointer to nullptr
if(left == true)
{
parent->m_left = nullptr;
}
else
{
parent->m_right = nullptr;
}
//left == true ? parent->m_left = nullptr : parent->m_right = nullptr;
delete delPtr;
if(match_value == getNodeKey())
{
std::cout << "node containing " << match_value<< " was removed" << std::endl;
}
else
{
std::cout << "node containing " << getNodeKey() << " was overwritten";
std::cout << " with " << match_value << ", and " << match_value <<
" was removed" << std::endl;
}
}
//case 1 - 1 child
else if(match->m_left == nullptr && match->m_right != nullptr)
{
//case for right child bit not a left child
left == true ? parent->m_left = match->m_right : parent->m_right = match->m_right;
match->m_right = nullptr;
delPtr = match;
delete delPtr;
std::cout << "node containing " << match_value << " was removed" << std::endl;
}
else if(match->m_left != nullptr && match->m_right == nullptr)
{
//case for left child but not a right child
left == true ? parent->m_left = match->m_left : parent->m_right = match->m_left;
match->m_left = nullptr;
delPtr = match;
delete delPtr;
std::cout << "node containing " << match_value << " was removed" << std::endl;
}
//case 2 - 2 children
else
{
//std::cout << "here 2 children" << std::endl;
smallestInRightSubtree = FindSmallestPrivate(match->m_right);
RemoveNodePrivate(smallestInRightSubtree, match);
match->m_val = smallestInRightSubtree;
}
}
else
{
std::cout << "empty tree" << std::endl;
}
}
void RemoveNodePrivate(T v, node<T>* parent)
{
if(root)//tree not empty
{
if(root->m_val == v)
{
RemoveRootMatch();
}
else
{
if(v < parent->m_val && parent->m_left != nullptr)
{
if(parent->m_left->m_val == v)
{
RemoveMatch(parent, parent->m_left, true);
}
else
{
RemoveNodePrivate(v, parent->m_left);
}
}
else if(v > parent->m_val && parent->m_right != nullptr)
{
if(parent->m_right->m_val == v)
{
RemoveMatch(parent, parent->m_right, false);
}
else
{
RemoveNodePrivate(v, parent->m_right);
}
}
else
{
std::cout << v << " was not found in tree" << std::endl;
}
}
}
else
{
std::cout << "tree is empty" << std::endl;
}
}//end RemoveNOdePrivate function
int FindSmallestPrivate(node<T>* Ptr)
{
if(root == nullptr)
{
std::cout << "empty tree" << std::endl;
return -1000;
}
else
{
if(Ptr->m_left != nullptr)
{
return FindSmallestPrivate(Ptr->m_left);
}
else
{
return Ptr->m_val;
}
}
}
//function to search for specific value
bool PrivateContains(T v, node<T>* match)
{
if(v == match->m_val)return true;//if there return found
else if(v < match->m_val)//is key lower than current node value
{
if(match->m_left == nullptr) return false;//is next node null then key is not in tree
else return PrivateContains(v, match->m_left);//go further into tree and continue with search
}
else
{
if(match->m_right == nullptr) return false;//is next node null then key is not in tree
return PrivateContains(v, match->m_right);//go further into tree and continue with search
}
}
void PrivateInsert(T v, node<T>* insert)
{
if(v == insert->m_val) return;//case for item already in tree
else if (v < insert->m_val)//is val less than current node
{
if (insert->m_left == nullptr)//is next node null then insert here
{
insert->m_left = new node(v);//insert
}
else PrivateInsert(v, insert->m_left);//continue further into tree
}
else
{
if(insert->m_right == nullptr)//is next node null then insert here
{
insert->m_right = new node(v);//insert
}
else PrivateInsert(v, insert->m_right);//continue further into tree
}
}
|