forked from aidanvanleuven/cs321-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeObject.java
More file actions
131 lines (113 loc) · 2.09 KB
/
TreeObject.java
File metadata and controls
131 lines (113 loc) · 2.09 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.Serializable;
public class TreeObject {
private long key;
private int frequency;
TreeObject(String sequence){
key = convertCharToNum(sequence.charAt(0));
for(int i = 1; i < sequence.length(); i++) {
key = key * 4 + convertCharToNum(sequence.charAt(i));
}
}
public TreeObject(long key){
this.frequency = 1;
this.key = key;
}
public TreeObject(long key, int frequency)
{
this.frequency = frequency;
this.key = key;
}
public int getFrequency()
{
return frequency;
}
public void setFrequency(int i) {
frequency = i;
}
public void increaseFrequency()
{
this.frequency++;
}
public void copy(TreeObject newValues) {
this.key = newValues.getKey();
this.frequency = newValues.getFrequency();
}
public void empty() {
this.key = 0;
this.frequency = 0;
}
public long getKey() {
return key;
}
public void setKey(long k)
{
this.key = k;
}
public static int convertCharToNum(char c) {
c = Character.toLowerCase(c);
int result;
switch(c) {
case 'a':
result = 0b00;
break;
case 't':
result = 0b11;
break;
case 'c':
result = 0b01;
break;
case 'g':
result = 0b10;
break;
default:
result = -0b10;
System.out.println("Invalid character "+ c);
break;
}
return result;
}
public static long sequenceToLong(String s) {
long k = convertCharToNum(s.charAt(0));
for(int i = 1; i < s.length(); i++) {
k = k * 4 + convertCharToNum(s.charAt(i));
}
return k;
}
public static String longToString(long k) {
String result = "" + longToChar(k);
while(k > 0) {
k/=4;
result = longToChar(k) + result;
}
return result;
}
public static char longToChar(long k) {
long mask = 0b11;
int result;
result = (int) (k & mask);
char r;
switch(result) {
case 0b00:
r = 'a';
break;
case 0b11:
r = 't';
break;
case 0b01:
r = 'c';
break;
case 0b10:
r = 'g';
break;
default:
r = 'z';
System.out.println("Invalid character");
break;
}
return r;
}
@Override
public String toString() {
return longToString(key) + ",F:" + frequency;
}
}