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

Commit f8c4c01

Browse files
committed
lab 29-32 complete
1 parent a4255bd commit f8c4c01

File tree

9 files changed

+279
-0
lines changed

9 files changed

+279
-0
lines changed

students/23K0351/23K0351-p29/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>23K0351</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0351-p29</artifactId>
12+
<description>/</description>
13+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.Scanner;
4+
5+
public abstract class Deykstr {
6+
public static void main(String[] args) {
7+
try (Scanner scanner = new Scanner(System.in)) {
8+
9+
10+
int n = scanner.nextInt();
11+
12+
13+
if (n == 0) {
14+
System.out.println(0);
15+
return;
16+
}
17+
18+
19+
int[][] adjacencyMatrix = new int[n][n];
20+
21+
22+
for (int i = 0; i < n; i++) {
23+
for (int j = 0; j < n; j++) {
24+
adjacencyMatrix[i][j] = scanner.nextInt();
25+
}
26+
}
27+
28+
29+
int roadCount = 0;
30+
for (int i = 0; i < n; i++) {
31+
for (int j = i + 1; j < n; j++) {
32+
if (adjacencyMatrix[i][j] == 1) {
33+
roadCount++;
34+
}
35+
}
36+
}
37+
38+
39+
System.out.println(roadCount);
40+
}
41+
}
42+
}

students/23K0351/23K0351-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>23K0351</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0351-p30</artifactId>
12+
<description>/</description>
13+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
class BinarySearchTree {
4+
Node root;
5+
6+
7+
void insert(int value) {
8+
root = insertRec(root, value);
9+
}
10+
11+
Node insertRec(Node root, int value) {
12+
if (root == null) {
13+
root = new Node(value);
14+
return root;
15+
}
16+
if (value < root.value) {
17+
root.left = insertRec(root.left, value);
18+
} else if (value > root.value) {
19+
root.right = insertRec(root.right, value);
20+
}
21+
return root;
22+
}
23+
24+
25+
void inorder() {
26+
inorderRec(root);
27+
}
28+
29+
void inorderRec(Node root) {
30+
if (root != null) {
31+
inorderRec(root.left);
32+
System.out.print(root.value + " ");
33+
inorderRec(root.right);
34+
}
35+
}
36+
37+
38+
void delete(int value) {
39+
root = deleteRec(root, value);
40+
}
41+
42+
Node deleteRec(Node root, int value) {
43+
if (root == null) {
44+
return root;
45+
}
46+
if (value < root.value) {
47+
root.left = deleteRec(root.left, value);
48+
} else if (value > root.value) {
49+
root.right = deleteRec(root.right, value);
50+
} else {
51+
52+
if (root.left == null) {
53+
return root.right;
54+
} else if (root.right == null) {
55+
return root.left;
56+
}
57+
58+
root.value = minValue(root.right);
59+
60+
root.right = deleteRec(root.right, root.value);
61+
}
62+
return root;
63+
}
64+
65+
int minValue(Node root) {
66+
int minValue = root.value;
67+
while (root.left != null) {
68+
minValue = root.left.value;
69+
root = root.left;
70+
}
71+
return minValue;
72+
}
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
BinarySearchTree bst = new BinarySearchTree();
6+
7+
8+
int[] values = {50, 40, 30, 20, 10, 60, 70, 80, 90, 5};
9+
for (int value : values) {
10+
bst.insert(value);
11+
}
12+
13+
14+
System.out.println("Содержимое дерева (инфиксный обход):");
15+
bst.inorder();
16+
System.out.println();
17+
18+
19+
bst.delete(20);
20+
System.out.println("Содержимое дерева после удаления узла со значением 20:");
21+
bst.inorder();
22+
System.out.println();
23+
24+
25+
bst.root = null;
26+
System.out.println("Дерево полностью удалено.");
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
class Node {
4+
int value;
5+
Node left;
6+
Node right;
7+
8+
public Node(int item) {
9+
value = item;
10+
left = right = null;
11+
}
12+
}

students/23K0351/23K0351-p32/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>23K0351</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0351-p32</artifactId>
12+
<description>/</description>
13+
</project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.Arrays;
4+
5+
public class JonsonTrotter {
6+
private int[] permutation;
7+
private int[] direction;
8+
private int n;
9+
10+
public JonsonTrotter(int n) {
11+
this.n = n;
12+
this.permutation = new int[n];
13+
this.direction = new int[n];
14+
for (int i = 0; i < n; i++) {
15+
permutation[i] = i;
16+
direction[i] = 1;
17+
}
18+
}
19+
20+
public void generatePermutations() {
21+
22+
printPermutation();
23+
24+
while (true) {
25+
int mobileIndex = getMobileIndex();
26+
if (mobileIndex == -1) {
27+
break;
28+
}
29+
30+
moveMobileElement(mobileIndex);
31+
32+
printPermutation();
33+
34+
changeDirection(mobileIndex);
35+
}
36+
}
37+
38+
private void printPermutation() {
39+
System.out.println(Arrays.toString(permutation));
40+
}
41+
42+
private int getMobileIndex() {
43+
int maxMobile = -1;
44+
int mobileIndex = -1;
45+
46+
for (int i = 0; i < n; i++) {
47+
int currentIndex = permutation[i];
48+
int nextIndex = currentIndex + direction[currentIndex];
49+
50+
51+
if (nextIndex >= 0 && nextIndex < n && currentIndex > maxMobile) {
52+
maxMobile = currentIndex;
53+
mobileIndex = i;
54+
}
55+
}
56+
return mobileIndex;
57+
}
58+
59+
private void moveMobileElement(int mobileIndex) {
60+
int mobileValue = permutation[mobileIndex];
61+
int nextIndex = mobileValue + direction[mobileValue];
62+
63+
64+
permutation[mobileIndex] = permutation[nextIndex];
65+
permutation[nextIndex] = mobileValue;
66+
}
67+
68+
private void changeDirection(int mobileIndex) {
69+
int mobileValue = permutation[mobileIndex];
70+
for (int i = 0; i < n; i++) {
71+
if (permutation[i] > mobileValue) {
72+
direction[permutation[i]] *= -1;
73+
}
74+
}
75+
}
76+
77+
public static void main(String[] args) {
78+
int n = 3;
79+
JonsonTrotter jt = new JonsonTrotter(n);
80+
jt.generatePermutations();
81+
}
82+
}

students/23K0351/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<module>23K0351-p26</module>
3636
<module>23K0351-p27</module>
3737
<module>23K0351-p28</module>
38+
<module>23K0351-p29</module>
39+
<module>23K0351-p30</module>
40+
<module>23K0351-p32</module>
3841

3942

4043

0 commit comments

Comments
 (0)