Hello, I am currently working on this project for my CS class and I am trying to find the number of binary tree nodes that are single parents. So I put some code into a traversal function to find this out, and it worked once and I was able to spit out 6 for the number of single parent nodes. However, now I keep getting this error message
Thread 1: EXC_BAD_ACCESS (code=1, address=0xe00000009)
It shows up next to my code root->lLink = nullptr; and if I take that away it shows up next to my code root->rLink=nullptr; :
template<class elemType>
binaryTreeType<elemType>::binaryTreeType() {
root->lLink = nullptr; //ERROR MSG HERE
root->rLink = nullptr;
}
It also shows up next to my inherited class.
template <class elemType>
class bSearchTreeType : public binaryTreeType<elemType> //ERROR MSG HERE
{...
I know I am kind of new to all of this, but anyone who could possibly help would be greatly appreciated. Thank you so much.
I've tried running it and every time I get this error message.
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 191 192 193 194 195 196 197 198 199 200 201 202 203
|
```
/*
//
//STACK CLASS WAS HERE... DIDN'T INPUT IT DUE TO SAVING SPACE
//
*/
template <class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
template <class elemType>
class binaryTreeType {
public:
void nonRecursiveInTraversal();
void insert(const elemType& insertItem);
static int singleParentCount;
binaryTreeType();
protected:
nodeType<elemType> *root;
};
template<class elemType>
binaryTreeType<elemType>::binaryTreeType() {
root->lLink = nullptr;
root->rLink = nullptr;
}
template<class elemType>
int binaryTreeType<elemType>::singleParentCount = 0;
template <class elemType>
void binaryTreeType<elemType> :: nonRecursiveInTraversal()
{
stackType<nodeType<elemType>*> stack;
nodeType<elemType> *current;
current = root;
while ((current != nullptr) || (!stack.isEmptyStack())){
if (current != nullptr)
{
stack.push(current);
if( (current->rLink == nullptr && current->lLink != nullptr) || (current->rLink != nullptr && current->lLink == nullptr) ){
singleParentCount += 1;
}
current = current->lLink;
}
else
{
current = stack.top();
stack.pop();
cout << current->info << " ";
current = current->rLink;
}
cout << endl;
}
}
template <class elemType>
class bSearchTreeType : public binaryTreeType<elemType>
{
public:
void insert(const elemType& insertItem);
bool search(const elemType& searchItem);
//bSearchTreeType();
};
/*
template<class elemType>
bSearchTreeType<elemType>::bSearchTreeType() : binaryTreeType<elemType> () {
}
*/
template <class elemType>
void bSearchTreeType<elemType>::insert (const elemType& insertItem)
{
nodeType<elemType> *current;
nodeType<elemType> *trailCurrent;
nodeType<elemType> *newNode;
newNode = new nodeType<elemType>;
newNode->info = insertItem;
newNode->lLink = nullptr;
newNode->rLink = nullptr;
if (this->root == nullptr)
{
this->root = newNode;
}
else
{
current = this->root;
while (current != nullptr)
//START WHILE
{
trailCurrent = current;
if(current->info == insertItem)
{
cout << "The item to be inserted is already in the tree -- duplicates are not allowed." << endl;
return;
}
else if(current->info > insertItem){
current = current-> lLink;
}
else {
current = current->rLink;
}
} //END WHILE
if (trailCurrent->info > insertItem)
trailCurrent->lLink = newNode;
else
trailCurrent->rLink = newNode;
} // END ELSE
} // END INSERT
//MAIN FUNCTION
//
//
int main() {
bSearchTreeType<int> hello;
hello.insert(4);
hello.insert(9);
hello.insert(12);
hello.insert(1);
hello.insert(8);
hello.insert(40);
hello.insert(41);
hello.insert(30);
hello.insert(7);
hello.insert(90);
hello.insert(50);
hello.insert(43);
hello.insert(6);
hello.nonRecursiveInTraversal();
cout << endl << endl << endl;
cout << "The amount of single parents in this binary search tree is:";
cout << hello.singleParentCount;
return 0;
}
|