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

Commit cf43aea

Browse files
committed
20+ pr
1 parent f7dd84e commit cf43aea

File tree

13 files changed

+405
-103
lines changed

13 files changed

+405
-103
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ru.mirea.practice.s23k0215.task1;
2+
3+
import java.util.Scanner;
4+
5+
public abstract class RoadCount {
6+
public static void main(String[] args) {
7+
try (Scanner scanner = new Scanner(System.in)) {
8+
int n = scanner.nextInt();
9+
10+
if (n == 0) {
11+
System.out.println(0);
12+
return;
13+
}
14+
15+
int[][] adjacencyMatrix = new int[n][n];
16+
17+
for (int i = 0; i < n; i++) {
18+
for (int j = 0; j < n; j++) {
19+
adjacencyMatrix[i][j] = scanner.nextInt();
20+
}
21+
}
22+
23+
int roadCount = 0;
24+
for (int i = 0; i < n; i++) {
25+
for (int j = i + 1; j < n; j++) {
26+
if (adjacencyMatrix[i][j] == 1) {
27+
roadCount++;
28+
}
29+
}
30+
}
31+
32+
System.out.println(roadCount);
33+
}
34+
}
35+
}
36+
// Если захотите посмотреть можете мне в
37+
// коментрии к пул-реквесту написать дам вам ссылку на диск
38+
// где лежит
39+
// решение лабораторной по теории графов на Си на 1000+ строк :)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package ru.mirea.practice.s23k0215.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+
}

students/23K0215/23K0215-p30/src/main/java/ru/mirea/practice/s23k0215/task1/Main.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

students/23K0215/23K0215-p30/src/main/java/ru/mirea/practice/s23k0215/task1/Triple.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package ru.mirea.practice.s23k0215.task2;
2+
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.PriorityQueue;
7+
8+
public final class HuffmanCoding {
9+
10+
private HuffmanCoding() {
11+
throw new UnsupportedOperationException("Это утилитный класс, и его нельзя создавать.");
12+
}
13+
14+
public static void buildHuffmanTree(String text) {
15+
Map<Character, Integer> frequencyMap = new HashMap<>();
16+
for (char ch : text.toCharArray()) {
17+
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
18+
}
19+
20+
PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(
21+
Comparator.comparingInt(a -> a.frequency)
22+
);
23+
24+
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
25+
priorityQueue.offer(new HuffmanNode(entry.getKey(), entry.getValue()));
26+
}
27+
28+
while (priorityQueue.size() > 1) {
29+
HuffmanNode left = priorityQueue.poll();
30+
HuffmanNode right = priorityQueue.poll();
31+
32+
assert right != null;
33+
HuffmanNode newNode = new HuffmanNode('\0', left.frequency + right.frequency);
34+
newNode.left = left;
35+
newNode.right = right;
36+
37+
priorityQueue.offer(newNode);
38+
}
39+
40+
HuffmanNode root = priorityQueue.poll();
41+
42+
Map<Character, String> huffmanCodeMap = new HashMap<>();
43+
generateHuffmanCodes(root, "", huffmanCodeMap);
44+
45+
System.out.println("Хаффмановские коды:");
46+
for (Map.Entry<Character, String> entry : huffmanCodeMap.entrySet()) {
47+
System.out.println(entry.getKey() + ": " + entry.getValue());
48+
}
49+
50+
StringBuilder compressedText = new StringBuilder();
51+
for (char ch : text.toCharArray()) {
52+
compressedText.append(huffmanCodeMap.get(ch));
53+
}
54+
55+
System.out.println("Сжатый текст: " + compressedText);
56+
}
57+
58+
private static void generateHuffmanCodes(HuffmanNode root, String code, Map<Character, String> huffmanCodeMap) {
59+
if (root == null) {
60+
return;
61+
}
62+
63+
if (root.left == null && root.right == null) {
64+
huffmanCodeMap.put(root.ch, code);
65+
}
66+
67+
generateHuffmanCodes(root.left, code + "0", huffmanCodeMap);
68+
generateHuffmanCodes(root.right, code + "1", huffmanCodeMap);
69+
}
70+
71+
public static void main(String[] args) {
72+
String text = "Это пример для кодирования Хаффмана";
73+
buildHuffmanTree(text);
74+
}
75+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s23k0215.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/23K0215/23K0215-p30/src/main/java/ru/mirea/practice/s23k0215/task2/Triple.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

students/23K0215/23K0215-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>23K0215</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0215-p31</artifactId>
12+
<description>пр31</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.s23k0215.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)