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

Practiks 21 30 #712

Merged
merged 17 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<module>students/23K0755</module>
<module>students/23K1292</module>
<module>students/23L0908</module>
<module>students/23K0342</module>
</modules>
<dependencies>
<dependency>
Expand Down
13 changes: 13 additions & 0 deletions students/23K0342/23K0342-p30_2/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>23K0342</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0342-p30_2</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ru.mirea.practice.s0000001;

class BinarySearchTree {
Node root;

public BinarySearchTree() {
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 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) {
minValue = root.left.data;
root = root.left;
}
return minValue;
}

public void printInOrder() {
printInOrderRec(root);
}

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

public void destroy() {
root = destroyRec(root);
}

private Node destroyRec(Node root) {
if (root == null) {
return null;
}
root.left = destroyRec(root.left);
root.right = destroyRec(root.right);
root = null;
return root;
}

public Node findMin() {
return findMinRec(root);
}

private Node findMinRec(Node root) {
if (root == null || root.left == null) {
return root;
}
return findMinRec(root.left);
}

public Node findMax() {
return findMaxRec(root);
}

private Node findMaxRec(Node root) {
if (root == null || root.right == null) {
return root;
}
return findMaxRec(root.right);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.mirea.practice.s0000001;

public abstract class BinarySearchTreeTest {
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();

int[] nodes = {20, 8, 22, 4, 12, 10, 14, 25, 3, 7};
for (int node : nodes) {
tree.insert(node);
}

System.out.println("Содержимое дерева (in-order):");
tree.printInOrder();
System.out.println();

System.out.println("\nУдаление узла 10:");
tree.delete(10);

System.out.println("Содержимое дерева (in-order) после удаления:");
tree.printInOrder();
System.out.println();

System.out.println("\nУдаление дерева:");
tree.destroy();

System.out.println("Содержимое дерева после полного удаления:");
tree.printInOrder();
System.out.println();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.mirea.practice.s0000001;

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

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

13 changes: 13 additions & 0 deletions students/23K0342/23K0342-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>23K0342</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0342-p31</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.mirea.practice.s0000001;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.List;
import java.io.File;

final class FileManager {
private FileManager() {
throw new UnsupportedOperationException("Utility class");
}

public static void loadFromFile(String filename, TwoThreeTree tree) throws IOException {
File file = new File(filename);
if (!file.exists()) {
throw new IOException("Файл не найден: " + filename);
}

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(", ");
Processor processor = new Processor(Integer.parseInt(data[0]), data[1], Double.parseDouble(data[2]),
Integer.parseInt(data[3]), Double.parseDouble(data[4]),
Integer.parseInt(data[5]), Integer.parseInt(data[6]));
tree.insert(processor);
}
}
}

public static void saveToFile(String filename, TwoThreeTree tree) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
List<Processor> processors = tree.toList();
for (Processor processor : processors) {
writer.write(processor.toString());
writer.newLine();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.mirea.practice.s0000001;

import java.io.IOException;
import java.util.Scanner;

public abstract class Main {
private static TwoThreeTree tree = new TwoThreeTree();

public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
try {
FileManager.loadFromFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
System.out.println("Данные загружены.");
} catch (IOException e) {
System.err.println("Ошибка загрузки данных: " + e.getMessage());
}

while (true) {
System.out.print("Введите команду (L, D n, A n, S, E): ");
String command = scanner.nextLine();

if ("L".equalsIgnoreCase(command)) {
tree.printTree();
} else if (command.startsWith("D")) {
try {
int key = Integer.parseInt(command.split(" ")[1]);
tree.delete(key);
System.out.println("Запись с ключом " + key + " удалена.");
} catch (Exception e) {
System.err.println("Ошибка удаления: " + e.getMessage());
}
} else if (command.startsWith("A")) {
try {
int key = Integer.parseInt(command.split(" ")[1]);
System.out.print("Введите данные процессора (название, тактовая частота, кеш, частота шины, SPECint, SPECfp): ");
String[] data = scanner.nextLine().split(", ");
Processor processor = new Processor(key, data[0], Double.parseDouble(data[1]), Integer.parseInt(data[2]),
Double.parseDouble(data[3]), Integer.parseInt(data[4]), Integer.parseInt(data[5]));
tree.insert(processor);
System.out.println("Запись добавлена: " + processor);
} catch (Exception e) {
System.err.println("Ошибка добавления: " + e.getMessage());
}
} else if ("S".equalsIgnoreCase(command)) {
try {
FileManager.saveToFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
System.out.println("Данные сохранены в файл.");
} catch (IOException e) {
System.err.println("Ошибка сохранения данных: " + e.getMessage());
}
} else if ("E".equalsIgnoreCase(command)) {
break;
} else {
System.out.println("Неизвестная команда.");
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mirea.practice.s0000001;

class Processor {
int key;
String name;
double clockSpeed;
int cacheSize;
double busFrequency;
int specInt;
int specFp;

public Processor(int key, String name, double clockSpeed, int cacheSize, double busFrequency, int specInt, int specFp) {
this.key = key;
this.name = name;
this.clockSpeed = clockSpeed;
this.cacheSize = cacheSize;
this.busFrequency = busFrequency;
this.specInt = specInt;
this.specFp = specFp;
}

@Override
public String toString() {
return key + ", " + name + ", " + clockSpeed + ", " + cacheSize + ", " + busFrequency + ", " + specInt + ", " + specFp;
}
}
Loading
Loading