Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit c7fa910

Browse files
committed
Лабораторная №30
1 parent 79d1608 commit c7fa910

File tree

6 files changed

+296
-0
lines changed

6 files changed

+296
-0
lines changed

students/23K0120/23K0120-p30/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0120</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0120-p30</artifactId>
12+
<description>Бинарные деревья</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0120;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать девятая практическая работа!");
11+
}
12+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public class BinaryTree<E> {
4+
private E data;
5+
private BinaryTree<E> left;
6+
private BinaryTree<E> right;
7+
8+
public BinaryTree() {
9+
left = null;
10+
right = null;
11+
data = null;
12+
}
13+
14+
public BinaryTree(E data) {
15+
left = null;
16+
right = null;
17+
this.data = data;
18+
}
19+
20+
public static <E> int height(BinaryTree<E> tree) {
21+
int h1 = 0;
22+
int h2 = 0;
23+
if (tree == null) {
24+
return 0;
25+
}
26+
if (tree.getLeft() != null) {
27+
h1 = height(tree.getLeft());
28+
}
29+
if (tree.getRight() != null) {
30+
h2 = height(tree.getRight());
31+
}
32+
return Math.max(h1, h2) + 1;
33+
}
34+
35+
public int height() {
36+
return height(this);
37+
}
38+
39+
public static <E> void reverse(BinaryTree<E> tree) {
40+
BinaryTree<E> treeTemp;
41+
if (tree != null) {
42+
if (tree.getRight() != null && tree.getLeft() != null) {
43+
treeTemp = tree.getLeft();
44+
tree.setLeft(tree.getRight());
45+
tree.setRight(treeTemp);
46+
reverse(tree.getRight());
47+
reverse(tree.getLeft());
48+
} else if (tree.getRight() == null && tree.getLeft() != null) {
49+
reverse(tree.getLeft());
50+
} else if (tree.getRight() != null && tree.getLeft() == null) {
51+
reverse(tree.getRight());
52+
}
53+
}
54+
}
55+
56+
public void reverse() {
57+
reverse(this);
58+
}
59+
60+
public static <E> boolean lookup(BinaryTree<E> tree, Comparable<E> data) {
61+
if (tree == null) {
62+
return false;
63+
} else {
64+
if (data.compareTo(tree.getData()) == 0) {
65+
return true;
66+
} else {
67+
if (data.compareTo(tree.getData()) < 0) {
68+
return lookup(tree.getLeft(), data);
69+
} else {
70+
return lookup(tree.getLeft(), data);
71+
}
72+
}
73+
}
74+
}
75+
76+
public boolean lookup(Comparable<E> data) {
77+
return lookup(this, data);
78+
}
79+
80+
public static <E> int getMaxWidth(BinaryTree<E> tree) {
81+
int maxWidth = 0;
82+
int width = 0;
83+
int height = height(tree);
84+
for (int i = 0; i < height; i++) {
85+
width = getWidth(tree, i);
86+
if (width > maxWidth) {
87+
maxWidth = width;
88+
}
89+
}
90+
return maxWidth;
91+
}
92+
93+
public static <E> int getWidth(BinaryTree<E> tree, int level) {
94+
if (tree == null) {
95+
return 0;
96+
}
97+
if (level == 1) {
98+
return 1;
99+
}
100+
return getWidth(tree.getLeft(), level - 1) + getWidth(tree.getRight(), level - 1);
101+
}
102+
103+
public static <E> int size(BinaryTree<E> tree) {
104+
if (tree == null) {
105+
return 0;
106+
}
107+
return size(tree.getLeft()) + 1 + size(tree.getRight());
108+
}
109+
110+
public int size() {
111+
return size(this);
112+
}
113+
114+
public static <E> boolean isEqual(BinaryTree<E> treeA, BinaryTree<E> treeB) {
115+
if (treeA == null && treeB == null) {
116+
return true;
117+
} else if (treeA != null && treeB != null) {
118+
return treeA.getData().equals(treeB.getData())
119+
&& isEqual(treeA.getLeft(), treeB.getLeft())
120+
&& isEqual(treeA.getRight(), treeB.getRight());
121+
}
122+
return false;
123+
}
124+
125+
public E getData() {
126+
return data;
127+
}
128+
129+
public void setData(E data) {
130+
this.data = data;
131+
}
132+
133+
public BinaryTree<E> getLeft() {
134+
return left;
135+
}
136+
137+
public void setLeft(BinaryTree<E> left) {
138+
this.left = left;
139+
}
140+
141+
public BinaryTree<E> getRight() {
142+
return right;
143+
}
144+
145+
public void setRight(BinaryTree<E> right) {
146+
this.right = right;
147+
}
148+
149+
public void addRight(E data) {
150+
this.right = new BinaryTree<>(data);
151+
}
152+
153+
public void addLeft(E data) {
154+
this.left = new BinaryTree<>(data);
155+
}
156+
157+
@Override
158+
public String toString() {
159+
String string = "";
160+
string += "Tree: {";
161+
if (this.getData() != null) {
162+
string += this.getData().toString();
163+
}
164+
if (this.left != null) {
165+
string += this.left.toString();
166+
}
167+
if (this.right != null) {
168+
string += this.right.toString();
169+
}
170+
string += "} ";
171+
return string;
172+
}
173+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mirea.practice.s23k0120.task2;
2+
3+
public class DataFreq<E> implements Comparable<E> {
4+
private final E data;
5+
private final double frequency;
6+
7+
public DataFreq(E data, double frequency) {
8+
this.data = data;
9+
this.frequency = frequency;
10+
}
11+
12+
public E getData() {
13+
return data;
14+
}
15+
16+
public double getFrequency() {
17+
return frequency;
18+
}
19+
20+
@Override
21+
public int compareTo(Object o) {
22+
return Double.compare(frequency, ((DataFreq<?>) o).getFrequency());
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Data:{" + data + "; Frequency: " + frequency + "}";
28+
}
29+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.mirea.practice.s23k0120.task2;
2+
3+
import ru.mirea.practice.s23k0120.task1.BinaryTree;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.PriorityQueue;
10+
11+
public abstract class Huffman {
12+
// This is the worst code I've ever written
13+
// but I don't have time to rewrite it
14+
15+
public static <E> BinaryTree<DataFreq<?>> huffman(List<DataFreq<E>> elements) {
16+
PriorityQueue<DataFreq<?>> queue = new PriorityQueue<>();
17+
for (DataFreq<E> element : elements) {
18+
queue.add(new DataFreq<>(new BinaryTree(element.getData()), element.getFrequency()));
19+
}
20+
while (queue.size() != 1) {
21+
DataFreq<?> el1 = queue.poll();
22+
DataFreq<?> el2 = queue.poll();
23+
BinaryTree<DataFreq<?>> tempTree = new BinaryTree<>();
24+
if (el1.compareTo(el2) <= 0) {
25+
tempTree.addLeft(el1);
26+
tempTree.addRight(el2);
27+
} else {
28+
tempTree.addLeft(el2);
29+
tempTree.addRight(el1);
30+
}
31+
queue.add(new DataFreq<>(tempTree, el1.getFrequency() + el2.getFrequency()));
32+
}
33+
return new BinaryTree<>(queue.poll());
34+
}
35+
36+
public static <E> void makeHuffmanCodesMap(String code, Map<E, String> map, BinaryTree<?> tree) {
37+
if (tree.getData() instanceof DataFreq) {
38+
tree = (BinaryTree<?>) ((DataFreq) tree.getData()).getData();
39+
}
40+
if (tree.getLeft() != null) {
41+
makeHuffmanCodesMap(code + "0", map, tree.getLeft());
42+
}
43+
if (tree.getLeft() != null) {
44+
makeHuffmanCodesMap(code + "1", map, tree.getRight());
45+
}
46+
if (tree.getLeft() == null && tree.getRight() == null) {
47+
map.put((E) tree.getData(), code);
48+
}
49+
}
50+
51+
public static <E> void makeHuffmanCodesMap(Map<E, String> map, BinaryTree<?> tree) {
52+
makeHuffmanCodesMap("", map, tree);
53+
}
54+
55+
public static void main(String[] args) {
56+
List<DataFreq<Character>> charFreq = new ArrayList<>();
57+
charFreq.add(new DataFreq<>('a', 5));
58+
charFreq.add(new DataFreq<>('b', 9));
59+
charFreq.add(new DataFreq<>('c', 12));
60+
charFreq.add(new DataFreq<>('d', 13));
61+
charFreq.add(new DataFreq<>('e', 16));
62+
charFreq.add(new DataFreq<>('f', 45));
63+
BinaryTree<?> tree = huffman(charFreq);
64+
HashMap<Character, String> map = new HashMap<>();
65+
makeHuffmanCodesMap(map, tree);
66+
System.out.println(map);
67+
}
68+
}

students/23K0120/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
<module>23K0120-p27</module>
4444
<module>23K0120-p28</module>
4545
<module>23K0120-p29</module>
46+
<module>23K0120-p30</module>
4647
</modules>
4748
</project>

0 commit comments

Comments
 (0)