-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeTree.java
More file actions
85 lines (63 loc) · 2.23 KB
/
DecodeTree.java
File metadata and controls
85 lines (63 loc) · 2.23 KB
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
package com.huffman;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
class DecodeTree{
static DecodeTreeNode root=new DecodeTreeNode();
static String decodeFile="decoded.txt";
public static void addToDecodeTree(String data,String seq){
DecodeTreeNode temp = root;
int i = 0;
for(i=0;i<seq.length()-1;i++){
if(seq.charAt(i)=='0'){
if(temp.left == null){
temp.left = new DecodeTreeNode();
temp = temp.left;
}
else{
temp = (DecodeTreeNode) temp.left;
}
}
else
if(seq.charAt(i)=='1'){
if(temp.right == null){
temp.right = new DecodeTreeNode();
temp = temp.right;
}
else{
temp = (DecodeTreeNode) temp.right;
}
}}
if(seq.charAt(i)=='0'){
temp.left = new DecodeTreeNode(data);
//System.out.println("setting data"+ data);
}
else{
temp.right = new DecodeTreeNode(data);
//System.out.println("setting data"+ data);
}
}
public static void decodeMessage(String encoding) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(decodeFile));
DecodeTreeNode temp = root;
for(int i = 0;i<encoding.length();i++){
//System.out.println(encoding.charAt(i));
if(encoding.charAt(i) == '0'){
temp = temp.left;
if(temp.left == null && temp.right == null){
bw.write(temp.getData()+"\n");
temp = root;
}
}
else
{
temp = temp.right;
if(temp.left == null && temp.right == null){
bw.write(temp.getData()+"\n");
temp = root;
}
}
}
bw.close();
}
}