-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.h
executable file
·85 lines (82 loc) · 3.15 KB
/
Huffman.h
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
79
80
81
82
83
84
85
#include <string>
#include <iostream>
#include <fstream>
#ifndef HUFFMAN
#define HUFFMAN
class Huffman
{
private:
/*** Node structure ***/
class BinNode
{
public:
char data;
BinNode * left, *right;
// BinNode constructor
BinNode(char item)
{
data = item;
left = right = 0;
}
};
typedef BinNode * BinNodePointer;
public:
/*** Function members ***/
Huffman();
/*---------------------------------------------------------------------------------
Constructor
Precondition: None.
Postcondition: A one-node binary tree with root node pointed to by myRoot
has been created.
---------------------------------------------------------------------------------*/
void buildDecodingTree(ifstream & codeIn);
/*---------------------------------------------------------------------------------
Build the Huffman decoding tree.
Precondition: ifstream codeIn is open and is connected to a file that contains
characters and their codes.
Postcondition: A oneHuffman decoding tree has been created with root node
pointed to by myRoot.
---------------------------------------------------------------------------------*/
void insert(char ch, string code);
/*---------------------------------------------------------------------------------
Insert a node for a character in Huffman decoding tree.
Precondition: code is the bit string that is the code for ch.
Postcondition: A node containing ch has been inserted into the Huffman tree
with root pointed to by myRoot.
---------------------------------------------------------------------------------*/
void decode(ifstream & messageIn);
/*---------------------------------------------------------------------------------
Build the Huffman decoding tree.
Precondition: ifstream messageIn is open and is connected to a file that
contains the message to be decoded.
Postcondition: The decoded message has been output to cout.
---------------------------------------------------------------------------------*/
void printTree(ostream & out, BinNodePointer root, int indent);
/*---------------------------------------------------------------------------------
Recursive function to display a binary tree with root pointed to by root.
Precondition: ostream out is open; root points to a binary tree; indent >= 0
is the amount to indent each level..
Postcondition: Graphical representation of the binary tree has been output
to out.
---------------------------------------------------------------------------------*/
void displayDecodingTree(ostream & out);
/*---------------------------------------------------------------------------------
Display the decoding tree.
Precondition: ostream out is open.
Postcondition: The decoding tree has been output to out (via printTree().)
---------------------------------------------------------------------------------*/
/*** Data members ***/
private:
BinNodePointer myRoot;
};
//--- Definition of constructor
inline Huffman::Huffman()
{
myRoot = new BinNode('*');
}
//--- Definition of displayDecodingTree()
inline void Huffman::displayDecodingTree(ostream & out)
{
printTree(out, myRoot, 0);
}
#endif