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

Commit 53a51e2

Browse files
committed
Lab30-31
1 parent 3009b51 commit 53a51e2

File tree

11 files changed

+477
-0
lines changed

11 files changed

+477
-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0565</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0565-p30-1</artifactId>
12+
<description>30.1 практическая</description>
13+
</project>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class BinTreeOper {
6+
Node root;
7+
8+
int height(Node n) {
9+
if (n == null) {
10+
return 0;
11+
}
12+
return Math.max(height(n.left), height(n.right)) + 1;
13+
}
14+
15+
boolean lookup(Node n, int target) {
16+
if (n == null) {
17+
return false;
18+
}
19+
if (n.data == target) {
20+
return true;
21+
}
22+
if (target < n.data) {
23+
return lookup(n.left, target);
24+
} else {
25+
return lookup(n.right, target);
26+
}
27+
}
28+
29+
void reverse(Node n) {
30+
if (n == null) {
31+
return;
32+
}
33+
Node tmp = n.left;
34+
n.left = n.right;
35+
n.right = tmp;
36+
reverse(n.left);
37+
reverse(n.right);
38+
}
39+
40+
int maxWidth(Node n) {
41+
int maxW = 0;
42+
int h = height(n);
43+
for (int i = 1; i <= h; i++) {
44+
int w = getWidth(n, i);
45+
if (w > maxW) {
46+
maxW = w;
47+
}
48+
}
49+
return maxW;
50+
}
51+
52+
int getWidth(Node n, int lvl) {
53+
if (n == null) {
54+
return 0;
55+
}
56+
if (lvl == 1) {
57+
return 1;
58+
}
59+
return getWidth(n.left, lvl - 1) + getWidth(n.right, lvl - 1);
60+
}
61+
62+
int size(Node n) {
63+
if (n == null) {
64+
return 0;
65+
}
66+
return size(n.left) + 1 + size(n.right);
67+
}
68+
69+
boolean sameTree(Node a, Node b) {
70+
if (a == null && b == null) {
71+
return true;
72+
}
73+
if (a != null && b != null) {
74+
return a.data == b.data && sameTree(a.left, b.left) && sameTree(a.right, b.right);
75+
}
76+
return false;
77+
}
78+
79+
static HuffmanN buildHuffTree(int[] freqs) {
80+
PriorityQueue<HuffmanN> pq = new PriorityQueue<>((a, b) -> a.freq - b.freq);
81+
for (int f : freqs) {
82+
pq.add(new HuffmanN(f));
83+
}
84+
85+
while (pq.size() > 1) {
86+
HuffmanN a = pq.poll();
87+
HuffmanN b = pq.poll();
88+
HuffmanN sum = new HuffmanN(a.freq + b.freq);
89+
sum.left = a;
90+
sum.right = b;
91+
pq.add(sum);
92+
}
93+
return pq.poll();
94+
}
95+
96+
public static void main(String[] args) {
97+
BinTreeOper tree = new BinTreeOper();
98+
tree.root = new Node(5);
99+
tree.root.left = new Node(3);
100+
tree.root.right = new Node(8);
101+
tree.root.left.left = new Node(1);
102+
tree.root.left.right = new Node(4);
103+
104+
System.out.println("Высота дерева: " + tree.height(tree.root));
105+
System.out.println("Поиск 4: " + tree.lookup(tree.root, 4));
106+
System.out.println("Ширина дерева: " + tree.maxWidth(tree.root));
107+
System.out.println("Количество узлов: " + tree.size(tree.root));
108+
109+
int[] freqs = {2, 3, 5, 7, 11, 13, 17};
110+
HuffmanN root = buildHuffTree(freqs);
111+
System.out.println("Корень дерева Хаффмана: " + root.freq);
112+
}
113+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
public class HuffmanN {
4+
int freq;
5+
HuffmanN left;
6+
HuffmanN right;
7+
8+
public HuffmanN(int freq) {
9+
this.freq = freq;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
public class Node {
4+
int data;
5+
Node left;
6+
Node right;
7+
8+
public Node(int d) {
9+
data = d;
10+
left = null;
11+
right = null;
12+
}
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ru.mirea.practice.s0000001.n2;
2+
3+
import java.util.PriorityQueue;
4+
5+
public final class HufAlg {
6+
7+
private HufAlg() {
8+
throw new UnsupportedOperationException("Utility class");
9+
}
10+
11+
static HufNod buildHuffmanTree(int[] freqs) {
12+
PriorityQueue<HufNod> pq = new PriorityQueue<>((a, b) -> a.freq - b.freq);
13+
for (int f : freqs) {
14+
pq.add(new HufNod(f));
15+
}
16+
17+
while (pq.size() > 1) {
18+
HufNod a = pq.poll();
19+
HufNod b = pq.poll();
20+
HufNod sum = new HufNod(a.freq + b.freq);
21+
sum.left = a;
22+
sum.right = b;
23+
pq.add(sum);
24+
}
25+
return pq.poll();
26+
}
27+
28+
static void printHuffmanTree(HufNod root, String code) {
29+
if (root == null) {
30+
return;
31+
}
32+
if (root.left == null && root.right == null) {
33+
System.out.println("Символ: " + root.freq + " Код: " + code);
34+
}
35+
printHuffmanTree(root.left, code + "0");
36+
printHuffmanTree(root.right, code + "1");
37+
}
38+
39+
public static void main(String[] args) {
40+
int[] freqs = {2, 3, 5, 7, 11, 13, 17};
41+
42+
HufNod root = buildHuffmanTree(freqs);
43+
System.out.println("Корень дерева Хаффмана: " + root.freq);
44+
System.out.println("Коды Хаффмана для символов:");
45+
printHuffmanTree(root, "");
46+
}
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s0000001.n2;
2+
3+
public class HufNod {
4+
int freq;
5+
HufNod left;
6+
HufNod right;
7+
8+
public HufNod(int freq) {
9+
this.freq = freq;
10+
}
11+
}
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0565</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0565-p30-2</artifactId>
12+
<description>30.2 практическая</description>
13+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public class BsTree {
4+
Node root;
5+
6+
public void insert(int value) {
7+
root = insertRec(root, value);
8+
}
9+
10+
private Node insertRec(Node root, int value) {
11+
if (root == null) {
12+
return new Node(value);
13+
}
14+
if (value < root.data) {
15+
root.left = insertRec(root.left, value);
16+
} else if (value > root.data) {
17+
root.right = insertRec(root.right, value);
18+
}
19+
return root;
20+
}
21+
22+
public void inorder() {
23+
inorderRec(root);
24+
System.out.println();
25+
}
26+
27+
private void inorderRec(Node root) {
28+
if (root != null) {
29+
inorderRec(root.left);
30+
System.out.print(root.data + " ");
31+
inorderRec(root.right);
32+
}
33+
}
34+
35+
public void delete(int value) {
36+
root = deleteRec(root, value);
37+
}
38+
39+
private Node deleteRec(Node root, int value) {
40+
if (root == null) {
41+
return null;
42+
}
43+
44+
if (value < root.data) {
45+
root.left = deleteRec(root.left, value);
46+
} else if (value > root.data) {
47+
root.right = deleteRec(root.right, value);
48+
} else {
49+
if (root.left == null) {
50+
return root.right;
51+
} else if (root.right == null) {
52+
return root.left;
53+
}
54+
55+
root.data = minValue(root.right);
56+
root.right = deleteRec(root.right, root.data);
57+
}
58+
return root;
59+
}
60+
61+
private int minValue(Node root) {
62+
int minVal = root.data;
63+
while (root.left != null) {
64+
root = root.left;
65+
minVal = root.data;
66+
}
67+
return minVal;
68+
}
69+
70+
public void deleteTree() {
71+
root = null;
72+
System.out.println("Дерево удалено.");
73+
}
74+
75+
public static void main(String[] args) {
76+
BsTree tree = new BsTree();
77+
78+
int[] values = {50, 30, 70, 20, 40, 60, 80, 10, 35, 65};
79+
for (int val : values) {
80+
tree.insert(val);
81+
}
82+
83+
System.out.println("Содержимое дерева до удаления такое:");
84+
tree.inorder();
85+
86+
tree.delete(40);
87+
System.out.println("Содержимое дерева после удаления узла со значением 40:");
88+
tree.inorder();
89+
90+
tree.deleteTree();
91+
}
92+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public class Node {
4+
int data;
5+
Node left;
6+
Node right;
7+
8+
public Node(int data) {
9+
this.data = data;
10+
this.left = null;
11+
this.right = null;
12+
}
13+
}

students/23K0565/23K0565-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0565</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0565-p31</artifactId>
12+
<description>31 практическая</description>
13+
</project>

0 commit comments

Comments
 (0)