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

Commit c0841a4

Browse files
authored
Merge pull request #680 from EgorBurov246/features/lab32-popytka
Лабораторная работа номер 32
2 parents 84593ed + eeaac01 commit c0841a4

File tree

11 files changed

+405
-0
lines changed

11 files changed

+405
-0
lines changed

students/23K0815/23K0815-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>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p30</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
class SearchTree {
4+
private TreeNode root;
5+
6+
public SearchTree() {
7+
root = null;
8+
}
9+
10+
public void insert(int key) {
11+
root = insertNode(root, key);
12+
}
13+
14+
private TreeNode insertNode(TreeNode current, int key) {
15+
if (current == null) {
16+
current = new TreeNode(key);
17+
return current;
18+
}
19+
if (key < current.value) {
20+
current.leftChild = insertNode(current.leftChild, key);
21+
} else if (key > current.value) {
22+
current.rightChild = insertNode(current.rightChild, key);
23+
}
24+
return current;
25+
}
26+
27+
public void printTree() {
28+
inOrderTraversal(root);
29+
System.out.println();
30+
}
31+
32+
private void inOrderTraversal(TreeNode current) {
33+
if (current != null) {
34+
inOrderTraversal(current.leftChild);
35+
System.out.print(current.value + " ");
36+
inOrderTraversal(current.rightChild);
37+
}
38+
}
39+
40+
public void delete(int key) {
41+
root = deleteNode(root, key);
42+
}
43+
44+
private TreeNode deleteNode(TreeNode current, int key) {
45+
if (current == null) {
46+
return current;
47+
}
48+
49+
if (key < current.value) {
50+
current.leftChild = deleteNode(current.leftChild, key);
51+
} else if (key > current.value) {
52+
current.rightChild = deleteNode(current.rightChild, key);
53+
} else {
54+
if (current.leftChild == null) {
55+
return current.rightChild;
56+
} else if (current.rightChild == null) {
57+
return current.leftChild;
58+
}
59+
60+
current.value = getMinValue(current.rightChild);
61+
current.rightChild = deleteNode(current.rightChild, current.value);
62+
}
63+
return current;
64+
}
65+
66+
private int getMinValue(TreeNode current) {
67+
int minValue = current.value;
68+
while (current.leftChild != null) {
69+
minValue = current.leftChild.value;
70+
current = current.leftChild;
71+
}
72+
return minValue;
73+
}
74+
75+
public void clear() {
76+
root = clearAll(root);
77+
}
78+
79+
private TreeNode clearAll(TreeNode current) {
80+
if (current == null) {
81+
return null;
82+
}
83+
current.leftChild = clearAll(current.leftChild);
84+
current.rightChild = clearAll(current.rightChild);
85+
current = null;
86+
return current;
87+
}
88+
}
89+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
class TreeNode {
4+
int value;
5+
TreeNode leftChild;
6+
TreeNode rightChild;
7+
8+
TreeNode(int key) {
9+
value = key;
10+
leftChild = rightChild = null;
11+
}
12+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Map;
4+
import java.util.PriorityQueue;
5+
import java.util.TreeMap;
6+
7+
public abstract class HuffmanAlgorithm {
8+
9+
public static Map<Character, Integer> generateFrequencyMap(String input) {
10+
Map<Character, Integer> frequencyMap = new TreeMap<>();
11+
for (char character : input.toCharArray()) {
12+
frequencyMap.put(character, frequencyMap.getOrDefault(character, 0) + 1);
13+
}
14+
return frequencyMap;
15+
}
16+
17+
public static Map<Character, String> constructHuffmanTree(Map<Character, Integer> frequencyMap) {
18+
PriorityQueue<HuffmanTreeNode> priorityQueue = new PriorityQueue<>();
19+
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
20+
priorityQueue.add(new HuffmanTreeNode(entry.getKey(), entry.getValue()));
21+
}
22+
23+
while (priorityQueue.size() > 1) {
24+
HuffmanTreeNode leftNode = priorityQueue.poll();
25+
HuffmanTreeNode rightNode = priorityQueue.poll();
26+
HuffmanTreeNode parentNode = new HuffmanTreeNode('\0', leftNode.frequency + rightNode.frequency);
27+
parentNode.left = leftNode;
28+
parentNode.right = rightNode;
29+
priorityQueue.add(parentNode);
30+
}
31+
32+
Map<Character, String> huffmanCodes = new TreeMap<>();
33+
createHuffmanCodes(priorityQueue.peek(), "", huffmanCodes);
34+
35+
return huffmanCodes;
36+
}
37+
38+
public static void createHuffmanCodes(HuffmanTreeNode node, String code, Map<Character, String> huffmanCodes) {
39+
if (node != null) {
40+
if (node.character != '\0') {
41+
huffmanCodes.put(node.character, code);
42+
}
43+
createHuffmanCodes(node.left, code + "0", huffmanCodes);
44+
createHuffmanCodes(node.right, code + "1", huffmanCodes);
45+
}
46+
}
47+
48+
public static class HuffmanTreeNode implements Comparable<HuffmanTreeNode> {
49+
char character;
50+
int frequency;
51+
HuffmanTreeNode left;
52+
HuffmanTreeNode right;
53+
54+
HuffmanTreeNode(char character, int frequency) {
55+
this.character = character;
56+
this.frequency = frequency;
57+
}
58+
59+
@Override
60+
public int compareTo(HuffmanTreeNode other) {
61+
return Integer.compare(this.frequency, other.frequency);
62+
}
63+
}
64+
}

