Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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

What I have tried:

I'm sure the problem is here somewhere, I've mostly been eliminating loops trying to find the problem

not sure where to start

it's a seg fault core dump
Posted
Updated 2-Nov-17 22:40pm
Comments
Peter_in_2780 2-Nov-17 21:04pm    
One word. DEBUGGER
Member 13500537 2-Nov-17 22:00pm    
Debugger just told me it's crashing at if(x < node->data) and that statement should be right?

in the insert function first iteration
nv3 3-Nov-17 2:16am    
Well, if the debugger blames the statement "if (x < node->data)" then look at the value of node. My guess is that it is 0 or undefined, which in produces the segmentation fault. The next step is to find out, why node has the wrong value in that particular test situation. That's how debugging works.

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.
C++
#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[^]
 
Share this answer
 
Quote:
Debugger just told me it's crashing at if(x < node->data) and that statement should be right?

because you didn't perform a sanity check:
C++
if ( ! node )
{
  // handle error
}
or
C++
assert(node);

Anyway, as others suggested, then you have to use the debugger to discover why the buggy condition occurred.
 
Share this answer
 
Your root member is not initialised.
I guess it should be set to the value passed in the constructor:
C++
binSTree( Node <t> *r = NULL ): binTree<t>::binTree(r), root(r) {}

You must also set the passed Node pointer to NULL after deleting it in the remove() function:
// ...
Node<t>* temp = pred(node);
temp->left = node->left;
delete node;
// Insert this line after each delete
node = NULL;
// ...

Your pred() function returns always NULL:
Node <t>* pred( Node<t>*& node)
{
    // Missing check for NULL here
    node = node->left;
    while(node != NULL)
        node = node->right; 
    // node is always NULL here!
    return node;
}
The function is also changing the content because node is passed by reference. It would be better to pass a const pointer instead and make the function const too.

Finally you should - as already suggested - always check if passed Node pointers are NULL. But these checks will fail (indicate success) for non-null but invalid pointers (not initialised or deleted meanwhile).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900