-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
140 lines (124 loc) · 3.57 KB
/
Node.java
File metadata and controls
140 lines (124 loc) · 3.57 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
132
133
134
135
136
137
138
139
140
/**
* This class represents a node in the Huffman encoding tree.
*/
public class Node {
private char character;
private int frequency;
private int value;
private Node left;
private Node right;
/**
* Constructs a node with the specified character, frequency, left child, and right child.
*
* @param character The character stored in the node.
* @param frequency The frequency of the character.
* @param left The left child node.
* @param right The right child node.
*/
public Node(char character, int frequency, Node left, Node right) {
this.character = character;
this.frequency = frequency;
this.left = left;
this.right = right;
this.value = 0;
}
/**
* Constructs a leaf node with the specified character and frequency.
*
* @param character The character stored in the node.
* @param frequency The frequency of the character.
*/
public Node(char character, int frequency) {
this.character = character;
this.frequency = frequency;
this.value = 0;
}
/**
* Constructs an internal node with the specified frequency, left child, and right child.
*
* @param frequency The combined frequency of the left and right child nodes.
* @param left The left child node.
* @param right The right child node.
*/
public Node(int frequency, Node left, Node right) {
this.frequency = frequency;
this.left = left;
this.right = right;
this.value = 0;
}
/**
* Returns the frequency of the character.
*
* @return The frequency of the character.
*/
public int getFrequency() {
return this.frequency;
}
/**
* Returns the character stored in the node.
*
* @return The character stored in the node.
*/
public char getCharacter() {
return this.character;
}
/**
* Returns the left child node.
*
* @return The left child node.
*/
public Node getLeft() {
return this.left;
}
/**
* Returns the right child node.
*
* @return The right child node.
*/
public Node getRight() {
return this.right;
}
/**
* Returns the value assigned to the node.
*
* @return The value assigned to the node.
*/
public int getValue() {
return this.value;
}
/**
* Sets the value assigned to the node.
*
* @param value The value to be assigned to the node.
*/
public void setValue(int value) {
this.value = value;
}
/**
* Traverses the Huffman encoding tree in depth-first order to find the value assigned to the specified character.
*
* @param target The character to find the value for.
* @param current The current node being visited.
* @return The value assigned to the character if found, otherwise -1.
*/
public int indepthCourse(char target, Node current) {
if (current == null) {
return -1;
}
if (current.getCharacter() == target) {
return current.getValue();
}
int leftValue = indepthCourse(target, current.getLeft());
if (leftValue != -1) {
return leftValue;
}
int rightValue = indepthCourse(target, current.getRight());
if (rightValue != -1) {
return rightValue;
}
return -1;
}
public boolean isLeaf() {
return left == null && right == null;
}
}