students/23K0815/23K0815-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>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p31</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public abstract class BurgerDisplay {
7+
8+
public static void displayBurgerTree(BurgerNode root) {
9+
if (root == null) {
10+
return;
11+
}
12+
13+
int height = calculateHeight(root);
14+
Deque<BurgerNode> queue = new ArrayDeque<>();
15+
queue.offer(root);
16+
int level = 0;
17+
18+
while (!queue.isEmpty()) {
19+
int levelSize = queue.size();
20+
int spacesBefore = (int) Math.pow(2, height - level - 1) - 1;
21+
int spacesBetween = (int) Math.pow(2, height - level) - 1;
22+
23+
System.out.print(" ".repeat(spacesBefore));
24+
25+
for (int i = 0; i < levelSize; i++) {
26+
BurgerNode node = queue.poll();
27+
System.out.print(node.patty);
28+
29+
if (i < levelSize - 1) {
30+
System.out.print(" ".repeat(spacesBetween));
31+
}
32+
33+
if (node.lettuce != null) {
34+
queue.offer(node.lettuce);
35+
}
36+
if (node.tomato != null) {
37+
queue.offer(node.tomato);
38+
}
39+
if (node.onion != null) {
40+
queue.offer(node.onion);
41+
}
42+
}
43+
System.out.println();
44+
level++;
45+
}
46+
}
47+
48+
// Function to compute the height of the burger tree
49+
private static int calculateHeight(BurgerNode root) {
50+
if (root == null) {
51+
return 0;
52+
}
53+
return 1 + Math.max(Math.max(calculateHeight(root.lettuce), calculateHeight(root.tomato)), calculateHeight(root.onion));
54+
}
55+
56+
public static void main(String[] args) {
57+
BurgerNode root = new BurgerNode(5);
58+
root.lettuce = new BurgerNode(2);
59+
root.tomato = new BurgerNode(3);
60+
root.onion = new BurgerNode(6);
61+
root.lettuce.lettuce = new BurgerNode(1);
62+
root.lettuce.onion = new BurgerNode(2);
63+
root.tomato.lettuce = new BurgerNode(4);
64+
root.onion.lettuce = new BurgerNode(5);
65+
root.onion.onion = new BurgerNode(6);
66+
67+
displayBurgerTree(root);
68+
}
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
class BurgerNode {
4+
int patty;
5+
BurgerNode lettuce;
6+
BurgerNode tomato;
7+
BurgerNode onion;
8+
9+
BurgerNode(int patty) {
10+
this.patty = patty;
11+
}
12+
}
13+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
public abstract class BurgerTree {
4+
public static void displayBurgerTree(BurgerNode node) {
5+
if (node == null) {
6+
return;
7+
}
8+
9+
System.out.print(node.patty);
10+
11+
if (node.lettuce != null && node.onion != null) {
12+
System.out.print("-");
13+
}
14+
15+
System.out.print(" ");
16+
displayBurgerTree(node.lettuce);
17+
displayBurgerTree(node.tomato);
18+
displayBurgerTree(node.onion);
19+
}
20+
21+
public static void main(String[] args) {
22+
BurgerNode root = new BurgerNode(5);
23+
root.lettuce = new BurgerNode(2);
24+
root.tomato = new BurgerNode(3);
25+
root.onion = new BurgerNode(6);
26+
root.lettuce.lettuce = new BurgerNode(1);
27+
root.lettuce.onion = new BurgerNode(2);
28+
root.tomato.lettuce = new BurgerNode(4);
29+
root.onion.lettuce = new BurgerNode(5);
30+
root.onion.onion = new BurgerNode(6);
31+
32+
displayBurgerTree(root);
33+
}
34+
}

students/23K0815/23K0815-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>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p32</artifactId>
12+
<description>Массивы</description>
13+
</project>

0 commit comments

Comments
 (0)