-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstree.cpp
157 lines (103 loc) · 2.99 KB
/
bstree.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
147
148
149
150
151
152
153
154
155
156
157
// bstree.cpp
// Implementations for BSTree class
// Author: Juan Arias
//
// The BSTree class is a binary-search tree for objects of the Account class,
// which can:
// -insert an Account
// -retrieve an Account
// -display info of all stored Accounts
// -clear all stored Accounts
// -check if it is empty
#include <iostream>
#include "bstree.h"
// Constructs BSTree
// Initializes root to nullptr
BSTree::BSTree() :root(nullptr) {}
// Destroys BSTree
// Calls Empty to deallocate dynamic memory
BSTree::~BSTree() {
Empty();
}
// Inserts Account object referenced by parameter acctPtr,
// returns true if successful, false otherwise
// Uses helper method insertNode
bool BSTree::Insert(Account* newPtr) {
if (root == nullptr) {
root = new Node(newPtr);
return true;
}
return insertNode(root, newPtr);
}
// Points parameter acctPtr to Account object with ID given as a parameter
// returns true if found, otherwise will point to nullptr then return false
// Uses helper method retrieveNode
bool BSTree::Retrieve(const int& ID, Account *& acct) const {
return retrieveNode(root, ID, acct);
}
// Displays info of all stored Accounts to output
// Uses helper method displayNode
void BSTree::Display() const {
displayNode(root);
}
// Clears all stored Accounts
// Uses helper method deleteNode
void BSTree::Empty() {
deleteNode(root);
root = nullptr;
}
// Returns true if BSTree is empty, false otherwise
bool BSTree::isEmpty() const {
return root == nullptr;
}
// Recursive helper for Insert, uses parameter curr to traverse
bool BSTree::insertNode(Node* curr, Account* newPtr) {
if (newPtr->GetID() < curr->acctPtr->GetID()) {
if (curr->left == nullptr) {
curr->left = new Node(newPtr);
return true;
}
return insertNode(curr->left, newPtr);
} else if (newPtr->GetID() > curr->acctPtr->GetID()) {
if (curr->right == nullptr) {
curr->right = new Node(newPtr);
return true;
}
return insertNode(curr->right, newPtr);
}
return false;
}
// Recursive helper for Retrieve, uses parameter curr to traverse
bool BSTree::retrieveNode(Node* curr, const int& ID, Account*& acct) const {
if (curr != nullptr) {
if (curr->acctPtr->GetID() == ID) {
acct = curr->acctPtr;
return true;
}
Node* nextPtr = (ID < curr->acctPtr->GetID()) ? curr->left:
curr->right;
return retrieveNode(nextPtr, ID, acct);
}
acct = nullptr;
return false;
}
// Recursive helper for Display, uses parameter curr to traverse
void BSTree::displayNode(Node* curr) const {
if (curr != nullptr) {
displayNode(curr->left);
curr->acctPtr->DisplayBalances();
displayNode(curr->right);
}
}
// Recursive helper for Empty, uses parameter curr to traverse
void BSTree::deleteNode(Node* curr) {
if (curr != nullptr) {
deleteNode(curr->left);
deleteNode(curr->right);
delete curr->acctPtr;
delete curr;
}
}
// Construct Node with given pointer to Account
BSTree::Node::Node(Account* newPtr) :acctPtr(newPtr), left(nullptr),
right(nullptr) {}