From 93c6b29368d1e0487937b433bc6e678da0058055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 1 Dec 2020 15:25:23 +0100 Subject: given code l6 --- labb6/src/HuffmanNode.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 labb6/src/HuffmanNode.cpp (limited to 'labb6/src/HuffmanNode.cpp') diff --git a/labb6/src/HuffmanNode.cpp b/labb6/src/HuffmanNode.cpp new file mode 100755 index 0000000..4f50054 --- /dev/null +++ b/labb6/src/HuffmanNode.cpp @@ -0,0 +1,78 @@ +/* + * TDDD86 Huffman Encoding + * This file implements the members of the HuffmanNode structure that you will + * use in your Huffman encoding tree. See HuffmanNode.h for documentation of + * each member. + * + * Please do not modify this provided file. Your turned-in files should work + * with an unmodified version of all provided code files. + */ + +#include +#include "HuffmanNode.h" +#include "huffmanutil.h" + +static void printHuffmanNode(ostream& out, const HuffmanNode& node, bool showAddress = false); + +HuffmanNode::HuffmanNode(int character, int count, HuffmanNode* zero, HuffmanNode* one) { + this->character = character; + this->count = count; + this->zero = zero; + this->one = one; +} + +bool HuffmanNode::isLeaf() const { + return zero == nullptr && one == nullptr; +} + +string HuffmanNode::toString() const { + ostringstream out; + out << *this; + return out.str(); +} + +bool HuffmanNode::operator <(const HuffmanNode &rhs) const { + return this->count > rhs.count; +} + +void printSideways(HuffmanNode* node, bool showAddresses, string indent) { + if (node != nullptr) { + printSideways(node->one, showAddresses, indent + " "); + cout << indent << *node << endl; + printSideways(node->zero, showAddresses, indent + " "); + } +} + +ostream& operator <<(ostream& out, const HuffmanNode& node) { + printHuffmanNode(out, node, false); + return out; +} + +static void printHuffmanNode(ostream& out, const HuffmanNode& node, bool showAddress) { + if (showAddress) { + out << "@" << &node; + } + out << "{"; + + if (node.character == NOT_A_CHAR) { + out << "NOT, "; + } else { + out << toPrintableChar(node.character) + << " (" << node.character << "), "; + } + out << "count=" << node.count; + + if (showAddress) { + if (node.zero == nullptr) { + out << ", zero=nullptr"; + } else { + out << ", zero=" << node.zero; + } + if (node.one == nullptr) { + out << ", one=nullptr"; + } else { + out << ", one=" << node.one; + } + } + out << "}"; +} -- cgit v1.2.1