forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.cpp
131 lines (113 loc) · 3.33 KB
/
BinarySearchTree.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
#include "BinarySearchTree.h"
#include <string>
#include <iostream>
#include "BinaryNode.h"
using namespace std;
BinarySearchTree::BinarySearchTree() {
root = NULL;
}
BinarySearchTree::~BinarySearchTree() {
delete root;
root = NULL;
}
// insert finds a position for x in the tree and places it there.
void BinarySearchTree::insert(const string& x) {
// YOUR IMPLEMENTATION GOES HERE
}
// remove finds x's position in the tree and removes it.
void BinarySearchTree::remove(const string& x) {
root = remove(root, x);
}
// private helper for remove to allow recursion over different nodes. returns
// a BinaryNode* that is assigned to the original node.
BinaryNode* BinarySearchTree::remove(BinaryNode*& n, const string& x) {
if (n == NULL) {
return NULL;
}
// first look for x
if (x == n->value) {
// found
// no children
if (n->left == NULL && n->right == NULL) {
delete n;
n = NULL;
return NULL;
}
// single child
if (n->left == NULL) {
BinaryNode* temp = n->right;
n->right = NULL;
delete n;
n = NULL;
return temp;
}
if (n->right == NULL) {
BinaryNode* temp = n->left;
n->left = NULL;
delete n;
n = NULL;
return temp;
}
// two children
string sr = min(n->right);
n->value = sr;
n->right = remove(n->right, sr);
} else if (x < n->value) {
n->left = remove(n->left, x);
} else {
n->right = remove(n->right, x);
}
return n;
}
// pathTo finds x in the tree and returns a string representing the path it
// took to get there.
string BinarySearchTree::pathTo(const string& x) const {
// YOUR IMPLEMENTATION GOES HERE
}
// find determines whether or not x exists in the tree.
bool BinarySearchTree::find(const string& x) const {
// YOUR IMPLEMENTATION GOES HERE
}
// numNodes returns the total number of nodes in the tree.
int BinarySearchTree::numNodes() const {
// YOUR IMPLEMENTATION GOES HERE
}
// min finds the string with the smallest value in a subtree.
string BinarySearchTree::min(BinaryNode* node) const {
// go to bottom-left node
if (node->left == NULL) {
return node->value;
}
return min(node->left);
}
// Helper function to print branches of the binary tree
void showTrunks(Trunk* p) {
if (p == nullptr) return;
showTrunks(p->prev);
cout << p->str;
}
void BinarySearchTree::printTree() {
printTree(root, NULL, false);
}
// Recursive function to print binary tree
// It uses inorder traversal
void BinarySearchTree::printTree(BinaryNode* root, Trunk* prev, bool isRight) {
if (root == NULL) return;
string prev_str = " ";
Trunk* trunk = new Trunk(prev, prev_str);
printTree(root->right, trunk, true);
if (!prev)
trunk->str = "---";
else if (isRight) { // github user @willzhang05 pointed out that I forgot to change this from isLeft to isRight on my first commit
trunk->str = ".---";
prev_str = " |";
} else {
trunk->str = "`---";
prev->str = prev_str;
}
showTrunks(trunk);
cout << root->value << endl;
if (prev) prev->str = prev_str;
trunk->str = " |";
printTree(root->left, trunk, false);
}