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.
Related
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.
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);
}
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.
I am making a tree of n children to store directories of computer. Now, concept is simply make a tree (that would not be a BT of course) and each node will have children as well. Consider the code below then I will explain the problem.
First Consider this:
C/users/DeadCoder/Movies/Batman.
Now In my main.cpp I have this all C, users, DeadCoder, Movies, Batman in a vector and then I send two pairs in insert Func. if root==NULL; it would just insert C. Next time C and users would go. It would find C and then insert users occordingly. Let's now see the code .
template <class T>
struct Node;
template <class T>
class tree
{
Node<T> *root;
public:
tree();
~tree();
int insert(T str, T str1);
Node<T> *getRoot();
Node<T> *search(T item, Node<T> *tempPtr);
};
template <class T>
struct Node{
T n;
Node<T> *sibling;
tree<T> children; // SEE my each node has children.
Node(T N){
this->n = N;
this->sibling = NULL;
}
};
// In .cpp FILE;
// Initilaizer
template <class T>
tree<T>::tree() // Constructor Initialization.
{
root=NULL;
}
// Insert Function.
template <class T>
int tree<T>::insert(T push, T find)
{
Node<T> *rPtr = root;
if (rPtr==NULL){
//ROOT is NULL. C needs to be inserted which is in find.
Node<T> *pusPtr = new Node<T>(find);
root = pushPtr;
root->sibling=NULL;
return 0;
}
else if(rPtr!=NULL){
Node<T> *pushPtr = new Node<T>(push);
Node<T> *temp2 = search(find, root);
Node<T> *temp = temp2->children.getRoot(); // say it LINE_40.
if (temp==NULL){
temp = pushPtr;
temp->sibling=NULL;
return 1;
}
// children are already present.
else if(temp!=NULL){
// You don't need to know code for this part.
}
}//if.
}
// Search Function.
template <class T>
Node<T> *tree<T>::search(T data, treeNode<T>* N)
{
if (N->n==data){ // where n represent directory.
return N; // data found.
}//if....
else{
Node<T> *child = N->children.getRoot();
// This is where i get Segmentation fault,
// because child is ==NULL; but you see in LINE_40 I did insert the child for C.
if(child!=NULL){ // say it line 80.
search(data, child);
}//if...
if(child->sibling!=NULL){
search(data, child->sibling);
}
}
}// search....
PROBLEM: C inserted. Users inserted. Now in search function at Line 80, it comes to find the child for C. and it should be Users as I have inserted it in LINE 40. BUT Instead it says child==NULL. I have been debugging for hours and I don't know why it says so. I hope Everybody gets the problem.
Now I really need to know why it is regarding C child to be NULL, It has to be users. Can anyOne see what is the problem???? HELP !!!!
Line 42 does nothing (I mean it has no side effect). It just puts a value in a temporary variable then leaves.
You probably want your temp to be a reference to the root. Something like: Node<T> *&temp =
Are you sure insert method actually inserted these elements?
It might be helpful to implement postconditions so to verify your methods actually fulfill their contract (design by contract).
This way you'll directly get what is wrong and debugging will be fast or unnecessary in some cases, since you'll get log messages saying "this method was supposed to do this but failed doing it", otherwise you'll look for hours where the problems comes from.
I'm trying to implement a templated priority queue using a heap to process frequencies of pixels in an image. It works fine, but when I try to use it by passing another class as the template argument, it ends up converting the class to a pointer to the class while trying to reheap down or up. Here is the heap specification:
template <typename ItemType>
struct HeapType
{
void ReheapDown(int, int);
void ReheapUp(int, int);
ItemType *elements;
int numElements;
};
reheap down function:
template<typename ItemType>
void HeapType<ItemType>::ReheapDown(int root, int bottom)
{
int maxChild, rightChild, leftChild;
leftChild = 2*root+1;
rightChild = 2*root+2;
if(leftChild <= bottom)
{
if(leftChild == bottom)
{
maxChild = leftChild;
}
else
{
if(elements[leftChild] <= elements[rightChild])
maxChild = rightChild;
else
maxChild = leftChild;
}
if(elements[root] < elements[maxChild])
{
Swap(elements, root, maxChild);
ReheapDown(maxChild, bottom);
}
}
}
and Swap funciton:
template<typename ItemType>
void Swap(ItemType &itemSwap, int swapFrom, int swapTo)
{
ItemType tempItem;
tempItem = itemSwap[swapFrom];
itemSwap[swapFrom] = itemSwap[swapTo];
itemSwap[swapTo] = tempItem;
}
So, I have the Priority queue being implemented using a helper class called Pfreq that overloads the comparison operators so that the heap sorts by frequency of the pixel rather than the value of the pixel. It has no problem until it gets to the Swap function and then complains that it can't convert the type from Pfreq to Pfreq*. I'm not entirely sure how to solve the problem that the template causes the Swap function to be called with type Pfreq*.
I think the issue is in the declaration of this function:
template<typename ItemType>
void Swap(ItemType &itemSwap, int swapFrom, int swapTo)
You're trying to use the first argument as an array, as evidenced here:
ItemType tempItem;
tempItem = itemSwap[swapFrom];
itemSwap[swapFrom] = itemSwap[swapTo];
itemSwap[swapTo] = tempItem;
The problem is that itemSwap is a reference to an ItemType, not an array of ItemTypes or a pointer to an ItemType. Try changing that parameter to be an ItemType* and see if that fixes things.
Hope this helps!