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

Commit 4ec468e

Browse files
authored
Merge pull request #707 from Florist01/main
pr30-32
2 parents f20d1c5 + 38d14e2 commit 4ec468e

File tree

12 files changed

+505
-0
lines changed

12 files changed

+505
-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: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>23K1167</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K1167-p30.part2</artifactId>
12+
<description>Пр </description>
13+
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package ru.mirea.practice.task1.task1;
2+
3+
public class BinaryTree {
4+
5+
static class Node {
6+
int value;
7+
Node left;
8+
Node right;
9+
10+
public Node(int value) {
11+
this.value = value;
12+
left = right = null;
13+
}
14+
}
15+
16+
Node root;
17+
18+
public BinaryTree() {
19+
root = null;
20+
}
21+
22+
public void insert(int value) {
23+
root = insertRec(root, value);
24+
}
25+
26+
private Node insertRec(Node root, int value) {
27+
if (root == null) {
28+
root = new Node(value);
29+
return root;
30+
}
31+
32+
if (value < root.value) {
33+
root.left = insertRec(root.left, value);
34+
} else if (value > root.value) {
35+
root.right = insertRec(root.right, value);
36+
}
37+
38+
return root;
39+
}
40+
41+
public void inorder() {
42+
inorderRec(root);
43+
}
44+
45+
private void inorderRec(Node root) {
46+
if (root != null) {
47+
inorderRec(root.left);
48+
System.out.print(root.value + " ");
49+
inorderRec(root.right);
50+
}
51+
}
52+
53+
public void delete(int value) {
54+
root = deleteRec(root, value);
55+
}
56+
57+
private Node deleteRec(Node root, int value) {
58+
if (root == null) {
59+
return root;
60+
}
61+
62+
if (value < root.value) {
63+
root.left = deleteRec(root.left, value);
64+
} else if (value > root.value) {
65+
root.right = deleteRec(root.right, value);
66+
} else {
67+
if (root.left == null) {
68+
return root.right;
69+
} else if (root.right == null) {
70+
return root.left;
71+
}
72+
73+
root.value = minValue(root.right);
74+
root.right = deleteRec(root.right, root.value);
75+
}
76+
77+
return root;
78+
}
79+
80+
private int minValue(Node root) {
81+
int minValue = root.value;
82+
while (root.left != null) {
83+
minValue = root.left.value;
84+
root = root.left;
85+
}
86+
return minValue;
87+
}
88+
89+
public void deleteTree() {
90+
root = null;
91+
}
92+
93+
public static void main(String[] args) {
94+
BinaryTree tree = new BinaryTree();
95+
96+
int[] values = {50, 30, 20, 40, 70, 60, 80, 10, 25, 65};
97+
for (int value : values) {
98+
tree.insert(value);
99+
}
100+
101+
System.out.println("Дерево до удаления узла:");
102+
tree.inorder();
103+
System.out.println();
104+
105+
tree.delete(20);
106+
107+
System.out.println("Дерево после удаления узла 20:");
108+
tree.inorder();
109+
System.out.println();
110+
111+
tree.deleteTree();
112+
System.out.println("Дерево удалено.");
113+
}
114+
}

