|
| 1 | +package ru.mirea.practice.s0000001; |
| 2 | + |
| 3 | +import java.util.PriorityQueue; |
| 4 | +import java.util.Comparator; |
| 5 | + |
| 6 | +public final class BinaryTree { |
| 7 | + |
| 8 | + private BinaryTree() { |
| 9 | + // Пусто |
| 10 | + } |
| 11 | + |
| 12 | + static class Node { |
| 13 | + int data; |
| 14 | + Node left; |
| 15 | + Node right; |
| 16 | + |
| 17 | + Node(int data) { |
| 18 | + this.data = data; |
| 19 | + left = right = null; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static int height(Node node) { |
| 24 | + if (node == null) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + return Math.max(height(node.left), height(node.right)) + 1; |
| 28 | + } |
| 29 | + |
| 30 | + public static Node mirror(Node node) { |
| 31 | + if (node == null) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + Node temp = node.left; |
| 35 | + node.left = mirror(node.right); |
| 36 | + node.right = mirror(temp); |
| 37 | + return node; |
| 38 | + } |
| 39 | + |
| 40 | + public static boolean search(Node node, int value) { |
| 41 | + if (node == null) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + if (node.data == value) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + return value < node.data ? search(node.left, value) : search(node.right, value); |
| 48 | + } |
| 49 | + |
| 50 | + public static void huffmanEncoding(int[] frequencies) { |
| 51 | + PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparingInt(n -> n.data)); |
| 52 | + for (int freq : frequencies) { |
| 53 | + queue.add(new Node(freq)); |
| 54 | + } |
| 55 | + while (queue.size() > 1) { |
| 56 | + Node left = queue.poll(); |
| 57 | + Node right = queue.poll(); |
| 58 | + Node parent = new Node(left.data + right.data); |
| 59 | + parent.left = left; |
| 60 | + parent.right = right; |
| 61 | + queue.add(parent); |
| 62 | + } |
| 63 | + printHuffmanTree(queue.poll(), ""); |
| 64 | + } |
| 65 | + |
| 66 | + private static void printHuffmanTree(Node root, String code) { |
| 67 | + if (root == null) { |
| 68 | + return; |
| 69 | + } |
| 70 | + if (root.left == null && root.right == null) { |
| 71 | + System.out.println("Значение: " + root.data + ", Код: " + code); |
| 72 | + } |
| 73 | + printHuffmanTree(root.left, code + "0"); |
| 74 | + printHuffmanTree(root.right, code + "1"); |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + Node root = new Node(10); |
| 79 | + root.left = new Node(5); |
| 80 | + root.right = new Node(15); |
| 81 | + root.left.left = new Node(3); |
| 82 | + root.left.right = new Node(7); |
| 83 | + |
| 84 | + System.out.println("Высота: " + height(root)); |
| 85 | + System.out.println("Найти 7: " + search(root, 7)); |
| 86 | + System.out.println("Найти 20: " + search(root, 20)); |
| 87 | + |
| 88 | + mirror(root); |
| 89 | + |
| 90 | + System.out.println("Алгоритм Хаффмана:"); |
| 91 | + int[] frequencies = {2, 3, 5, 7, 11, 13}; |
| 92 | + huffmanEncoding(frequencies); |
| 93 | + } |
| 94 | +} |
0 commit comments