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

Commit f770d1c

Browse files
committed
Лабораторная 30.1
1 parent 1b99913 commit f770d1c

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K1302</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K1302-p30.1</artifactId>
12+
<description>Задание 30.1</description>
13+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.Comparator;
5+
6+
public final class BinaryTree {
7+
8+
private BinaryTree() {
9+
// Пусто
10+
}
11+
12+
static class Node {
13+
int data;
14+
Node left;
15+
Node right;
16+
17+
Node(int data) {
18+
this.data = data;
19+
left = right = null;
20+
}
21+
}
22+
23+
public static int height(Node node) {
24+
if (node == null) {
25+
return 0;
26+
}
27+
return Math.max(height(node.left), height(node.right)) + 1;
28+
}
29+
30+
public static Node mirror(Node node) {
31+
if (node == null) {
32+
return null;
33+
}
34+
Node temp = node.left;
35+
node.left = mirror(node.right);
36+
node.right = mirror(temp);
37+
return node;
38+
}
39+
40+
public static boolean search(Node node, int value) {
41+
if (node == null) {
42+
return false;
43+
}
44+
if (node.data == value) {
45+
return true;
46+
}
47+
return value < node.data ? search(node.left, value) : search(node.right, value);
48+
}
49+
50+
public static void huffmanEncoding(int[] frequencies) {
51+
PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparingInt(n -> n.data));
52+
for (int freq : frequencies) {
53+
queue.add(new Node(freq));
54+
}
55+
while (queue.size() > 1) {
56+
Node left = queue.poll();
57+
Node right = queue.poll();
58+
Node parent = new Node(left.data + right.data);
59+
parent.left = left;
60+
parent.right = right;
61+
queue.add(parent);
62+
}
63+
printHuffmanTree(queue.poll(), "");
64+
}
65+
66+
private static void printHuffmanTree(Node root, String code) {
67+
if (root == null) {
68+
return;
69+
}
70+
if (root.left == null && root.right == null) {
71+
System.out.println("Значение: " + root.data + ", Код: " + code);
72+
}
73+
printHuffmanTree(root.left, code + "0");
74+
printHuffmanTree(root.right, code + "1");
75+
}
76+
77+
public static void main(String[] args) {
78+
Node root = new Node(10);
79+
root.left = new Node(5);
80+
root.right = new Node(15);
81+
root.left.left = new Node(3);
82+
root.left.right = new Node(7);
83+
84+
System.out.println("Высота: " + height(root));
85+
System.out.println("Найти 7: " + search(root, 7));
86+
System.out.println("Найти 20: " + search(root, 20));
87+
88+
mirror(root);
89+
90+
System.out.println("Алгоритм Хаффмана:");
91+
int[] frequencies = {2, 3, 5, 7, 11, 13};
92+
huffmanEncoding(frequencies);
93+
}
94+
}

students/23K1302/pom.xml

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

0 commit comments

Comments
 (0)