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

Commit 6201815

Browse files
committed
Лабораторные работы 30-32
1 parent 07da61f commit 6201815

File tree

17 files changed

+595
-0
lines changed

17 files changed

+595
-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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p30_1</artifactId>
12+
<description>Деревья</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("30_1 практическая работа!");
11+
}
12+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package ru.mirea.practice.s23k0116.task;
2+
3+
public class BinaryTree {
4+
Node root;
5+
6+
int height(Node node) {
7+
if (node == null) {
8+
return 0;
9+
}
10+
int heighLeft = height(node.left);
11+
int heighRight = height(node.right);
12+
return Math.max(heighLeft, heighRight) + 1;
13+
}
14+
15+
boolean lookup(Node node, int target) {
16+
if (node == null) {
17+
return false;
18+
}
19+
if (node.data == target) {
20+
return true;
21+
}
22+
if (target < node.data) {
23+
return lookup(node.left, target);
24+
} else {
25+
return lookup(node.right, target);
26+
}
27+
}
28+
29+
int getMaxWidth(Node root) {
30+
int h = this.height(this.root);
31+
int maxWidth = 0;
32+
for (int i = 1; i <= h; i++) {
33+
int w = getWidth(this.root, i);
34+
if (w > maxWidth) {
35+
maxWidth = w;
36+
}
37+
}
38+
return maxWidth;
39+
}
40+
41+
int getWidth(Node node, int level) {
42+
if (node == null) {
43+
return 0;
44+
}
45+
if (level == 1) {
46+
return 1;
47+
}
48+
return getWidth(node.left, level - 1) + getWidth(node.right, level - 1);
49+
}
50+
51+
int size(Node node) {
52+
if (node == null) {
53+
return 0;
54+
}
55+
return size(node.left) + 1 + size(node.right);
56+
}
57+
58+
boolean sameTree(Node a, Node b) {
59+
if (a == null && b == null) {
60+
return true;
61+
}
62+
if (a != null && b != null) {
63+
return a.data == b.data
64+
&& sameTree(a.left, b.left)
65+
&& sameTree(a.right, b.right);
66+
}
67+
return false;
68+
}
69+
70+
public static void main(String[] args) {
71+
BinaryTree tree = new BinaryTree();
72+
tree.root = new Node(10);
73+
tree.root.left = new Node(5);
74+
tree.root.right = new Node(15);
75+
tree.root.left.left = new Node(2);
76+
tree.root.left.right = new Node(7);
77+
78+
System.out.println("Высота дерева: " + tree.height(tree.root));
79+
System.out.println("Размер дерева: " + tree.size(tree.root));
80+
System.out.println("Максимальная ширина дерева: " + tree.getMaxWidth(tree.root));
81+
82+
int target1 = 2;
83+
System.out.println("Поиск " + target1 + ": " + tree.lookup(tree.root, target1));
84+
int target2 = 4;
85+
System.out.println("Поиск " + target2 + ": " + tree.lookup(tree.root, target2));
86+
87+
BinaryTree tree2 = new BinaryTree();
88+
tree2.root = new Node(7);
89+
tree2.root.left = new Node(1);
90+
tree2.root.right = new Node(4);
91+
92+
System.out.println("Являются ли эти два дерева одинаковыми? " + tree.sameTree(tree.root, tree2.root));
93+
}
94+
}
95+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s23k0116.task;
2+
3+
class Node {
4+
int data;
5+
Node left;
6+
Node right;
7+
8+
Node(int d) {
9+
data = d;
10+
left = null;
11+
right = null;
12+
}
13+
}
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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p30_2</artifactId>
12+
<description>Деревья</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("30_2 практическая работа!");
11+
}
12+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package ru.mirea.practice.s23k0116.task;
2+
3+
class BinaryTree {
4+
Node root;
5+
int count;
6+
7+
public BinaryTree() {
8+
root = null;
9+
count = 0;
10+
}
11+
12+
public void insert(int item) {
13+
Node newNode = new Node(item);
14+
if (root == null) {
15+
root = newNode;
16+
count++;
17+
return;
18+
}
19+
20+
Node current = root;
21+
Node parent;
22+
23+
while (true) {
24+
parent = current;
25+
if (item < current.data) {
26+
current = current.left;
27+
if (current == null) {
28+
parent.left = newNode;
29+
count++;
30+
return;
31+
}
32+
} else if (item > current.data) {
33+
current = current.right;
34+
if (current == null) {
35+
parent.right = newNode;
36+
count++;
37+
return;
38+
}
39+
} else {
40+
System.out.println("Элемент " + item + " уже существует в дереве.");
41+
return;
42+
}
43+
}
44+
}
45+
46+
public void inorder() {
47+
inorderRec(root);
48+
System.out.println();
49+
}
50+
51+
private void inorderRec(Node node) {
52+
if (node != null) {
53+
inorderRec(node.left);
54+
System.out.print(node.data + " ");
55+
inorderRec(node.right);
56+
}
57+
}
58+
59+
public void delete(int key) {
60+
root = deleteRec(root, key);
61+
}
62+
63+
private Node deleteRec(Node root, int key) {
64+
if (root == null) {
65+
System.out.println("Узел " + key + " не найден.");
66+
return null;
67+
}
68+
69+
if (key < root.data) {
70+
root.left = deleteRec(root.left, key);
71+
} else if (key > root.data) {
72+
root.right = deleteRec(root.right, key);
73+
} else {
74+
if (root.left == null && root.right == null) {
75+
return null;
76+
} else if (root.left == null) {
77+
return root.right;
78+
} else if (root.right == null) {
79+
return root.left;
80+
} else {
81+
Node successor = findMin(root.right);
82+
root.data = successor.data;
83+
root.right = deleteRec(root.right, successor.data);
84+
}
85+
count--;
86+
}
87+
return root;
88+
}
89+
90+
private Node findMin(Node node) {
91+
while (node.left != null) {
92+
node = node.left;
93+
}
94+
return node;
95+
}
96+
97+
public void destroyTree() {
98+
root = null;
99+
count = 0;
100+
}
101+
102+
public static void main(String[] args) {
103+
BinaryTree tree = new BinaryTree();
104+
105+
int[] nodes = {5, 4, 44, 2, 1, 12, 6, 3, 8, 19, 10};
106+
for (int node : nodes) {
107+
tree.insert(node);
108+
}
109+
110+
System.out.println("Содержимое дерева в сортированном порядке:");
111+
tree.inorder();
112+
113+
int keyToDelete = 5;
114+
System.out.println("Удаление узла: " + keyToDelete);
115+
tree.delete(keyToDelete);
116+
117+
System.out.println("Содержимое дерева после удаления узла:");
118+
tree.inorder();
119+
120+
tree.destroyTree();
121+
System.out.println("Полное удаление дерева.");
122+
}
123+
}
124+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s23k0116.task;
2+
3+
class Node {
4+
int data;
5+
Node left;
6+
Node right;
7+
8+
Node(int d) {
9+
data = d;
10+
left = null;
11+
right = null;
12+
}
13+
}

students/23K0116/23K0116-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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p31</artifactId>
12+
<description>Деревья</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("31 практическая работа!");
11+
}
12+
}

0 commit comments

Comments
 (0)