summaryrefslogtreecommitdiffstats
path: root/labb6/src/HuffmanNode.cpp
blob: 4f50054a76177f2be0f3dddc77e23eb974d02ab0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 <cctype>
#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 << "}";
}