Quote:
How do I get rid of my segmentation fault
Your code is not autonomous, we can't reproduce the problem, just guess.
Use the debugger to inspect variables, and check that any empty pointer is NULL as expected.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include "binTree.h"
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;
#ifndef BINSTREE_H
#define BINSTREE_H
template <class T>
class binSTree : public binTree <t>
{
public:
binSTree( Node <t> *r = NULL ): binTree<t>::binTree(r) {}
void insert(const T& x){ insert(root, x); }
void remove(const T& x){ remove(root, x); }
unsigned size() const{ return size(root); }
protected:
Node <t> * root;
private:
void insert(Node <t>*&, const T& );
void remove(Node <t> *&, const T&);
unsigned size( Node<t>* node) const{
int l;
int ll;
int lr;
if(!node)
{
l = 0;
}
else if(!node->left && !node->right)
{
l = 1;
}
else{
ll = size(node->left);
lr = size(node->right);
l=ll+lr;
}
return l;
}
Node <t>* pred( Node<t>*& node){
node = node->left;
while(node != NULL)
{
node = node->right;
}
return node;}
};
template <class T>
void binSTree<t>::insert(Node <t>*& node, const T& x)
{
if(!node)
{
node = new Node<t>(x);
}
else if( x < node->data )
{
insert(node->left, x);
}
else if( x > node->data )
{
insert(node->right, x);
}
}
template <class T>
void binSTree<t>::remove(Node <t> *& node, const T& x)
{
if(x < node->data)
{
remove(node->left, x);
}
else if(x > node->data)
{
remove(node->right, x);
}
else if(x == node->data)
{
if(node->left == NULL && node->right == NULL)
{
delete node;
}
else if( node->left != NULL )
{
Node<t>* temp = pred(node);
temp->left = node->left;
delete node;
}
else if(node->right != NULL )
{
Node<t>* temp = pred(node);
temp->right = node->right;
delete node;
}
else
{
Node<t>* temp = pred(node);
node = node->left;
while(node->right != NULL)
{
node = node->right;
}
node = temp;
Node<t>* temp2 = pred(node);
delete temp;
temp2->left = node;
}
}
}
#endif
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[
^]
ultraedit[
^]