jubilant-funicular
HuffmanNode.cpp
1 #include "nta/Compressor.h"
2 
3 namespace nta {
4  HuffmanNode::HuffmanNode() : m_lChild(nullptr), m_rChild(nullptr), m_freq(0) {
5  }
6  HuffmanNode::HuffmanNode(HuffmanNode* l, HuffmanNode* r) : m_lChild(l), m_rChild(r), m_freq(l->m_freq+r->m_freq) {
7  }
9  m_freq = 0;
10  if (m_lChild) {
11  delete m_lChild;
12  }
13  if (m_rChild) {
14  delete m_rChild;
15  }
16  }
17  std::map<GLubyte,std::string> HuffmanNode::getEncodings(crstring enc) const {
18  std::map<GLubyte,std::string> ret;
19  if (!hasChildren()) {
20  ret.insert(std::make_pair(((HuffmanLeaf*)this)->getData(), enc));
21  } else {
22  std::map<GLubyte,std::string> lEncode = m_lChild->getEncodings(enc+"0"), rEncode = m_rChild->getEncodings(enc+"1");
23  ret.insert(lEncode.begin(), lEncode.end());
24  ret.insert(rEncode.begin(), rEncode.end());
25  }
26  return ret;
27  }
29  return m_lChild;
30  }
31  HuffmanNode* HuffmanNode::getRight() const {
32  return m_rChild;
33  }
34  bool HuffmanNode::hasChildren() const {
35  return (m_lChild != nullptr || m_rChild != nullptr);
36  }
38  return m_freq;
39  }
40 }
nta::HuffmanNode
A node in a Huffman tree.
Definition: Compressor.h:13
nta::HuffmanNode::getEncodings
auto getEncodings(crstring enc="") const -> std::map< GLubyte, std::string >
returns map of all the bytes and how they are encoded
Definition: HuffmanNode.cpp:17
nta::HuffmanNode::HuffmanNode
HuffmanNode()
basic constructor
Definition: HuffmanNode.cpp:4
nta::HuffmanNode::hasChildren
bool hasChildren() const
returns whether or not the node has children
Definition: HuffmanNode.cpp:34
nta::HuffmanNode::~HuffmanNode
virtual ~HuffmanNode()
recursively destroys node
Definition: HuffmanNode.cpp:8
nta
Definition: Animation2D.h:6
nta::HuffmanNode::getFrequency
int getFrequency() const
returns the frequency of the node
Definition: HuffmanNode.cpp:37
nta::HuffmanLeaf
represents a leaf in a Huffman tree
Definition: Compressor.h:38
nta::HuffmanNode::getLeft
HuffmanNode * getLeft() const
returns children
Definition: HuffmanNode.cpp:28
nta::HuffmanNode::m_lChild
HuffmanNode * m_lChild
the children of the node
Definition: Compressor.h:16
nta::HuffmanNode::m_freq
int m_freq
the frequency of the nodes associated bytes
Definition: Compressor.h:19