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
|
#include "PeopleTree.h"
#include <string>
PeopleTree::PeopleTree() {
root = NULL;
}
PeopleTree::~PeopleTree() {
deleteTree(root);
}
void PeopleTree::insert(node*& current, node* newNode) {
//base case 1: current is null
if (current == NULL) {
current = newNode;
}
//general case 1: current->value < newNode->value
else if (current->value < newNode->value) {
/*will go to left node and then keep calling function and going through the tree
until it hits a nullptr*/
insert(current->left, newNode);
}
//general case 2: current->value > newNode->value
else if (current->value > newNode->value) {
/*will go to right node and then keep calling function and going through the tree
until it hits a nullptr*/
insert(current->right, newNode);
}
}
void PeopleTree::insert(std:: string name,std::string birthday) {
//new employee object
People* newPeople = new People(name, birthday);
//create a node to add to tree
node* newNode = new node;
//set left and right inside newNode
newNode->left = nullptr;
newNode->right = nullptr;
//now insert into tree
insert(root, newNode);
}
void PeopleTree::insert(People& item) {
//create new node
node* newNode = new node;
//set left and right pointers to nullptr
newNode->left = NULL;
newNode->right = NULL;
insert(root, newNode);
}
void PeopleTree::deleteTree(node* current) {
//base case
if (current == NULL) {
//do nothing
}
else {
deleteTree(current->left);
deleteTree(current->right);
delete current;
}
}
People* PeopleTree::find(node* current, std::string name) {
//Base case 1:
if (current == nullptr) {
return nullptr;
}
//Base case 2:
else if (current->value.getName() == name) { // class has no memeber
return ¤t->value;
}
//General case:
else {
//go right or left
if (name < current->value.getName()) { // class has no memeber
return find(current->right, name);
}
else {
return find(current->left, name);
}
}
}
People* PeopleTree::findMonth(node* current, std::string birthday) {
//Base case 1:
if (current == nullptr) {
return nullptr;
}
//Base case 2:
else if (current->value.getBirthday() == birthday) { // class has no memeber
return ¤t->value;
}
//General case:
else {
//go right or left
if (birthday < current->value.getBirthday()) { // class has no memeber
return find(current->right, birthday);
}
else {
return find(current->left, birthday);
}
}
}
//wrapper for find
void PeopleTree::find(std::string name) {
People* found = find(root, name);
if (found == nullptr) {
std:: cout << "That name is not in the tree." << std::endl;
}
else {
std::cout << "Name: " << found->getName() << " Birthday: " << found->getBirthday() << std::endl;
}
}
void PeopleTree::findMonth(std::string birthday) {
People* found = find(root, birthday);
if (found == nullptr) {
std::cout << "That month is not in the tree." << std::endl;
}
else {
std::cout << "Name: " << found->getName() << " Birthday: " << found->getBirthday() << std::endl;
}
}
//Prints left nodes, current node, right nodes. Prints in descending order.
void PeopleTree::printInOrder(node* current) {
//Base case: If null, do nothing
//General case
if (current != nullptr) {
//prints the left nodes
printInOrder(current->left);
//prints the current node
std::cout << current->value.getName() << current->value.getBirthday() << std::endl; // class has no memeber
//prints the right nodes
printInOrder(current->right);
}
}
void PeopleTree::printInOrder() {
printInOrder(root);
}
void PeopleTree::remove(node*& current, std::string name) {
//Base Case 1: Does the node exist?
if (current == nullptr) {
//do nothing
}
//Base Case 2: Item was found
else if (current->value.getName() == name) { //Error occurs here
//Create a temp node pointer and set it equal to current
node* tempPtr = current;
//Check every case of nodes that may be attached to current node
//Left and right point to null
if (current->left == nullptr && current->right == nullptr) {
current = nullptr;
}
//Left points to null, right points to somthing
else if (current->left == nullptr && current->right != nullptr) {
current = current->right;
}
//Left points to something, right points to null
else if (current->left != nullptr && current->right == nullptr) {
current = current->left;
}
//Left and right both point to something
else {
node* leftMost = current->right;
while (leftMost->left != nullptr) {
leftMost = leftMost->left;
}
leftMost->left = current->left;
current = current->right;
}
delete tempPtr;
}
//General Case:
else {
if (name < current->value.getName()) {
remove(current->left, name);
}
else {
remove(current->right, name);
}
}
}
void PeopleTree::remove(std::string name) {
remove(root, name);
}
//PeopleTree.h
#include "People.h"
#include <iostream>
#include <cstdlib>
#include <string>
class PeopleTree {
private:
struct node {
node* left;
node* right;
People value;
};
node* root;
void insert(node*& currentPtr, node* newNode);
void remove(node*& currentPtr, std::string name);
void printInOrder(node* currentPtr);
People* find(node* current, std::string name);
People* findMonth(node* current, std::string birthday);
void deleteTree(node* currentPtr);
public:
PeopleTree();
~PeopleTree();
void insert(std::string name, std::string birthday);
void insert(People& item);
void remove(std::string name);
//bool search(std::string name);
void find(std::string name);
void findMonth(std::string birthday);
void printInOrder();
};
|