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

30+ pr #710

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.mirea.practice.s23k0215.task1;

import java.util.Scanner;

public abstract class RoadCount {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();

if (n == 0) {
System.out.println(0);
return;
}

int[][] adjacencyMatrix = new int[n][n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
adjacencyMatrix[i][j] = scanner.nextInt();
}
}

int roadCount = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (adjacencyMatrix[i][j] == 1) {
roadCount++;
}
}
}

System.out.println(roadCount);
}
}
}
// Если захотите посмотреть можете мне в
// коментрии к пул-реквесту написать дам вам ссылку на диск
// где лежит
// решение лабораторной по теории графов на Си на 1000+ строк :)
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package ru.mirea.practice.s23k0215.task1;

public final class BinaryTree {

static class Node {
int data;
Node left;
Node right;

public Node(int data) {
this.data = data;
left = right = null;
}
}

private Node root;

public BinaryTree() {
root = null;
}

public void insert(int data) {
root = insertRec(root, data);
}

private Node insertRec(Node root, int data) {
if (root == null) {
root = new Node(data);
return root;
}

if (data < root.data) {
root.left = insertRec(root.left, data);
} else if (data > root.data) {
root.right = insertRec(root.right, data);
}

return root;
}

public boolean search(int data) {
return searchRec(root, data);
}

private boolean searchRec(Node root, int data) {
if (root == null) {
return false;
}
if (data == root.data) {
return true;
}
if (data < root.data) {
return searchRec(root.left, data);
} else {
return searchRec(root.right, data);
}
}

public void delete(int data) {
root = deleteRec(root, data);
}

private Node deleteRec(Node root, int data) {
if (root == null) {
return root;
}

if (data < root.data) {
root.left = deleteRec(root.left, data);
} else if (data > root.data) {
root.right = deleteRec(root.right, data);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}

root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}

return root;
}

private int minValue(Node root) {
int minValue = root.data;
while (root.left != null) {
root = root.left;
minValue = root.data;
}
return minValue;
}

public void inorder() {
inorderRec(root);
}

private void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}

public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);

System.out.println("Обход дерева в порядке возрастания:");
tree.inorder();

System.out.println("\nПоиск элемента 40: " + tree.search(40));
System.out.println("Поиск элемента 90: " + tree.search(90));

tree.delete(20);
System.out.println("\nОбход дерева после удаления 20:");
tree.inorder();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ru.mirea.practice.s23k0215.task2;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public final class HuffmanCoding {

private HuffmanCoding() {
throw new UnsupportedOperationException("Это утилитный класс, и его нельзя создавать.");
}

public static void buildHuffmanTree(String text) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char ch : text.toCharArray()) {
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
}

PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(
Comparator.comparingInt(a -> a.frequency)
);

for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
priorityQueue.offer(new HuffmanNode(entry.getKey(), entry.getValue()));
}

while (priorityQueue.size() > 1) {
HuffmanNode left = priorityQueue.poll();
HuffmanNode right = priorityQueue.poll();

assert right != null;
HuffmanNode newNode = new HuffmanNode('\0', left.frequency + right.frequency);
newNode.left = left;
newNode.right = right;

priorityQueue.offer(newNode);
}

HuffmanNode root = priorityQueue.poll();

Map<Character, String> huffmanCodeMap = new HashMap<>();
generateHuffmanCodes(root, "", huffmanCodeMap);

System.out.println("Хаффмановские коды:");
for (Map.Entry<Character, String> entry : huffmanCodeMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}

StringBuilder compressedText = new StringBuilder();
for (char ch : text.toCharArray()) {
compressedText.append(huffmanCodeMap.get(ch));
}

System.out.println("Сжатый текст: " + compressedText);
}

private static void generateHuffmanCodes(HuffmanNode root, String code, Map<Character, String> huffmanCodeMap) {
if (root == null) {
return;
}

if (root.left == null && root.right == null) {
huffmanCodeMap.put(root.ch, code);
}

generateHuffmanCodes(root.left, code + "0", huffmanCodeMap);
generateHuffmanCodes(root.right, code + "1", huffmanCodeMap);
}

public static void main(String[] args) {
String text = "Это пример для кодирования Хаффмана";
buildHuffmanTree(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.mirea.practice.s23k0215.task2;

public class HuffmanNode {
char ch;
int frequency;
HuffmanNode left;
HuffmanNode right;

public HuffmanNode(char ch, int frequency) {
this.ch = ch;
this.frequency = frequency;
this.left = this.right = null;
}
}

This file was deleted.

13 changes: 13 additions & 0 deletions students/23K0215/23K0215-p31/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0215</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0215-p31</artifactId>
<description>пр31</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.mirea.practice.s23k0215.task1;

public class Node {
int value;
Node left;
Node middle;
Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.middle = null;
this.right = null;
}
}

Loading
Loading