This is seg faulting whenever I try to remove anything. I've been trying to fix it for hours, to no avail.
Any ideas?
#ifndef BST_H_
#define BST_H_
#include <stdexcept>
#include <iostream>
#include "btnode.h"
using namespace std;
/*
A class to represent a templated binary search tree.
*/
template <typename T>
class BST
{
private:
//pointer to the root node in the tree
BTNode<T>* root;
public:
//default constructor to make an empty tree
BST();
/*
You have to document these 4 functions
*/
void insert(T value);
bool search(const T& value) const;
bool search(BTNode<T>* node, const T& value) const;
void printInOrder() const;
void remove(const T& value);
//function to print out a visual representation
//of the tree (not just print the tree's values
//on a single line)
void print() const;
private:
//recursive helper function for "print()"
void print(BTNode<T>* node,int depth) const;
};
/*
Default constructor to make an empty tree
*/
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
void BST<T>::insert(T value)
{
BTNode<T>* newNode = new BTNode<T>(value);
cout << newNode->data;
if(root == NULL)
{
root = newNode;
return;
}
BTNode<T>* current = root;
while(true)
{
if(current->left == NULL && current->right == NULL)
break;
if(current->right != NULL && current->left != NULL)
{
if(newNode->data > current->data)
current = current->right;
else if(newNode->data < current->data)
current = current->left;
}
else if(current->right != NULL && current->left == NULL)
{
if(newNode->data < current->data)
break;
else if(newNode->data > current->data)
current = current->right;
}
else if(current->right == NULL && current->left != NULL)
{
if(newNode->data …