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

Commit 926abff

Browse files
author
juliavlv
committed
Практические 21-31
1 parent 157ef94 commit 926abff

File tree

12 files changed

+578
-0
lines changed

12 files changed

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

students/23K0342/23K0342-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>23K0342</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0342-p31</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.io.IOException;
4+
import java.io.BufferedReader;
5+
import java.io.FileReader;
6+
import java.io.BufferedWriter;
7+
import java.io.FileWriter;
8+
import java.util.List;
9+
import java.io.File;
10+
11+
final class FileManager {
12+
private FileManager() {
13+
throw new UnsupportedOperationException("Utility class");
14+
}
15+
16+
public static void loadFromFile(String filename, TwoThreeTree tree) throws IOException {
17+
File file = new File(filename);
18+
if (!file.exists()) {
19+
throw new IOException("Файл не найден: " + filename);
20+
}
21+
22+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
23+
String line;
24+
while ((line = reader.readLine()) != null) {
25+
String[] data = line.split(", ");
26+
Processor processor = new Processor(Integer.parseInt(data[0]), data[1], Double.parseDouble(data[2]),
27+
Integer.parseInt(data[3]), Double.parseDouble(data[4]),
28+
Integer.parseInt(data[5]), Integer.parseInt(data[6]));
29+
tree.insert(processor);
30+
}
31+
}
32+
}
33+
34+
public static void saveToFile(String filename, TwoThreeTree tree) throws IOException {
35+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
36+
List<Processor> processors = tree.toList();
37+
for (Processor processor : processors) {
38+
writer.write(processor.toString());
39+
writer.newLine();
40+
}
41+
}
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public abstract class Main {
7+
private static TwoThreeTree tree = new TwoThreeTree();
8+
9+
public static void main(String[] args) {
10+
try (Scanner scanner = new Scanner(System.in)) {
11+
try {
12+
FileManager.loadFromFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
13+
System.out.println("Данные загружены.");
14+
} catch (IOException e) {
15+
System.err.println("Ошибка загрузки данных: " + e.getMessage());
16+
}
17+
18+
while (true) {
19+
System.out.print("Введите команду (L, D n, A n, S, E): ");
20+
String command = scanner.nextLine();
21+
22+
if ("L".equalsIgnoreCase(command)) {
23+
tree.printTree();
24+
} else if (command.startsWith("D")) {
25+
try {
26+
int key = Integer.parseInt(command.split(" ")[1]);
27+
tree.delete(key);
28+
System.out.println("Запись с ключом " + key + " удалена.");
29+
} catch (Exception e) {
30+
System.err.println("Ошибка удаления: " + e.getMessage());
31+
}
32+
} else if (command.startsWith("A")) {
33+
try {
34+
int key = Integer.parseInt(command.split(" ")[1]);
35+
System.out.print("Введите данные процессора (название, тактовая частота, кеш, частота шины, SPECint, SPECfp): ");
36+
String[] data = scanner.nextLine().split(", ");
37+
Processor processor = new Processor(key, data[0], Double.parseDouble(data[1]), Integer.parseInt(data[2]),
38+
Double.parseDouble(data[3]), Integer.parseInt(data[4]), Integer.parseInt(data[5]));
39+
tree.insert(processor);
40+
System.out.println("Запись добавлена: " + processor);
41+
} catch (Exception e) {
42+
System.err.println("Ошибка добавления: " + e.getMessage());
43+
}
44+
} else if ("S".equalsIgnoreCase(command)) {
45+
try {
46+
FileManager.saveToFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
47+
System.out.println("Данные сохранены в файл.");
48+
} catch (IOException e) {
49+
System.err.println("Ошибка сохранения данных: " + e.getMessage());
50+
}
51+
} else if ("E".equalsIgnoreCase(command)) {
52+
break;
53+
} else {
54+
System.out.println("Неизвестная команда.");
55+
}
56+
}
57+
}
58+
}
59+
}
60+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
class Processor {
4+
int key;
5+
String name;
6+
double clockSpeed;
7+
int cacheSize;
8+
double busFrequency;
9+
int specInt;
10+
int specFp;
11+
12+
public Processor(int key, String name, double clockSpeed, int cacheSize, double busFrequency, int specInt, int specFp) {
13+
this.key = key;
14+
this.name = name;
15+
this.clockSpeed = clockSpeed;
16+
this.cacheSize = cacheSize;
17+
this.busFrequency = busFrequency;
18+
this.specInt = specInt;
19+
this.specFp = specFp;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return key + ", " + name + ", " + clockSpeed + ", " + cacheSize + ", " + busFrequency + ", " + specInt + ", " + specFp;
25+
}
26+
}

0 commit comments

Comments
 (0)