-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookContainer.cpp
146 lines (132 loc) · 4.05 KB
/
bookContainer.cpp
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
#include "bookContainer.h"
#include <iostream>
#include "book.h"
//---------------------------------------------------------------------------
//Default constructor
//creates empty tree
BookContainer::BookContainer() {
root = nullptr;
}
//---------------------------------------------------------------------------
//Destructor
//calls makeEmpty to delete tree
BookContainer::~BookContainer() {
makeEmpty();
}
//makeEmpty
//deletes all memory and clears tree by calling makeEmptyHelper
void BookContainer::makeEmpty() {
if (root != nullptr) {
makeEmptyHelper(root);
root = nullptr;
}
}
//makeEmptyHelper
//called by makeEmpty to recursively delete tree
void BookContainer::makeEmptyHelper(Node* curr) {
if (curr != nullptr) {
makeEmptyHelper(curr->left);
makeEmptyHelper(curr->right);
delete curr->data;
curr->data = nullptr;
delete curr;
curr = nullptr;
}
}
//---------------------------------------------------------------------------
//getRoot
//returns the root of the tree
Book* BookContainer::getRoot() const {
return root->data;
}
//---------------------------------------------------------------------------
//isEmpty
//returns true if tree is empty
bool BookContainer::isEmpty() const {
if (root == nullptr) {
return true;
}
return false;
}
//---------------------------------------------------------------------------
//print
//displays the tree with an inorder traversal using the print helper func
//relies on the book having a display function
void BookContainer::print() {
if (root == nullptr) return;
printHelper(root);
}
//inorder helper function moves through tree to display variables
//in order and return them
void BookContainer::printHelper(Node* curr) {
if (curr != nullptr) {
printHelper(curr->left);
curr->data->print();
printHelper(curr->right);
}
}
//---------------------------------------------------------------------------
//retrieve
//used to find the data and if it is not found then returns false
bool BookContainer::retrieve(const Book& target, Book*& point) const {
if (root == nullptr) {
return false;
}
Node* curr = root;
while (curr != nullptr) {
if (*curr->data == target) {
point = curr->data;
return true;
}
else if (target < *curr->data) {
curr = curr->left;
}
else {
curr = curr->right;
}
}
return false;
}
//---------------------------------------------------------------------------
//insert
//Inserts in order within tree assuming that operator< exists in Book
bool BookContainer::insert(Book* dataptr) {
Node* ptr = new Node;
ptr->data = dataptr;
dataptr = nullptr;
ptr->left = ptr->right = nullptr;
if (isEmpty()) {
root = ptr;
}
else {
Node* current = root;
bool inserted = false;
// if item is less than current item, insert in left subtree,
// otherwise insert in right subtree
while (!inserted) {
if (*current->data == *ptr->data) {
delete ptr;
ptr = nullptr;
return false;
}
if (*ptr->data < *current->data) {
if (current->left == nullptr) { // at leaf, insert left
current->left = ptr;
inserted = true;
}
else
current = current->left; // one step left
}
else {
if (current->right == nullptr) { // at leaf, insert right
current->right = ptr;
inserted = true;
}
else
current = current->right; // one step right
}
}
}
return true;
}
//---------------------------------------------------------------------------