Getting a "SIGSEGV" segmentation fault and unsure why - c++

My program requires me to create a binary search tree that is also a set. I've got up to inserting items into it and having that work correctly, but my issue comes when I'm attempting to get recursively get the size of the tree, aka how many nodes there are. Below is all the code that matters I believe.
struct SetNode
{
T data;
SetNode<T>* left;
SetNode<T>* right;
SetNode(const T& value);
};
//Set based on a BST
template <class T>
class MySet
{
private:
SetNode<T>* root;
public:
//constructor, insert function, "contains" function declared here
//get number of items contained
int size() const;
int sizeHelper(SetNode<T>* curNode) const;
}
template<typename T>
int MySet<T>::size() const {
if (root == nullptr)
return 0;
else
return this->sizeHelper(root);
}
template<typename T>
int MySet<T>::sizeHelper(SetNode<T>* curNode) const {
return 1 + sizeHelper(curNode->left) + sizeHelper(curNode->right);
}
The issue arises in main after I declare Set<string> setA and attempt to call size with setA.size(). From the debugger, I've seen that this causes the aforementioned SIGSEGV error. I can change the declaration of sizeHelper and even remove it if need be, but other than the code within it, size must remain as it is. Should sizeHelper be a non-member function? Removing the const doesn't work.

Your sizeHelper is a recursive function with no exit condition, you just keep reading the left and right fields from the node you're given, but you never check if they are nullptr. If you do pass nullptr, you have UB and possibly a segfault.
To avoid it you need to add an exit condition like so.
template<typename T>
int MySet<T>::sizeHelper(SetNode<T>* curNode) const {
if (curNode == nullptr) {
return 0;
}
return 1 + sizeHelper(curNode->left) + sizeHelper(curNode->right);
}

Related

Pointer to Array throwing errors when I try to assign an index to nullptr

Class:
template <class T>
class vectorADT
{
public:
//default constructor
vectorADT();
//destructor
~vectorADT();
//push data to the front of the vector
void push_front(T data);
//push data to the rear of the vector
void push_back(T value);
void insert(int position, T value);
//remove data from the front of the vector
void remove_front();
//remove data from the rear of the vector
void remove_rear();
//return the front of the vector
T getFront();
//return the rear of the vector
T getRear();
//check if vector is full
bool isFull();
//create a new vector with more space
T *resize(T *prevSizePtr);
//return the size of the vector
int size();
//check if the vector is empty
bool isEmpty();
//print the vectors data
void print();
private:
T *vectPtr;
T array[4] = {};
int front;
int rear;
int vectSize;
};
constructor:
template <class T>
vectorADT<T>::vectorADT()
{
front = 0;
rear = -1;
vectSize = 4;
vectPtr = array;
}
Class Method:
template <class T>
void vectorADT<T>::push_front(T data)
{
if (vectPtr[0] == nullptr)
{
vectPtr[front] = data;
}
front++;
}
I asked a question earlier but I am still really confused on how this works. I want to be able to check if my array has nullptr as a value, that way I know whether that index is empty, and if it is, I can assign that index some data. Whenever I try to make the comparison to nullptr, I get a host of errors about operator==. I thought that when I do T array[4] = {} it would initialize all the index to nullptr or zero, thus making it valid to compare that index with nullptr, however that obviously is not the case. If anyone could point me in the right direction on how I would go about doing something like that I would really appreciate it. Thank you.
Re: I want to be able to check if my array has nullptr as a value, that way I know whether that index is empty - You really don't need to know that. You should maintain a size of your vector that would tell you if there are available slots and where they are. What you call vectSize is in fact its capacity.

Huffman coding and priority queue

