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 __NODE_H__
#define __NODE_H__
#include <iostream>
template <typename T>
class Node {
public:
Node() {}
Node(T data) {
next_right_ = NULL;
next_left_ = NULL;
data_ = data;
}
~Node() {}
void SetNextRight(Node<T>* next_right) {
next_right_ = next_right;
}
Node* NextRight() {
return next_right_;
}
void SetNextLeft(Node<T>* next_left) {
next_left_ = next_left;
}
Node* NextLeft() {
return next_left_;
}
void SetData(T data) {
data_ = data;
}
T Data() const {
return data_;
}
private:
Node<T>* next_right_;
Node<T>* next_left_;
T data_;
};
#endif // !__NODE_H__
Tree.h
#ifndef __TREE_H__
#define __TREE_H__
#include <iostream>
#include <algorithm>
#include "Node.h"
using namespace std;
template <typename T>
class Tree {
public:
Tree() {}
~Tree() {
PostOrderDeletion(root_);
}
void Add(T data) {
Add(root_, data);
}
void Remove(T data) {
Remove(root_, data);
}
private:
void Add(Node<T>* current, T data) {
if (root_ == NULL) {
root_ = new Node<T>(data);
cout << "Added " << data << " as the root" << endl;
return;
}
else if (data <= current->Data()) {
if (current->NextLeft() != NULL) {
Add(current->NextLeft(), data);
}
else {
current->SetNextLeft(new Node<T>(data));
cout << "Added " << data << " as a child of " << current->Data() << endl;
}
}
else if (data > current->Data()){
if (current->NextRight() != NULL) {
Add(current->NextRight(), data);
}
else {
current->SetNextRight(new Node<T>(data));
cout << "Added " << data << " as a child of " << current->Data() << endl;
}
}
}
void RemoveRoot() {
if (root_ != NULL) {
Node<T>* temp_root = root_;
T temp_root_data = root_->Data();
T temp_lowest_data;
// Case of no children
if (root_->NextLeft() == NULL && root_->NextRight() == NULL) {
root_ = NULL;
delete temp_root;
}
// Case of one child
else if (root_->NextLeft() == NULL && root_->NextRight() != NULL) {
root_ = root_->NextRight();
delete temp_root;
temp_root->SetNextRight(NULL);
cout << "The root node with data " << temp_root_data <<
" was deleted. The new root contains the data " << root_->Data() << endl;
}
else if (root_->NextLeft() != NULL && root_->NextRight() == NULL) {
root_ = root_->NextLeft();
delete temp_root;
temp_root->SetNextLeft(NULL);
cout << "The root node with data " << temp_root_data <<
" was deleted. The new root contains the data " << root_->Data() << endl;
}
// Case of two children
else {
temp_lowest_data = FindLowest(root_->NextRight());
Remove(root_, temp_lowest_data);
root_->SetData(temp_lowest_data);
cout << "The root node containing the data value " << temp_root_data <<
" was overwritten with the data " << root_->Data() << endl;
}
}
else {
cout << "[Root Remove] - The tree is empty." << endl;
}
}
void Remove(Node<T>* current, T data) {
if (root_ != NULL) {
if (data == root_->Data()) {
RemoveRoot();
}
else {
if (data <= current->Data() && current->NextLeft() != NULL) {
(current->NextLeft())->Data() == data ?
Remove(current, current->NextLeft(), true) :
Remove(current->NextLeft(), data);
}
else if (data > current->Data() && current->NextRight() != NULL) {
(current->NextRight())->Data() == data ?
Remove(current, current->NextRight(), false) :
Remove(current->NextRight(), data);
}
else {
cout << "The data value " << data << " was not found" << endl;
}
}
}
else {
cout << "[Remove Recursive] - The tree is empty." << endl;
}
}
void Remove(Node<T>* current, Node<T>* match_test, bool is_left) {
if (root_ != NULL) {
Node<T>* delete_node;
T match_data = match_test->Data();
T lowest_data;
// Case of no children
if (match_test->NextLeft() == NULL && match_test->NextRight() == NULL) {
delete_node = match_test;
is_left == true ? current->SetNextLeft(NULL) : current->SetNextRight(NULL);
delete delete_node;
cout << "The node containing the data value " << match_data << " was removed" << endl;
}
// Case of one child
if (match_test->NextLeft() == NULL && match_test->NextRight() != NULL) {
is_left == true ? current->SetNextLeft(match_test->NextRight()) : current->SetNextRight(match_test->NextRight());
match_test->SetNextRight(NULL);
delete_node = match_test;
delete delete_node;
cout << "The node containing key " << match_data << " was removed" << endl;
}
else if (match_test->NextLeft() != NULL && match_test->NextRight() == NULL) {
is_left == true ? current->SetNextLeft(match_test->NextLeft()) : current->SetNextRight(match_test->NextLeft());
match_test->SetNextLeft(NULL);
delete_node = match_test;
delete delete_node;
cout << "The node containing the data value " << match_data << " was removed" << endl;
}
else {
lowest_data = FindLowest(match_test->NextRight());
Remove(match_test, lowest_data);
match_test->SetData(lowest_data);
}
}
else {
cout << "[Remove Match-Test] - The tree is empty." << endl;
}
}
T FindLowest(Node<T>* current) {
if (root_ != NULL) {
if (current->NextLeft() != NULL) {
return FindLowest(current->NextLeft());
}
else {
cout << "The lowest value is " << current->Data() << endl;
return current->Data();
}
}
else {
cout << "[Find Lowest] - The tree is empty" << endl;
return NULL;
}
}
void PostOrderDeletion(Node<T>* current) {
if (current != NULL) {
if (current->NextLeft() != NULL) {
PostOrderDeletion(current->NextLeft());
}
if (current->NextRight() != NULL) {
PostOrderDeletion(current->NextRight());
}
cout << "Deleting the node containing the data value " << current->Data() << endl;
delete current;
}
}
Node<T>* root_;
};
#endif // !__TREE_H__
|