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

Лабораторная№32 #709

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions students/23K0565/23K0565-p30-1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0565</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0565-p30-1</artifactId>
<description>30.1 практическая</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package ru.mirea.practice.s0000001.n1;

import java.util.PriorityQueue;

public class BinTreeOper {
Node root;

int height(Node n) {
if (n == null) {
return 0;
}
return Math.max(height(n.left), height(n.right)) + 1;
}

boolean lookup(Node n, int target) {
if (n == null) {
return false;
}
if (n.data == target) {
return true;
}
if (target < n.data) {
return lookup(n.left, target);
} else {
return lookup(n.right, target);
}
}

void reverse(Node n) {
if (n == null) {
return;
}
Node tmp = n.left;
n.left = n.right;
n.right = tmp;
reverse(n.left);
reverse(n.right);
}

int maxWidth(Node n) {
int maxW = 0;
int h = height(n);
for (int i = 1; i <= h; i++) {
int w = getWidth(n, i);
if (w > maxW) {
maxW = w;
}
}
return maxW;
}

int getWidth(Node n, int lvl) {
if (n == null) {
return 0;
}
if (lvl == 1) {
return 1;
}
return getWidth(n.left, lvl - 1) + getWidth(n.right, lvl - 1);
}

int size(Node n) {
if (n == null) {
return 0;
}
return size(n.left) + 1 + size(n.right);
}

boolean sameTree(Node a, Node b) {
if (a == null && b == null) {
return true;
}
if (a != null && b != null) {
return a.data == b.data && sameTree(a.left, b.left) && sameTree(a.right, b.right);
}
return false;
}

static HuffmanN buildHuffTree(int[] freqs) {
PriorityQueue<HuffmanN> pq = new PriorityQueue<>((a, b) -> a.freq - b.freq);
for (int f : freqs) {
pq.add(new HuffmanN(f));
}

while (pq.size() > 1) {
HuffmanN a = pq.poll();
HuffmanN b = pq.poll();
HuffmanN sum = new HuffmanN(a.freq + b.freq);
sum.left = a;
sum.right = b;
pq.add(sum);
}
return pq.poll();
}

public static void main(String[] args) {
BinTreeOper tree = new BinTreeOper();
tree.root = new Node(5);
tree.root.left = new Node(3);
tree.root.right = new Node(8);
tree.root.left.left = new Node(1);
tree.root.left.right = new Node(4);

System.out.println("Высота дерева: " + tree.height(tree.root));
System.out.println("Поиск 4: " + tree.lookup(tree.root, 4));
System.out.println("Ширина дерева: " + tree.maxWidth(tree.root));
System.out.println("Количество узлов: " + tree.size(tree.root));

int[] freqs = {2, 3, 5, 7, 11, 13, 17};
HuffmanN root = buildHuffTree(freqs);
System.out.println("Корень дерева Хаффмана: " + root.freq);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.mirea.practice.s0000001.n1;

public class HuffmanN {
int freq;
HuffmanN left;
HuffmanN right;

public HuffmanN(int freq) {
this.freq = freq;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.mirea.practice.s0000001.n1;

public class Node {
int data;
Node left;
Node right;

public Node(int d) {
data = d;
left = null;
right = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.mirea.practice.s0000001.n2;

import java.util.PriorityQueue;

public final class HufAlg {

private HufAlg() {
throw new UnsupportedOperationException("Utility class");
}

static HufNod buildHuffmanTree(int[] freqs) {
PriorityQueue<HufNod> pq = new PriorityQueue<>((a, b) -> a.freq - b.freq);
for (int f : freqs) {
pq.add(new HufNod(f));
}

while (pq.size() > 1) {
HufNod a = pq.poll();
HufNod b = pq.poll();
HufNod sum = new HufNod(a.freq + b.freq);
sum.left = a;
sum.right = b;
pq.add(sum);
}
return pq.poll();
}

static void printHuffmanTree(HufNod root, String code) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
System.out.println("Символ: " + root.freq + " Код: " + code);
}
printHuffmanTree(root.left, code + "0");
printHuffmanTree(root.right, code + "1");
}

public static void main(String[] args) {
int[] freqs = {2, 3, 5, 7, 11, 13, 17};

HufNod root = buildHuffmanTree(freqs);
System.out.println("Корень дерева Хаффмана: " + root.freq);
System.out.println("Коды Хаффмана для символов:");
printHuffmanTree(root, "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.mirea.practice.s0000001.n2;

public class HufNod {
int freq;
HufNod left;
HufNod right;

public HufNod(int freq) {
this.freq = freq;
}
}
13 changes: 13 additions & 0 deletions students/23K0565/23K0565-p30-2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0565</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0565-p30-2</artifactId>
<description>30.2 практическая</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ru.mirea.practice.s0000001;

public class BsTree {
Node root;

public void insert(int value) {
root = insertRec(root, value);
}

private Node insertRec(Node root, int value) {
if (root == null) {
return new Node(value);
}
if (value < root.data) {
root.left = insertRec(root.left, value);
} else if (value > root.data) {
root.right = insertRec(root.right, value);
}
return root;
}

public void inorder() {
inorderRec(root);
System.out.println();
}

private void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}

public void delete(int value) {
root = deleteRec(root, value);
}

private Node deleteRec(Node root, int value) {
if (root == null) {
return null;
}

if (value < root.data) {
root.left = deleteRec(root.left, value);
} else if (value > root.data) {
root.right = deleteRec(root.right, value);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}

root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}
return root;
}

private int minValue(Node root) {
int minVal = root.data;
while (root.left != null) {
root = root.left;
minVal = root.data;
}
return minVal;
}

public void deleteTree() {
root = null;
System.out.println("Дерево удалено.");
}

public static void main(String[] args) {
BsTree tree = new BsTree();

int[] values = {50, 30, 70, 20, 40, 60, 80, 10, 35, 65};
for (int val : values) {
tree.insert(val);
}

System.out.println("Содержимое дерева до удаления такое:");
tree.inorder();

tree.delete(40);
System.out.println("Содержимое дерева после удаления узла со значением 40:");
tree.inorder();

tree.deleteTree();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.mirea.practice.s0000001;

public class Node {
int data;
Node left;
Node right;

public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
13 changes: 13 additions & 0 deletions students/23K0565/23K0565-p31/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0565</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0565-p31</artifactId>
<description>31 практическая</description>
</project>
Loading
Loading