I have a problem with deleting node from BST. I wrote a method that searches a node, that works fine, and when I'm trying to delete a leaf (an only case that I wrote so far) it causes an error (0xDDDD...) in printing methods. I assume that is because printing methods encounter some kind of null, but I have no idea how to fix that. Here is the code...
Deleting node by value method:
void deleteNodeByValue(T val)
{
cout << "\nElement to delete: " << val << " \n";
Node<T>* tmp = root;
while (tmp != NULL)
{
if (val == tmp->data)
{
cout << "Element found: " << tmp->data << " \n";
if (tmp->right_child == NULL && tmp->left_child == NULL)
{
delete tmp;
tmp = NULL;
size--;
}
break;
}
else if (val > tmp->data)
{
tmp = tmp->right_child;
}
else if (val < root->data)
{
tmp = tmp->left_child;
}
}
}
and tree print:
string to_string()
{
stringstream ss;
Node<T>* tmp = root;
queue<Node<T>*> q;
while (!q.empty() || tmp != NULL)
{
if (tmp != NULL)
{
q.push(tmp);
tmp = tmp->left_child; //debugger shows error in this place
}
else
{
tmp = q.front();
q.pop();
ss << "Data: " << tmp->data;
if (tmp->left_child != NULL)
{
ss << " Left child: " << tmp->left_child->data;
}
if (tmp->right_child != NULL)
{
ss << " Right child: " << tmp->right_child->data;
}
ss << " \n";
tmp = tmp->right_child;
}
}
return ss.str();
It also causes error in other methods like getting tree height or getting trees in order, whatever.
What should I do?
Simple, you delete the node, but the parent of that node still points to it. So you are trying to print a deleted node.
You have to remember who was the parent of the node you delete and to update it to no longer point to it.
Many ways to do it, I personally would make tmp a double pointer if I was you but since your name is newbie here is a little easier to read solution:
cout << "\nElement to delete: " << val << " \n";
Node<T>* tmp = root;
Node<T>* parent = NULL;
while (tmp != NULL)
{
if (val == tmp->data)
{
cout << "Element found: " << tmp->data << " \n";
if (tmp->right_child == NULL && tmp->left_child == NULL)
{
if (parent == NULL) root = NULL;
else {
if (parent->right_child == tmp) {
parent->right_child = NULL;
} else {
parent->left_child = NULL;
}
}
delete tmp;
size--;
}
break;
}
else if (val > tmp->data)
{
parent = tmp;
tmp = tmp->right_child;
}
else if (val < root->data)
{
parent = tmp;
tmp = tmp->left_child;
}
}
Of course you have to add the handling for the case when node is not a leaf too, but it looks like you understand this.
Related
void deleteBasedOnID(student* head,student* tail,int stu_ID)
{
if (head == NULL)
{
std::cout << "nothing to be deleted";
}
else if (stu_ID == head->stu_ID)
{
//delete from the beginning
temp = head->next;
if (temp == NULL)
{
tail = NULL;
}
else
{
temp->prev = NULL;
}
std::cout << "Deleted ID: " << head->stu_ID << std::endl;
delete head;
head = temp;
}
else
{
//start from second
temp = head;
previous = NULL;
while (stu_ID != temp->stu_ID)
{
previous = temp;
temp = temp->next;
if (temp == NULL)
{
std::cout << "no such ID!" << std::endl;
return;
}
}
previous->next = temp->next;
if (temp->next == NULL)
{
tail = previous;
}
else
{
temp->next->prev = previous;
}
std::cout << "Deleted ID: " << temp->stu_ID << std::endl;
delete temp;
}
}
I have a student struct and global pointers head and tail, I put the said head and tail into deleteBasedOnID head and tail arguments
it works fine for if(head == NULL) or the else block but however whenever I try to delete from the beginning, it works fine internally but after I tried to display the list, error occurred.
This is my display function body
void display()
{
temp = head;
while (temp != NULL)
{
std::cout << "Student ID: " << temp->stu_ID << std::endl;
temp = temp->next;
}
}
main function
int main()
{
head = NULL;
tail = NULL;
temp = NULL;
int id;
std::cout << std::endl;
std::cout << "Enter the id you want to delete: ";
std::cin >> id;
deleteBasedOnID(head, tail, id);
std::cout << "New sorted list" << std::endl;
display();
return 0;
}
In your deleteBasedOnId-function, you change the value for the parameter head, which is a copy of the pointer to head you made in main. The original pointer in main keeps its value, which now points to deleted memory, hence the segfault. You can take a reference to the pointer to head as parameter to solve this problem:
void deleteBasedOnID(student*& head,student*& tail,int stu_ID)
Need a void function Display that will display the binary search tree in tree shape
Need a value return or void function Search that will search for a specific data.
Not really sure What to do from this point except trial and error and trying to read off of others code...
I need help
Help
Here is my code...
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL; }
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
bool Search(int, tree_node*);
void Display(tree_node*, int);
};
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
system("cls");
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << " ----------------------------- " << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Display " << endl;
cout << " 7. Display Message From Creator " << endl;
cout << " 8. EXIT " << endl;
cout << " Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1: cout << " Enter Number to be inserted : ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal " << endl;
cout << " --------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted : ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: cout << " Enter data to be deleted : ";
b.Display(NULL, tmp);
break;
case 7: cout << " I think you are a great person. Smile be happy ";
case 8:
return 0;
}
}
}
bool BinarySearchTree::Search(int d, tree_node* curr){
if (curr == NULL) {
return 0;
}
if (curr->data == d) {
return 1;
}
if (d > curr->data) {
return Search(d, curr->right);
}
else {
return Search(d, curr->left);
}
}
// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if (isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
//Locate the element
bool found = false;
if (isEmpty())
{
cout << " This Tree is empty! " << endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else
{
parent = curr;
if (d>curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data not found! " << endl;
return;
}
// 3 cases :
// 1. We're removing a leaf node
// 2. We're removing a node with a single child
// 3. we're removing a node with 2 children
parent = NULL;
// Node with single child
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL
&& curr->right == NULL))
{
if (curr->left == NULL && curr->right != NULL)
{
if (parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else // left child present, no right child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
//We're looking at a leaf node
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while (lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else
{
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left) preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::print_postorder()
{
postorder(root);
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
void BinarySearchTree::Display(tree_node *curr, int indent)
{
if (curr != NULL)
{
cout << "My Tree. The left is top and right is bottom" << endl;
Display(curr->left, indent + 4);
if (indent > 0)
cout << setw(indent) << " ";
cout << curr->data << endl;
Display(curr->right, indent + 4);
}
}
I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
//Binary Search Tree
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool IsEmpty() const {
return root == NULL;
}
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
};
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if (IsEmpty()) root = t;
else
{
tree_node* curr;
curr = root;
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
tree_node* curr;
tree_node* parent;
bool found= false;
if (IsEmpty())
{
cout << " This Tree is Empty!" << endl;
return;
}
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else {
parent = curr;
if (d > curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data is not found!" << endl;
return;
}
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL))
{
if (curr->left == NULL && curr->right !=NULL)
{
if (parent -> left == curr)
{
parent->left = curr->right;
delete curr;
}
else {
parent->right = curr->right;
delete curr;
}
}
else //Left child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 Children
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else
{
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr ->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else {
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left)preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << "---------------------------------" << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Exit " << endl;
cout << " Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: cout << "Enter Number to be Inserted: ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal: " << endl;
cout << "---------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal: " << endl;
cout << "------------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal: " << endl;
cout << "--------------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted: ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: system("pause");
return 0;
break;
}
}
}
tree_node* parent;
You declare parent but never assign it a tree_node memory address to point to. Thus, performing operations on it or accessing its members (like parent->left) will cause issues, and raise the error you are getting.
Solution: Assign a value to parent before accessing it's members.
I am currently writing a binary tree project in c++ and it is giving me some errors that I am struggling to understand. The error in question is a segmentation error that I have no idea how to fix. Below is the code from one one the files where all the functions are written. The function in particular that is giving me an issue is the last one - the print_pre_order_private function.
#include <iostream>
#include <cstdlib>
#include "BT.h"
using namespace std;
BT::BT()
{
root = NULL; //makes sure pointer isn't pointing at anything
}
BT::node* BT::create_leaf(int key)
{
node* n = new node;
n->key = key;
n->left = NULL;
n->right = NULL;
return n;
}
void BT::add_leaf(int key)
{
add_leaf_private(key, root);
}
void BT::add_leaf_private(int key, node* Ptr)
{
if(root == NULL)
{
root = create_leaf(key);
}
else if(key < Ptr->key)
{
if(Ptr->left != NULL)
{
add_leaf_private(key, Ptr->left);
}
else
{
Ptr->left = create_leaf(key);
}
}
else if(key > Ptr->key)
{
if(Ptr->right != NULL)
{
add_leaf_private(key, Ptr->right);
}
else
{
Ptr->right = create_leaf(key);
}
}
else
{
cout << "The key " << key << " has already been added to the tree\n";
}
}
void BT::print_in_order()
{
print_in_order_private(root);
}
void BT::print_in_order_private(node* Ptr)
{
if(root != NULL)
{
if(Ptr->left != NULL)
{
print_in_order_private(Ptr->left);
}
cout << Ptr->key << " ";
if(Ptr->right != NULL)
{
print_in_order_private(Ptr->right);
}
}
else
{
cout << "The tree is empty\n";
}
}
BT::node* BT::return_node(int key)
{
return return_node_private(key, root);
}
BT::node* BT::return_node_private(int key, node* Ptr)
{
if(Ptr != NULL)
{
if(Ptr->key == key)
{
return Ptr;
}
else
{
if(key < Ptr->key)
{
return return_node_private(key, Ptr->left);
}
else
{
return return_node_private(key, Ptr->right);
}
}
}
else
{
return NULL;
}
}
int BT::return_root_key()
{
if(root != NULL)
{
return root->key;
}
else
{
return -1;
}
}
void BT::print_children(int key)
{
node* Ptr = return_node(key);
if(Ptr != NULL)
{
cout << "Parent Node = " << Ptr->key << endl;
Ptr->left == NULL ?
cout << "Left child = NULL\n":
cout << "Left child = " << Ptr->left->key << endl;
Ptr->right == NULL ?
cout << "Right child = NULL\n":
cout << "Right child = " << Ptr->right->key << endl;
}
else
{
cout << "Key " << key << " is not in the tree\n";
}
}
int BT::find_smallest()
{
return find_smallest_private(root);
}
int BT::find_smallest_private(node* Ptr)
{
if(root == NULL)
{
cout << "The tree is empty\n";
return -1;
}
else
{
if(Ptr->left != NULL)
{
return find_smallest_private(Ptr->left);
}
else
{
return Ptr->key;
}
}
}
void BT::remove_node(int key)
{
remove_node_private(key, root);
}
void BT::remove_node_private(int key, node* parent)
{
if(root != NULL)
{
if(root->key == key)
{
remove_root_match();
}
else
{
if(key < parent->key && parent->left != NULL)
{
parent->left->key == key ?
remove_match(parent, parent->left, true) :
remove_node_private(key, parent->left);
}
else if(key < parent->key && parent->right != NULL)
{
parent->right->key == key ?
remove_match(parent, parent->right, true) :
remove_node_private(key, parent->right);
}
else
{
cout << "The key " << key << " was not found in the tree\n";
}
}
}
else
{
cout << "The tree is empty\n";
}
}
void BT::remove_root_match()
{
if(root != NULL)
{
node* delPtr = root;
int root_key = root->key;
int smallest_right_subtree;
//case 0 - 0 children
if(root->left == NULL && root->right == NULL)
{
root = NULL;
delete delPtr;
}
//case 1 - 1 child
else if(root->left == NULL && root->right != NULL)
{
root = root->right;
delPtr->right = NULL;
delete delPtr;
cout << "The root node with key " << root_key << " was deleted. "
<< " The new root contains key " << root->key << endl;
}
else if(root->right == NULL && root->left != NULL)
{
root = root->left;
delPtr->left = NULL;
delete delPtr;
cout << "The root node with key " << root_key << " was deleted. "
<< " The new root contains key " << root->key << endl;
}
//case 2 - 2 children
else
{
smallest_right_subtree = find_smallest_private(root->right);
remove_node_private(smallest_right_subtree, root);
root->key = smallest_right_subtree;
cout << "The rout key containing key " << root_key
<< " was overwritten with key " << root->key << endl;
}
}
else
{
cout << "Cannot remove root. Tree is empty\n";
}
}
void BT::remove_match(node* parent, node* match, bool left)
{
if(root != NULL)
{
node* delPtr;
int match_key = match->key;
int smallest_right_subtree;
//case 0 - 0 children
if(match->left == NULL && match->right == NULL)
{
delPtr = match;
left == true ? parent->left = NULL : parent->right = NULL;
delete delPtr;
cout << "The node containing key " << match_key << " was removed\n";
}
//case 1 - 1 child
else if(match->left == NULL && match->right != NULL)
{
left == true ? parent->left = match->left : parent->right = match->right;
match->left = NULL;
delPtr = match;
delete delPtr;
cout << "The node containing key " << match_key << " was removed\n";
}
//case 2 - 2 children
else
{
smallest_right_subtree = find_smallest_private(match->right);
remove_node_private(smallest_right_subtree, match);
match->key = smallest_right_subtree;
}
}
else
{
cout << "Cannot remove match. The tree is empty";
}
}
void print_pre_order()
{
print_pre_order_private(root);
}
void print_pre_order_private(node* Ptr)
{
if(root != NULL)
{
cout << Ptr->key << " ";
print_pre_order_private(Ptr->left);
print_pre_order_private(Ptr->right);
}
}
Any help in fixing this situation is greatly appreciated. Thanks in advance. :)
In print_pre_order_private(node* Ptr) function. Shouldn't this be "if(Ptr != NULL)" instead of "if(root != NULL)" ?. I think there is the crash.
Since you have designed "print_pre_order_private()" function as a recursive function you should check for the validity of "Ptr" instead of always checking for the validity of "root", since the left/right/both of the child may be NULL at the very bottom of the tree depending on your input.
I'm working on a program that works with a binary tree. I'm getting an error with adding new nodes into the tree. I can add one node, but after adding another, I get a STATUS_ACCESS_VIOLATION error. I think the error could be with the function arguments dealing with the search function. Please help me if you can.
Here is he .h file that i've written:
#ifndef P4_H
#define P4_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
struct TreeNode //ListNode structure with components
{
int acctNum;
TreeNode *left;
TreeNode *right;
};
typedef TreeNode* nodePtr; //defines a pointer to a treenode struct
class Tree
{
private:
nodePtr root;
int numElements;
public:
Tree()
{ root = NULL; numElements = 0; }
bool treeEmpty()
{ return (numElements == 0); }
void addNode()
{
int key;
cout << "Enter account number to add: ";
cin >> key;
cout << endl << endl;
nodePtr location = NULL, parent = NULL, p = NULL;
bool found = searchTree(key, &location, &parent);
cout << found << " " << location << " " << parent << endl << endl;
if (found)
{
cout << "Error! Account number: " << key << " already exists within"
<< " the tree." << endl << endl;
}
else
{
if (parent == NULL)
{
root = new TreeNode;
root->acctNum = key;
}
else
{
if (parent->acctNum > key)
{
parent->left = new TreeNode;
p = parent->left;
p->acctNum = key;
}
else
{
parent->right = new TreeNode;
p = parent->right;
p->acctNum = key;
}
}
}
}
bool searchTree(int key, nodePtr *location, nodePtr *parent)
{
bool found = false;
nodePtr p = root;
*location = root;
parent = NULL;
while (p != NULL && !found)
{
*location = p;
if (key == p->acctNum)
found = true;
else if (key < p->acctNum)
{
*parent = p;
p = p->left;
}
else if (key > p->acctNum)
{
*parent = p;
p = p->right;
}
}
return found;
}
void deleteNode()
{
int key;
nodePtr location = NULL, parent = NULL;
cout << "Enter account number to delete: ";
cin >> key;
cout << endl << endl;
bool found = searchTree(key, &location, &parent);
if (!found)
{
cout << "Error! Account number: " << key << " was not found."
<< endl << endl;
}
else if (location->left != NULL && location->right != NULL)
{ //delete node with left and right subtrees
nodePtr leftmost = location->right, leftmostParent = NULL;
while (leftmost->left != NULL)
{
leftmostParent = leftmost;
leftmost = leftmost->left;
}
leftmost->left = location->left;
leftmost->right = location->right;
leftmostParent->left = NULL;
if (parent->acctNum > location->acctNum)
parent->left = leftmost;
else
parent->right = leftmost;
delete location;
location = NULL;
}
else if (location->left != NULL && location->right == NULL)
{ //delete node with only a left subtree
if (parent->acctNum > location->acctNum)
parent->left = location->left;
else
parent->right = location->left;
delete location;
location = NULL;
}
else if (location->left == NULL && location->right != NULL)
{ //delete node with only a right subtree
nodePtr leftmost = location->right, leftmostParent = NULL;
while (leftmost->left != NULL)
{
leftmostParent = leftmost;
leftmost = leftmost->left;
}
leftmost->right = location->right;
leftmostParent->left = NULL;
if (parent->acctNum > location->acctNum)
parent->left = leftmost;
else
parent->right = leftmost;
delete location;
location = NULL;
}
else
{ //delete a leaf node
if (location->acctNum > parent->acctNum)
parent->right = NULL;
else
parent->left = NULL;
delete location;
location = NULL;
}
}
void displayAscend(nodePtr p, int count)
{
if (p->left != NULL)
displayAscend(p->left, count);
cout << count << ". " << p->acctNum << endl;
count ++;
if (p->right != NULL)
displayAscend(p->right, count);
}
void displayDescend(nodePtr p, int count)
{
if (p->right != NULL)
displayAscend(p->right, count);
cout << count << ". " << p->acctNum << endl;
count ++;
if (p->left != NULL)
displayAscend(p->left, count);
}
void readFile()
{
char filename[50];
cout << "Enter name of file to open: ";
cin.getline(filename,51);
cout << endl << endl;
ifstream inFile;
inFile.open(filename);
while (!inFile)
{
cout << "Error opening file! Please try again." << endl << endl;
cout << "Enter name of file: ";
cin.getline(filename,51);
cout << endl << endl;
}
int num;
while (!inFile.eof())
{
inFile >> num;
nodePtr location = NULL, parent = NULL, p = NULL;
bool found = searchTree(num, &location, &parent);
if (found)
{
cout << "Error! Account number: " << num << " already exists"
<< " within the tree." << endl << endl;
}
else
{
if (parent == NULL)
{
root = new TreeNode;
root->acctNum = num;
}
else
{
if (parent->acctNum > num)
{
parent->left = new TreeNode;
p = parent->left;
p->acctNum = num;
}
else
{
parent->right = new TreeNode;
p = parent->right;
p->acctNum = num;
}
}
}
}
}
};
#endif
In searchTree, you set parent to NULL before the loop, then dereference it in the loop.