forked from ucsb-cs24-mirza-s21/lab04_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintbst.cpp
152 lines (129 loc) · 3.48 KB
/
intbst.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
// intbst.cpp
// Implements class IntBST
// YOUR NAME(S), DATE
#include "intbst.h"
#include <iostream>
using std::cout;
// constructor sets up empty tree
IntBST::IntBST() : root(0) { }
// destructor deletes all nodes
IntBST::~IntBST() {
clear(root);
}
// recursive helper for destructor
void IntBST::clear(Node *n) {
if (n) {
clear(n->left);
clear(n->right);
delete n;
}
}
// insert value in tree; return false if duplicate
bool IntBST::insert(int value) {
// handle special case of empty tree first
if (!root) {
root = new Node(value);
return true;
}
// otherwise use recursive helper
return insert(value, root);
}
// recursive helper for insert (assumes n is never 0)
bool IntBST::insert(int value, Node *n) {
if (value == n->info)
return false;
if (value < n->info) {
if (n->left)
return insert(value, n->left);
else {
n->left = new Node(value);
n->left->parent = n;
return true;
}
}
else {
if (n->right)
return insert(value, n->right);
else {
n->right = new Node(value);
n->right->parent = n;
return true;
}
}
}
// print tree data pre-order
void IntBST::printPreOrder() const {
printPreOrder(root);
}
// recursive helper for printPreOrder()
void IntBST::printPreOrder(Node *n) const {
if (n) {
cout << n->info << " ";
printPreOrder(n->left);
printPreOrder(n->right);
}
}
// print tree data in-order, with helper
void IntBST::printInOrder() const {
printInOrder(root);
}
void IntBST::printInOrder(Node *n) const {
// IMPLEMENT HERE
}
// prints tree data post-order, with helper
void IntBST::printPostOrder() const {
printPostOrder(root);
}
void IntBST::printPostOrder(Node *n) const {
// IMPLEMENT HERE
}
// return sum of values in tree
int IntBST::sum() const {
return sum(root);
}
// recursive helper for sum
int IntBST::sum(Node *n) const {
return 0; // REPLACE THIS NON-SOLUTION
}
// return count of values
int IntBST::count() const {
return count(root);
}
// recursive helper for count
int IntBST::count(Node *n) const {
return 0; // REPLACE THIS NON-SOLUTION
}
// IMPLEMENT THIS FIRST: returns the node for a given value or NULL if none exists
// Parameters:
// int value: the value to be found
// Node* n: the node to start with (for a recursive call)
// Whenever you call this method from somewhere else, pass it
// the root node as "n"
IntBST::Node* IntBST::getNodeFor(int value, Node* n) const{
return NULL; // REPLACE THIS NON-SOLUTION
}
// returns true if value is in the tree; false if not
bool IntBST::contains(int value) const {
return true; // REPLACE THIS NON-SOLUTION
}
// returns the Node containing the predecessor of the given value
IntBST::Node* IntBST::getPredecessorNode(int value) const{
return NULL; // REPLACE THIS NON-SOLUTION
}
// returns the predecessor value of the given value or 0 if there is none
int IntBST::getPredecessor(int value) const{
return 0; // REPLACE THIS NON-SOLUTION
}
// returns the Node containing the successor of the given value
IntBST::Node* IntBST::getSuccessorNode(int value) const{
return NULL; // REPLACE THIS NON-SOLUTION
}
// returns the successor value of the given value or 0 if there is none
int IntBST::getSuccessor(int value) const{
return 0; // REPLACE THIS NON-SOLUTION
}
// deletes the Node containing the given value from the tree
// returns true if the node exist and was deleted or false if the node does not exist
bool IntBST::remove(int value){
return false; // REPLACE THIS NON-SOLUTION
}