I had an assignment today and I'm really struggling trying to find a solution.
"Using a Binary-Search-Tree based priority queue, implement Huffman Coding [...]".
So, basically, I have to write my own priority queue based on a Binary Search Tree.
I managed to get a working BST and so on, but I've been really smashing my head against every wall In my room for the last two-tree days for the second part.
And that's Huffman's fault (Just kidding, I know I'm kinda stupid).
In Huffman Algorithm, we create a priority queue, fill it with our N starting elements, then we pop() the 2 nodes with minimum frequences, create a new node whose frequence is the sum of the previously popped elements and push() that node into the priority queue and reiterate[...].
To make my BST into a priority queue, I added one member and two methods: min,getMin() and extractMin().
min is a pointer to the node whose value is the lowest in the tree.
getMin() is a method that starts from a given node, and looks for the minimum value in its left subtree.
Please, note that everytime a node gets removed or a new node inserted, it calls an update of the min variable of the tree (a new inserted node could have a lower value than the previous min, so mingets updated, and the node pointer by min could be removed, somin gets updated).
extractMin() is basically a wrapper for pQueue.remove(pQueue.getMin()).
The thing is this: after popping the two elements with lowest frequency, and creating the new node, inserting it in the tree causes a min update. Since the left and right members of the new inserted node have a lower frequence than the inserted note itself, the min variable is set to one of those two members. I am struggling to find a solution to this. I don't want code or lines, I just want some ideas because I really ran out of patience and intelligence.
Following, my BST class and a snippet of the Huffman code I wrote. Please, be kind, I'm new to coding.
template <class T>
class BST
{
private:
void setRoot(bstNode<T>* nd){this->root=nd;}
bool isEmpty()const{if (this->getRoot()==nullptr) return true; else return false;}
protected:
bstNode<T>* root;
bstNode<T>* min;
public:
BST(void){this->root=nullptr;this->min=nullptr;}
BST(bstNode<T>* rt){this->root=rt;}
~BST(void){this->root=nullptr;};
void inorder(bstNode<T>*) const;
void insert(bstNode<T>*);
bool remove(bstNode<T>*);
void extractMin();
bstNode<T>* getRoot()const {return this->root;}
bstNode<T>* getMin(bstNode<T>*)const;
bstNode<T>* getMin()const {return this->min;} //simply returns a pointer to the minimum
void setMin(bstNode<T>* nd){this->min=nd;}
};
template <class T>
void BST<T>::extractMin()
{
if (this->getMin()!=nullptr)
this->remove(this->getMin());
else return;
}
template <class T>
void BST<T>::insert(bstNode<T>* nd)
{
if (this->isEmpty()==true)
{
this->setRoot(nd);
this->setMin(nd);
return;
}
else
{
bstNode<T>* up=nullptr;
bstNode<T>* actual=this->getRoot();
while (actual!=nullptr)
{
up=actual;
if(nd->getValue()<=actual->getValue())
actual=actual->getLeft();
else actual=actual->getRight();
}
nd->setParent(up);
if (nd->getValue()<=up->getValue())
up->setLeft(nd);
else up->setRight(nd);
}
if (nd->getValue()<=this->getMin()->getValue())
this->setMin(nd);
}
template <class T>
bool BST<T>::remove(bstNode<T>* nd)
{
if (nd==this->getMin())
this->setMin(nullptr);
if (nd==this->getRoot() && nd->getRight()==nullptr && nd->getLeft()==nullptr)
{
this->setRoot(nullptr);
return true;
}
if (nd==this->getRoot() && nd->getRight()!=nullptr)
this->setRoot(nd->getRight());
else if( nd==this->getRoot() && nd->getLeft()!=nullptr)
this->setRoot(nd->getLeft());
bstNode<T>* Root=this->getRoot();
bstNode<T>* temp, *temp2;;
if (nd->getLeft()==nullptr)
this->swapTree(Root,nd,nd->getRight());
else if (nd->getRight()==nullptr)
this->swapTree(Root,nd,nd->getLeft());
else
{
temp=this->getMin(nd->getRight());
if (temp->getParent()!=nd)
{
this->swapTree(Root,temp,temp->getRight());
temp->setRight(nd->getRight());
temp2=temp->getRight();
temp2->setParent(temp);
}
this->swapTree(Root,nd,temp);
temp->setLeft(nd->getLeft());
temp2=temp->getLeft();
temp2->setParent(temp);
}
this->setMin(this->getMin(this->getRoot()));
return true;
}
template <class T>
bstNode<T>* BST<T>::getMin(bstNode<T>* nd)const //find and return the minimum of the tree whose root is nd
{
while (nd->getLeft()!=nullptr)
nd=nd->getLeft();
return nd;
}
and here's the Huffman part:
template <class T>
class Encoder
{
private:
std::vector <myTuple> *alphabet; //vector of tuples <fequency,character, isInternal>
void createPqueue();
void createAlphabet();
void encode();
void showHuff(bstNode<T>*, string);
BST<T> *hTree;
public:
Encoder(){createAlphabet();createPqueue();};
~Encoder(){};
std::vector <myTuple> * getAlphabet()const{return this- >alphabet;}
BST<T> *getPqueue()const{return this->hTree;}
void askWhat();
};
template <class T>
void Encoder<T>::createPqueue()
{
this->hTree=new BST<myTuple>();
if (this->hTree==nullptr)
{
cout<<"Error allocating Red-Black Tree, now exiting..."<<endl;
exit(-1);
}
}
for (unsigned int i = 0; i < this->getAlphabet()->size(); ++i)
{
bstNode<myTuple>* temp;
temp=new bstNode<myTuple>(this->getAlphabet()->at(i));
if (temp!=nullptr)
this->getPqueue()->insert(temp); //fill the priority Queue with <int frequency, char character, bool is_internal> Nodes. I still have to remove tuples since they are not necessary anymore.
else
exit(-1);
}
bstNode<myTuple> *left, *right, *top;
for (unsigned int i = 0; i< u_int(this->getAlphabet()->size())-2;i++)
{
left=this->getPqueue()->getMin();
this->getPqueue()->extractMin();
right=this->getPqueue()->getMin();
this->getPqueue()->extractMin();
myTuple temp ((get<0>( left->getValue() ) + get<0>( right->getValue() )),'\0',true);
top=new bstNode<myTuple>(temp);
if (top==nullptr)
{
cout <<"Can't allocate top, now exiting..."<<endl;
exit(-1);
}
top->setLeft(left);
top->setRight(right);
this->getPqueue()->insert(top);
}
I've been using the bool value in tuples to distinguish between internal and external nodes, but with no success.
Thank you in advance and I'm really sorry if I've been messy, my clearness equals my state of mind as of now. Thank you.

Object not instantiating properly

I am currently using VS2015 for this.
I am trying to create a binary search tree in c++ so that I can learn both the language and the data structure while trying to see if I can follow good practices. However, I am coming through a problem where I am not properly instantiating the object properly in the driver file.
BSTHeader.h
#pragma once
/*
Properties of Binary Search Tree:
1.) Elements less than root will go to the left child of root
2.) Elements greater than root will go to the right child of root
*/
#include <memory>
// Binary Search Tree handler class
class BSTHeader {
/*
Naive implementation of BSTNode (non-generic version)
Nested class is private, but it's internal fields and member functions
are public to outer class: BSTHeader
*/
class BSTNode {
public:
int data;
std::unique_ptr<BSTNode> left;
std::unique_ptr<BSTNode> right;
BSTNode(int val) {
data = val;
left = NULL;
right = NULL;
}
~BSTNode() {}
};
std::unique_ptr<BSTNode> root; // Root of BST
unsigned int size; // Total amount of nodes in tree from root
public:
BSTHeader();
BSTHeader(int val);
~BSTHeader();
bool insert(std::unique_ptr<BSTNode>& root, int val);
}
BSTHeader.cpp
#include "BSTHeader.h"
/*
Constructors:
*/
BSTHeader::BSTHeader() {
root = NULL;
size = 0;
}
BSTHeader::BSTHeader(int val) {
root = std::unique_ptr<BSTNode>(new BSTHeader::BSTNode(val)); // Smart pointer to an internal BSTNode
size = 1;
}
BSTHeader::~BSTHeader() {} // Empty destructor from use of smart pointer
/*
Member functions:
*/
bool BSTHeader::insert(std::unique_ptr<BSTNode>& root, int val) {
if (root == NULL) { // Place new element here
root = std::unique_ptr<BSTNode>(new BSTHeader::BSTNode(val));
size++;
return true;
}
if (val < root.get()->data) { // val < root
insert(root.get()->left, val);
}
else if (val > root.get()->data) { // val > root
insert(root.get()->right, val);
}
The issue I get is here, where I believe I am trying to instantiate a BSTHeader object.
Program.cpp
#include "BSTHeader.h"
int main()
{
BSTHeader::BSTHeader bst(); // <----- ERROR
return 0;
}
The error I am getting is cannot determine which instance of overloaded function "BSTHeader:BSTHeader" is intended
However, whenever I do:
BSTHeader bst()
I am not able to access the insert(..., ...) function for the object doing bst.insert(..., ...) due to expression must have class type even though the error above does not appear.
Yet everything works fine and I am able to access all the member methods by doing this: BSTHeader bst(5) by using the overloaded constructor.
I am not sure whether its a namespace issue or not. I feel as though I am missing something.
The line
BSTHeader::BSTHeader bst(); // <----- ERROR
is a declaration of a function named bst that takes no arguments and returns a BSTHeader::BSTHeader.
This is known as the "most vexing parse", and often described in less polite language.
If you want to instantiate an instance, giving the constructor no arguments, remove the ().

C++ Trouble overloading operators in a template class

Every time I add comments inside the definitions of the operators, it starts giving me errors, but removing the comments immediately gets rid of the errors. I don't see why comments would have any effect at all on the code. Also just general advice on the overloading of operators in general would be appreciated.
Heres my class template:
template<class THING>
struct LLNode
{
THING data;
LLNode<THING> *next;
LLNode<THING> *prev;
};
template<class THING>
class LinkedList
{
private:
//use a doubly linked-list based implementation
//keep a head and tail pointer for efficiency
LLNode<THING> *Head;
LLNode<THING> *Tail;
int count;
public:
//setup initial conditions
LinkedList();
//delete all dynamic memory, etc.
~LinkedList();
//constant bracket operator to access specific element
const THING& operator[](int);
//Bracket operator to access specific element
THING& operator[](int);
//Equality operator to check if two lists are equal
bool operator==(const LinkedList<THING>&);
//Inequality operator to check if two lists are equal
bool operator!=(const LinkedList<THING>&);
//add x to front of list
void addFront(THING);
//add x to back of list
void addBack(THING);
//add x as the ith thing in the list
//if there are less than i things, add it to the back
void add(THING, int);
//remove and return front item from list
THING removeFront();
//remove and return back item from list
THING removeBack();
//return value of back item (but don't remove it)
THING getBack();
//return value of front item (but don't remove it)
THING getFront();
//return how many items are in the list
int length();
//print all elements in the linked list
void print();
};
And the operators I'm currently working on:
template<class THING>
THING& LinkedList<THING>::operator[](int index)
{
}
template<class THING>
bool LinkedList<THING>::operator==(const LinkedList<THING>& list_one, const LinkedList<THING>& list_two)
{
//checking for same size on both lists
//if both are same size, move on to checking for same data
if(list_one.count != list_two.count)
{
return false;
}
else
{
//boolean flag to hold truth of sameness
bool flag = true;
//two pointers to go through
LLNode<THING> *temp_one = list_one.Head;
LLNode<THING> *temp_two = list_two.Head;
while(temp_one != NULL && temp_two != NULL)
{
if(temp_one->data != temp_two->data)
{
flag = false;
break;
}
else
{
temp_one = temp_one->next;
temp_two = temp_two->next;
}
}
return flag;
}
}
These, as you've said, aren't compilation errors: they are Intellisense errors. These errors take a while to refresh in the extension and therefore aren't very indicative most of the time, and it's a known issue that Intellisense isn't great with adding comments, and is even worse when colliding with other extensions.
One way to get rid of the errors is to cut-paste all of the code (just go ctrl+a, ctrl+x, ctrl+v). This forces Intellisense to refresh.
Another way which is a personal favorite of mine is to shut down Intellisense :) you can see how to do that in here.

Having trouble returning class member

I'm making a B inary S earch T ree (BST for short) and I've run into a problem that I can't figure out.
I shall try and reduce the amount of code but it still may require quite a bit I'm afraid.
Nodes:
template <typename Type>
class BSTNode { // Binary Search Tree nodes
private:
int key; // we search by key, no matter what type of data we have
Type data;
BSTNode *left;
BSTNode *right;
public:
BSTNode (int, Type);
bool add (int, Type);
Type search (int);
BSTNode<Type> *remove (int, BSTNode*);
BSTNode<Type> *minNode (int);
};
Root:
template <typename Type>
class BST { // The binary search tree containing nodes
private:
BSTNode<Type> *root; // Has reference to root node
public:
BST ();
bool add (int, Type);
Type search (int);
bool remove (int);
};
I don't know how much code to give since I don't want to exaggerate, if you need more, say so please.
I do both do recursive search and remove
template<typename Type>
BSTNode<Type> *BSTNode<Type>::remove(int removeKey, BSTNode *parent) {
// Here I try to remove nodes
// Depending on the number of children a node has, I remove in different ways
// The error occurs at removing a node with 2 children
// here I look for smallest node greater than current node, replace current node, delete node I replaced WITH
if (this->left != NULL && this->right != NULL){
int *auxKey = &key;
this = this->right->minNode(auxKey); // replace
return this->right->remove(this->key, this); // remove old node
}
}
Here is minNode:
template<typename Type>
Type *BSTNode<Type>::minNode (int oldKey) {
if (this->left == NULL) {
//oldKey = this->key;
return this->data;
} else
return left->minNode();
}
This is where the error occurs:
this = right->minNode(auxKey);
This causes a chain of errors, but I think the main error is:
error: invalid conversion from 'int*' to 'int' [-fpermissive]
I'm guessing it's something simple I've overlooked, but I just can't find it, have been trying for quite some time.
EDIT: Decided for now to simply pass key to minNode() and ignore oldKey and auxKey, modified minNode to return pointer.
New Error, same place
lvalue required as left operand
Your minNode function takes in an int value representing the old key, but you're passing an int* into it in the remove function (specifically, auxKey). Try passing in the value of the old key, not a pointer to it. Alternatively, if you want to update the in parameter to hold the correct value (you seem to be trying to do this), change the parameter to a reference parameter.
Hope this helps!