Help with BST implementation

mandrewb (1)
Hey guys, I'm working on a BST implementation for class and I cannot for the life of me figure out the segfaults.

Here is my code:
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef BST_H
#define	BST_H

#include <iostream>

using namespace std;

template <typename Comparable>
class BinarySearchTree
{
public:
    //TEST FUNCTIONS
    void printRoot() const;
    
    //Creating a BST
    void create(const Comparable &theElement);
    
    //Inserting a new value into an existing BST
    void insertElement(const Comparable &theElement);
    
    //Overloaded assignment operator
    const BinarySearchTree<Comparable> &operator=
                        (const BinarySearchTree<Comparable> &tree);
    
    //Preorder traversal
    void preorderTraversal();
    
    //Default constructor
    BinarySearchTree();
    
    //Copy constructor
    BinarySearchTree(const BinarySearchTree<Comparable> &otherTree);
    
    //Destructor
    //~BinarySearchTree();

private:
    struct BinaryNode
    {
        Comparable element;
        BinarySearchTree<Comparable> *left;
        BinarySearchTree<Comparable> *right;

        BinaryNode(const Comparable &theElement,
                   BinarySearchTree<Comparable> *leftTree,
                   BinarySearchTree<Comparable> *rightTree)
                : element(theElement), left(leftTree), right(rightTree) {}
    };
    
    BinaryNode *root;
    
    
    void copyTree(BinarySearchTree<Comparable> *&copiedTree,
                  BinarySearchTree<Comparable> *otherTree);
    
    void destroyTree(BinarySearchTree<Comparable> *destroyed);
    
    void insert(const Comparable &theElement, 
                BinarySearchTree<Comparable> *tree) const;
    
    void preorder(BinarySearchTree<Comparable> *t) const;
    
};

/*********************************FUNCTIONS************************************/

template <typename Comparable>
void BinarySearchTree<Comparable>
                ::copyTree(BinarySearchTree<Comparable> *&copiedTree,
                           BinarySearchTree<Comparable> *otherTree)
{
    if (otherTree == NULL)
        copiedTree = NULL;
    else
    {
        copiedTree = new BinarySearchTree<Comparable>;
        copiedTree->root->element = otherTree->root->element;
        copiedTree(copiedTree->root->left, otherTree->root->left);
        copiedTree(copiedTree->root->right, otherTree->root->right);
    }
}//end copyTree

template <typename Comparable>
void BinarySearchTree<Comparable>
        ::destroyTree(BinarySearchTree<Comparable> *destroyed)
{
    if (destroyed != NULL)
    {
        destroyTree(destroyed->root->left);
        destroyTree(destroyed->root->right);
        delete destroyed;
        destroyed = NULL;
    }
}

//Test function used in driver to print root
template <typename Comparable>
void BinarySearchTree<Comparable>::printRoot() const
{
    cout << this->root->element;
}

//Creates BST
template <typename Comparable>
void BinarySearchTree<Comparable>::create(const Comparable &theElement)
{ 
    if (this->root == NULL)
        this->root = new BinaryNode(theElement, NULL, NULL);
}

template <typename Comparable>
void BinarySearchTree<Comparable>
        ::insert(const Comparable &theElement,
                 BinarySearchTree<Comparable> *tree) const
{
    if (tree == NULL)
    {
        tree = new BinarySearchTree<Comparable>;
        tree->root = new BinaryNode(theElement, NULL, NULL);
    }
    else if (theElement < tree->root->element)
        insert(theElement, tree->root->left);
    else if (theElement > tree->root->element)
        insert(theElement, tree->root->right);     
}

template <typename Comparable>
void BinarySearchTree<Comparable>::insertElement(const Comparable &theElement)
{
    insert(theElement, this);
}

template <typename Comparable>
BinarySearchTree<Comparable>::BinarySearchTree()
{
    this->root = NULL;
}//end default constructor

template <typename Comparable>
BinarySearchTree<Comparable>
        ::BinarySearchTree(const BinarySearchTree<Comparable> &otherTree)
{
    if (otherTree.root == NULL)
        root = NULL;
    else
        copyTree(this, otherTree);
}//end copy constructor

//Disabled destructor - if used it would not print my output to screen using
//printRoot function. (?)
//template <typename Comparable>
//BinarySearchTree<Comparable>::~BinarySearchTree()
//{
//    destroyTree(this);
//}

template <typename Comparable>
const BinarySearchTree<Comparable> &BinarySearchTree<Comparable>
        ::operator=(const BinarySearchTree<Comparable> &tree)
{
    if (this != &tree)
    {
        if (root != NULL)
            destroyTree(this);
        if (tree.root == NULL)
            root = NULL;
        else
            copyTree(this, tree);
    }
}//end overloaded assignment

template <typename Comparable>
void BinarySearchTree<Comparable>
        ::preorder(BinarySearchTree<Comparable> *t) const
{
    if (t->root != NULL)
    {
        cout << t->root->element << " ";
        preorder(t->root->left);
        preorder(t->root->right);
    }
}

template <typename Comparable>
void BinarySearchTree<Comparable>::preorderTraversal()
{
    preorder(this);
}

#endif 


Thanks for the help!
Registered users can post here. Sign in or register to post.