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

Commit 38d14e2

Browse files
committed
123
1 parent 209164c commit 38d14e2

File tree

22 files changed

+466
-522
lines changed

22 files changed

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

students/23K1167/23K1167-p30/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111
<artifactId>23K1167-p30</artifactId>
12-
<description>Массивы</description>
12+
<description>Пр </description>
1313
</project>

students/23K1167/23K1167-p30/src/main/java/ru/mirea/practice/s23k1167/task1/task1/task2/task1/BinaryTree.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

students/23K1167/23K1167-p30/src/main/java/ru/mirea/practice/s23k1167/task1/task1/task2/task1/Node.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

students/23K1167/23K1167-p30/src/main/java/ru/mirea/practice/s23k1167/task1/task1/task2/task2/HuffmanCoding.java

Lines changed: 0 additions & 111 deletions
This file was deleted.

students/23K1167/23K1167-p30/src/main/java/ru/mirea/practice/s23k1167/task1/task1/task2/task2/HuffmanComparator.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

students/23K1167/23K1167-p30/src/main/java/ru/mirea/practice/s23k1167/task1/task1/task2/task2/HuffmanNode.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)