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

Commit 9d1599c

Browse files
authored
Merge pull request #604 from Eliizava/features/lab30-31
Лабораторная №30 части 1 и 2
2 parents f6d43eb + 7e8e2fb commit 9d1599c

File tree

15 files changed

+401
-0
lines changed

15 files changed

+401
-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>23K0143</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0143-p30_1</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public class BinaryTree {
4+
Node root;
5+
6+
int lookup(Node node, int target) {
7+
if (node == null) {
8+
return 0;
9+
}
10+
if (target == node.data) {
11+
return 1;
12+
}
13+
return target < node.data ? lookup(node.left, target) : lookup(node.right, target);
14+
}
15+
16+
int size(Node node) {
17+
if (node == null) {
18+
return 0;
19+
}
20+
return size(node.left) + 1 + size(node.right);
21+
}
22+
23+
void reverse(Node p) {
24+
if (p != null) {
25+
Node temp = p.left;
26+
p.left = p.right;
27+
p.right = temp;
28+
29+
reverse(p.left);
30+
reverse(p.right);
31+
}
32+
}
33+
34+
int getMaxWidth(Node root) {
35+
int maxWdth = 0;
36+
int h = height(root);
37+
for (int i = 1; i <= h; i++) {
38+
int width = getWidth(root, i);
39+
if (width > maxWdth) {
40+
maxWdth = width;
41+
}
42+
}
43+
return maxWdth;
44+
}
45+
46+
int height(Node node) {
47+
if (node == null) {
48+
return 0;
49+
}
50+
return Math.max(height(node.left), height(node.right)) + 1;
51+
}
52+
53+
int getWidth(Node node, int level) {
54+
if (node == null) {
55+
return 0;
56+
}
57+
if (level == 1) {
58+
return 1;
59+
}
60+
return getWidth(node.left, level - 1) + getWidth(node.right, level - 1);
61+
}
62+
63+
boolean sameTree(Node a, Node b) {
64+
if (a == null && b == null) {
65+
return true;
66+
}
67+
if (a == null || b == null) {
68+
return false;
69+
}
70+
return a.data == b.data && sameTree(a.left, b.left) && sameTree(a.right, b.right);
71+
}
72+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
public class Huffman {
7+
public void printCodes(HuffmanNode root, String s) {
8+
if (root.left == null && root.right == null) {
9+
System.out.println(root.data + ": " + s);
10+
return;
11+
}
12+
printCodes(root.left, s + "0");
13+
printCodes(root.right, s + "1");
14+
}
15+
16+
public void huffmanAlgorithm(char[] charArray, int[] charFreq) {
17+
PriorityQueue<HuffmanNode> q = new PriorityQueue<>(charArray.length, Comparator.comparingInt(node -> node.freq));
18+
19+
for (int i = 0; i < charArray.length; i++) {
20+
q.add(new HuffmanNode(charArray[i], charFreq[i]));
21+
}
22+
23+
HuffmanNode root = null;
24+
25+
while (q.size() > 1) {
26+
HuffmanNode left = q.poll();
27+
HuffmanNode right = q.poll();
28+
29+
HuffmanNode f = new HuffmanNode('$', left.freq + right.freq);
30+
f.left = left;
31+
f.right = right;
32+
root = f;
33+
34+
q.add(f);
35+
}
36+
37+
printCodes(root, "");
38+
}
39+
}
40+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public class HuffmanNode {
4+
int freq;
5+
char data;
6+
HuffmanNode left;
7+
HuffmanNode right;
8+
9+
HuffmanNode(char data, int freq) {
10+
left = right = null;
11+
this.data = data;
12+
this.freq = freq;
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("первая практическая работа!");
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public 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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public abstract class TestBinaryTree {
4+
public static void main(String[] args) {
5+
6+
//Задание 1
7+
BinaryTree tree1 = new BinaryTree();
8+
tree1.root = new Node(10);
9+
tree1.root.left = new Node(5);
10+
tree1.root.right = new Node(20);
11+
12+
BinaryTree tree2 = new BinaryTree();
13+
tree2.root = new Node(10);
14+
tree2.root.left = new Node(5);
15+
tree2.root.right = new Node(20);
16+
17+
boolean areSame = tree1.sameTree(tree1.root, tree2.root);
18+
System.out.println("Проверка на одинаковые деревья: " + areSame);
19+
20+
// Задание 2
21+
Huffman huffmanCode = new Huffman();
22+
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
23+
int[] charFreq = { 12, 4, 15, 8, 25 };
24+
huffmanCode.huffmanAlgorithm(charArray, charFreq);
25+
}
26+
}
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>23K0143</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0143-p30_2</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
class BinaryTree2 {
4+
Node root;
5+
int count;
6+
7+
public BinaryTree2() {
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 = null;
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+
} else {
39+
return;
40+
}
41+
} else {
42+
return;
43+
}
44+
}
45+
}
46+
47+
public void inorder() {
48+
inorderRec(root);
49+
System.out.println();
50+
}
51+
52+
private void inorderRec(Node node) {
53+
if (node != null) {
54+
inorderRec(node.left);
55+
System.out.print(node.data + " ");
56+
inorderRec(node.right);
57+
}
58+
}
59+
60+
public boolean delete(int key) {
61+
Node current = root;
62+
Node parent = null;
63+
64+
while (current != null && current.data != key) {
65+
parent = current;
66+
if (key < current.data) {
67+
current = current.left;
68+
} else {
69+
current = current.right;
70+
}
71+
}
72+
73+
if (current == null) {
74+
return false;
75+
}
76+
77+
if (current.left == null && current.right == null) {
78+
if (current == root) {
79+
root = null;
80+
} else if (parent.left == current) {
81+
parent.left = null;
82+
} else {
83+
parent.right = null;
84+
}
85+
} else if (current.left == null || current.right == null) {
86+
Node child = (current.left != null) ? current.left : current.right;
87+
if (current == root) {
88+
root = child;
89+
} else if (parent.left == current) {
90+
parent.left = child;
91+
} else {
92+
parent.right = child;
93+
}
94+
} else {
95+
Node successor = findMin(current.right);
96+
int val = successor.data;
97+
delete(successor.data);
98+
current.data = val;
99+
}
100+
101+
count--;
102+
return true;
103+
}
104+
105+
private Node findMin(Node node) {
106+
while (node.left != null) {
107+
node = node.left;
108+
}
109+
return node;
110+
}
111+
112+
public void destroyTree() {
113+
root = null;
114+
count = 0;
115+
}
116+
}
117+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0143;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("первая практическая работа!");
11+
}
12+
}

0 commit comments

Comments
 (0)