students/23K1167/23K1167-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>23K1167</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K1167-p30</artifactId>
12+
<description>Пр </description>
13+
</project>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package ru.mirea.practice.task1.task1;
2+
3+
public final class BinaryTree {
4+
5+
static class Node {
6+
int data;
7+
Node left;
8+
Node right;
9+
10+
public Node(int data) {
11+
this.data = data;
12+
left = right = null;
13+
}
14+
}
15+
16+
private Node root;
17+
18+
public BinaryTree() {
19+
root = null;
20+
}
21+
22+
public void insert(int data) {
23+
root = insertRec(root, data);
24+
}
25+
26+
private Node insertRec(Node root, int data) {
27+
if (root == null) {
28+
root = new Node(data);
29+
return root;
30+
}
31+
32+
if (data < root.data) {
33+
root.left = insertRec(root.left, data);
34+
} else if (data > root.data) {
35+
root.right = insertRec(root.right, data);
36+
}
37+
38+
return root;
39+
}
40+
41+
public boolean search(int data) {
42+
return searchRec(root, data);
43+
}
44+
45+
private boolean searchRec(Node root, int data) {
46+
if (root == null) {
47+
return false;
48+
}
49+
if (data == root.data) {
50+
return true;
51+
}
52+
if (data < root.data) {
53+
return searchRec(root.left, data);
54+
} else {
55+
return searchRec(root.right, data);
56+
}
57+
}
58+
59+
public void delete(int data) {
60+
root = deleteRec(root, data);
61+
}
62+
63+
private Node deleteRec(Node root, int data) {
64+
if (root == null) {
65+
return root;
66+
}
67+
68+
if (data < root.data) {
69+
root.left = deleteRec(root.left, data);
70+
} else if (data > root.data) {
71+
root.right = deleteRec(root.right, data);
72+
} else {
73+
if (root.left == null) {
74+
return root.right;
75+
} else if (root.right == null) {
76+
return root.left;
77+
}
78+
79+
root.data = minValue(root.right);
80+
root.right = deleteRec(root.right, root.data);
81+
}
82+
83+
return root;
84+
}
85+
86+
private int minValue(Node root) {
87+
int minValue = root.data;
88+
while (root.left != null) {
89+
root = root.left;
90+
minValue = root.data;
91+
}
92+
return minValue;
93+
}
94+
95+
public void inorder() {
96+
inorderRec(root);
97+
}
98+
99+
private void inorderRec(Node root) {
100+
if (root != null) {
101+
inorderRec(root.left);
102+
System.out.print(root.data + " ");
103+
inorderRec(root.right);
104+
}
105+
}
106+
107+
public static void main(String[] args) {
108+
BinaryTree tree = new BinaryTree();
109+
110+
tree.insert(50);
111+
tree.insert(30);
112+
tree.insert(20);
113+
tree.insert(40);
114+
tree.insert(70);
115+
tree.insert(60);
116+
tree.insert(80);
117+
118+
System.out.println("Обход дерева в порядке возрастания:");
119+
tree.inorder();
120+
121+
System.out.println("\nПоиск элемента 40: " + tree.search(40));
122+
System.out.println("Поиск элемента 90: " + tree.search(90));
123+
124+
tree.delete(20);
125+
System.out.println("\nОбход дерева после удаления 20:");
126+
tree.inorder();
127+
}
128+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ru.mirea.practice.task1.task2;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public final class HuffmanCoding {
8+
9+
private HuffmanCoding() {
10+
throw new UnsupportedOperationException("Это утилитный класс, и его нельзя создавать.");
11+
}
12+
13+
public static void buildHuffmanTree(String text) {
14+
Map<Character, Integer> frequencyMap = new HashMap<>();
15+
for (char ch : text.toCharArray()) {
16+
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
17+
}
18+
19+
PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(
20+
(a, b) -> a.frequency - b.frequency
21+
);
22+
23+
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
24+
priorityQueue.offer(new HuffmanNode(entry.getKey(), entry.getValue()));
25+
}
26+
27+
while (priorityQueue.size() > 1) {
28+
HuffmanNode left = priorityQueue.poll();
29+
HuffmanNode right = priorityQueue.poll();
30+
31+
HuffmanNode newNode = new HuffmanNode('\0', left.frequency + right.frequency);
32+
newNode.left = left;
33+
newNode.right = right;
34+
35+
priorityQueue.offer(newNode);
36+
}
37+
38+
HuffmanNode root = priorityQueue.poll();
39+
40+
Map<Character, String> huffmanCodeMap = new HashMap<>();
41+
generateHuffmanCodes(root, "", huffmanCodeMap);
42+
43+
System.out.println("Хаффмановские коды:");
44+
for (Map.Entry<Character, String> entry : huffmanCodeMap.entrySet()) {
45+
System.out.println(entry.getKey() + ": " + entry.getValue());
46+
}
47+
48+
StringBuilder compressedText = new StringBuilder();
49+
for (char ch : text.toCharArray()) {
50+
compressedText.append(huffmanCodeMap.get(ch));
51+
}
52+
53+
System.out.println("Сжатый текст: " + compressedText.toString());
54+
}
55+
56+
private static void generateHuffmanCodes(HuffmanNode root, String code, Map<Character, String> huffmanCodeMap) {
57+
if (root == null) {
58+
return;
59+
}
60+
61+
if (root.left == null && root.right == null) {
62+
huffmanCodeMap.put(root.ch, code);
63+
}
64+
65+
generateHuffmanCodes(root.left, code + "0", huffmanCodeMap);
66+
generateHuffmanCodes(root.right, code + "1", huffmanCodeMap);
67+
}
68+
69+
public static void main(String[] args) {
70+
String text = "Это пример для кодирования Хаффмана";
71+
buildHuffmanTree(text);
72+
}
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.task1.task2;
2+
3+
public class HuffmanNode {
4+
char ch;
5+
int frequency;
6+
HuffmanNode left;
7+
HuffmanNode right;
8+
9+
public HuffmanNode(char ch, int frequency) {
10+
this.ch = ch;
11+
this.frequency = frequency;
12+
this.left = this.right = null;
13+
}
14+
}
15+

students/23K1167/23K1167-p31/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>23K1167</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K1167-p31</artifactId>
12+
<description>Пр </description>
13+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.mirea.practice.task1.task1;
2+
3+
public class Node {
4+
int value;
5+
Node left;
6+
Node middle;
7+
Node right;
8+
9+
public Node(int value) {
10+
this.value = value;
11+
this.left = null;
12+
this.middle = null;
13+
this.right = null;
14+
}
15+
}
16+

0 commit comments

Comments
 (0)