Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(C++) I need to write a class that can take in a HuffmanNode and use it in a Linked Binary Tree. I have written my

(C++) I need to write a class that can take in a HuffmanNode and use it in a Linked Binary Tree. I have written my own class trying to implement this in "LinkedBinaryTree.hpp", but it seems I don't know how to add new nodes and add child nodes to it. Could you help me add any member functions I may be missing/correct any mistakes I made, and make a main() program that will help me understand this better. My code is below.

// LINKED BINARY TREE FILE --- What I wrote

#ifndef LINKEDBINARYTREE_H_

#define LINKEDBINARYTREE_H_

#include "HuffmanBase.hpp"

#include

//Forward Declerations

template class LinkedBinaryTree;

template class Position;

/**********************************************************************

Tree Node Class

**********************************************************************/

template

class TNode {

private:

E elem; // Element value

TNode *par; // parent

TNode *left; // left child

TNode *right; // right child

//Default Constructor

TNode() : elem(), par(NULL), left(NULL), right(NULL) { }

TNode(E &e) : elem(e), par(NULL), left(NULL), right(NULL) { }

//Provides friends access.

friend class LinkedBinaryTree;

friend class Position;

};

/**********************************************************************

TreeNode Position Class

**********************************************************************/

template

class Position {

private:

//Pointer to the TNode.

TNode *v;

public:

//Constructor

Position(TNode* _v = NULL) : v(_v) { }

//Gets Element

E& operator*() { return v->elem; }

//Gets left child

Position left() const { return Position(v->left); }

//Gets right child

Position right() const { return Position(v->right); }

//Gets Parent

Position parent() const { return Position(v->par); }

//Root of the Tree?

bool isRoot() const { return (v->par == NULL); }

//An external node?

bool isExternal() const { return (v->left == NULL && v->right == NULL); }

//Provides friends access.

friend class LinkedBinaryTree;

typedef typename std::list> PositionList;

};

/**********************************************************************

Linked Binary Tree Class

**********************************************************************/

template

class LinkedBinaryTree {

public:

LinkedBinaryTree(); //Constructor

int size() const; //Number of nodes.

bool empty() const; //Is tree empty?

Position root() const; //Get the root.

std::list> positions() const; //List of nodes.

void addRoot(); //Add root to empty tree.

void addRoot(const E& e); //Add root to empty tree.

void expandExternal(const Position& p); //Expand external node.

Position removeAboveExternal(const Position& p); //Removes p and parent.

protected:

//Utility funtions

void preorder(TNode* v, std::list>& pl);

private:

TNode * _root;

int nodeCounter;

};

#endif // !LINKEDBINARYTREE_H_

template

inline LinkedBinaryTree::LinkedBinaryTree() : _root(NULL), nodeCounter(0) {}

template

inline int LinkedBinaryTree::size() const

{

return nodeCounter;

}

template

inline bool LinkedBinaryTree::empty() const

{

return (size() == 0);

}

template

inline Position LinkedBinaryTree::root() const

{

return Position(_root);

}

template

inline std::list> LinkedBinaryTree::positions() const

{

std::list> pl;

//Preorder Traversal.

preorder(_root, pl);

return std::list>(pl);

}

template

inline void LinkedBinaryTree::addRoot()

{

_root = new TNode;

nodeCounter = 1;

}

template

inline void LinkedBinaryTree::addRoot(const E& e)

{

_root = new TNode;

_root->elem = e;

nodeCounter = 1;

}

template

inline void LinkedBinaryTree::expandExternal(const Position& p)

{

//p's node.

TNode* temp = p.v;

//Add a new left child.

v->left = new TNode;

//Sets v to be it's parent.

v->left->par = v;

//Add a new right child.

v->right = new TNode;

//Sets v to be it's parent.

v->right->par = v;

//Increments nodeCounter by two.

nodeCounter += 2;

}

template

inline Position LinkedBinaryTree::removeAboveExternal(const Position& p)

{

//Gets p's node and parent.

TNode* w = p.v;

TNode* v = w->par;

TNode* sibling = (w == v->left ? v->right : v->left);

//Child of root?

if (v == _root) {

//Make sibling root.

_root = sibling;

sibling->par = NULL;

}

else {

//w's grandparent.

TNode* gPar = v->par;

//Replace parent and sibling.

if (v == gPar->left) {

gPar->left = sibling;

}

else {

gPar->right = sibling;

}

sibling->par = NULL;

}

//Deletes removed TNodes.

delete w;

delete v;

//Decrements nodeCounter.

nodeCounter -= 2;

//Returns Sibling.

return Position(sibling);

}

template

inline void LinkedBinaryTree::preorder(TNode* v, std::list>& pl)

{

//Adds this node.

pl.push_back(Position(v));

//Traverse left subtree

if (v->left != NULL) {

preorder(v->left, pl);

}

if (v->right != NULL) {

preorder(v->right, pl);

}

}

// HUFFMAN NODE HPP

class HuffmanNode { public: HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanNode *r); HuffmanNode(char c, size_t f);

char getCharacter() const; size_t getFrequency() const;

bool isLeaf() const; bool isBranch() const; bool isRoot() const;

class Compare { public: Compare(bool lessThan = true) : lessThan(lessThan) {}; bool operator()(const HuffmanNode &n1, const HuffmanNode &n2) const; bool operator()(const HuffmanNode *n1, const HuffmanNode *n2) const;

private: bool lessThan; };

public: HuffmanNode *parent; HuffmanNode *left; HuffmanNode *right;

private: char character; size_t frequency; };

//HUFFMAN NODE CPP

#include "HuffmanBase.hpp"

HuffmanNode::HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanNode *r) {

character = c;

frequency = f;

parent = p;

left = l;

right = r;

}

HuffmanNode::HuffmanNode(char c = '\0', size_t f = 0) {

character = c;

frequency = f;

parent = nullptr;

left = nullptr;

right = nullptr;

}

char HuffmanNode::getCharacter() const

{

return character;

}

size_t HuffmanNode::getFrequency() const

{

return frequency;

}

bool HuffmanNode::isLeaf() const

{

return (left == nullptr && right == nullptr);

}

bool HuffmanNode::isBranch() const

{

return (left != nullptr || right != nullptr);

};

bool HuffmanNode::isRoot() const

{

return (parent == nullptr);

};

bool HuffmanNode::Compare::operator()(const HuffmanNode &n1, const HuffmanNode &n2) const

{

if (n1.frequency == n2.frequency) {

return lessThan ? n1.character < n2.character : n1.character >= n2.character;

} else {

return lessThan ? n1.frequency < n2.frequency : n1.frequency >= n2.frequency;

}

}

bool HuffmanNode::Compare::operator()(const HuffmanNode *n1, const HuffmanNode *n2) const

{

return operator()(*n1, *n2);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions

Question

What is motivation?

Answered: 1 week ago

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago