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.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 labb6/src/HuffmanNode.h (limited to 'labb6/src/HuffmanNode.h') diff --git a/labb6/src/HuffmanNode.h b/labb6/src/HuffmanNode.h new file mode 100755 index 0000000..b788c00 --- /dev/null +++ b/labb6/src/HuffmanNode.h @@ -0,0 +1,70 @@ +/* + * TDDD86 Huffman Encoding + * + * This file declares the HuffmanNode structure that you will use in your + * Huffman encoding tree. + * + * Please do not modify this provided file. Your turned-in files should work + * with an unmodified version of all provided code files. + */ + +#ifndef _huffmannode_h +#define _huffmannode_h + +#include +#include +#include +#include +#include +#include "bitstream.h" +using namespace std; + +/* Type: HuffmanNode + * A node inside a Huffman encoding tree. Each node stores four + * values - the character stored here (or NOT_A_CHAR if the value + * is not a character), pointers to the 0 and 1 subtrees, and the + * character count (weight) of the tree. + */ +struct HuffmanNode { + int character; // character being represented by this node + int count; // number of occurrences of that character + HuffmanNode* zero; // 0 (left) subtree (nullptr if empty) + HuffmanNode* one; // 1 (right) subtree (nullptr if empty) + + /* + * Constructs a new node to store the given character and its count, + * along with the given child pointers. + */ + HuffmanNode(int character = NOT_A_CHAR, int count = 0, + HuffmanNode* zero = nullptr, HuffmanNode* one = nullptr); + + /* + * Returns true if this node is a leaf (has NULL children). + */ + bool isLeaf() const; + + /* + * Returns a string representation of this node for debugging. + */ + string toString() const; + + /* + * Returns false if count of this node is strictly less than that of rhs, + * true otherwise. + */ + bool operator <(const HuffmanNode &rhs) const; +}; + +/* + * Prints an indented horizontal view of the tree of HuffmanNodes with the given + * node as its root. + * Can optionally show the memory addresses of each node for debugging. + */ +void printSideways(HuffmanNode* node, bool showAddresses = false, string indent = ""); + +/* + * Stream insertion operator so that a HuffmanNode can be printed for debugging. + */ +ostream& operator <<(ostream& out, const HuffmanNode& node); + +#endif -- cgit v1.2.1