Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions labs/lab05/code/inlab/BinarySearchTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ BinaryNode* BinarySearchTree::remove(BinaryNode*& n, const string& x) {
return NULL;
}
// first look for x
if (x == n->value) {
int cmp = n->value.compare(x);
if (cmp == 0) {
// found
// no children
if (n->left == NULL && n->right == NULL) {
Expand All @@ -68,7 +69,7 @@ BinaryNode* BinarySearchTree::remove(BinaryNode*& n, const string& x) {
string sr = min(n->right);
n->value = sr;
n->right = remove(n->right, sr);
} else if (x < n->value) {
} else if (cmp < 0) {
n->left = remove(n->left, x);
} else {
n->right = remove(n->right, x);
Expand Down Expand Up @@ -117,8 +118,9 @@ void BinarySearchTree::printTree(BinaryNode* root, Trunk* prev, bool isRight) {

if (prev) prev->str = prev_str;
trunk->str = " |";

printTree(root->left, trunk, false);
delete trunk;
}

void BinarySearchTree::printTree() { printTree(root, NULL, false); }
void BinarySearchTree::printTree() { printTree(root, NULL, false); }
10 changes: 6 additions & 4 deletions labs/lab05/code/postlab/AVLTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ AVLNode* AVLTree::remove(AVLNode*& n, const string& x) {
return NULL;
}
// first look for x
if (x == n->value) {
int cmp = n->value.compare(x);
if (cmp == 0) {
// found
// no children
if (n->left == NULL && n->right == NULL) {
Expand All @@ -86,7 +87,7 @@ AVLNode* AVLTree::remove(AVLNode*& n, const string& x) {
string sr = min(n->right);
n->value = sr;
n->right = remove(n->right, sr);
} else if (x < n->value) {
} else if (cmp < 0) {
n->left = remove(n->left, x);
} else {
n->right = remove(n->right, x);
Expand Down Expand Up @@ -154,8 +155,9 @@ void AVLTree::printTree(AVLNode* root, Trunk* prev, bool isRight) {

if (prev) prev->str = prev_str;
trunk->str = " |";

printTree(root->left, trunk, false);
delete trunk;
}

void AVLTree::printTree() { printTree(root, NULL, false); }
void AVLTree::printTree() { printTree(root, NULL, false); }