截图:
27 Hanoi塔问题
27.1 汉诺塔问题:有A、B、C三个柱子,需要将A上面的那个n个盘子(从下到上盘子依次变大),移动到C柱上,移动的过程借助B柱,但不能出现大盘在小盘的情况 。
代码:
package 日撸Java300行_21_30;/*** Hanoi tower* @time 2022/4/15* @author Hui Xiao*/public class Hanoi {/********************** Move a number of plates.* * @param paraSource*The source pole.* @param paraIntermedium*The intermediary pole.* @param paraDestination*The destination pole.* @param paraNumber*The number of plates.********************/public static void hanoi(char paraSource, char paraIntermediary, char paraDestionation,int paraNumber) {if (paraNumber == 1) {System.out.println(paraSource + "->" + paraDestionation + " ");return;} // Of ifhanoi(paraSource, paraDestionation, paraIntermediary, paraNumber - 1);System.out.println(paraSource + "->" + paraDestionation + " ");hanoi(paraIntermediary, paraSource, paraDestionation, paraNumber - 1);} // Of hanoi/********************** The entrance of the program.* * @param args*Not used now.********************/public static void main(String[] args) {hanoi('a', 'b', 'c', 3);} // Of main} // Of class Hanoi
截图:
28 编码
28.1 定义了一个内嵌类 。如果是实际项目,我就为其单独写一个文件了,这里仅仅是为了方便 。
28.2 每个节点的内容包括:字符 (仅对叶节点有效)、权重 (用的整数, 该字符的个数)、指向子节点父节点的引用 。这里指向父节点的引用是必须的 。
28.3是指 ASCII 字符集的字符个数 。为方便起见,仅支持 ASCII 。
28.4的引入只是想把程序尽可能细分成独立的模块,这样便于学习和调拭 。
28.5仅存出现过的字符 。
28.6完全可以用 .() 代替 。
28.7要为所有的节点负责,其元素对应于里面的。
28.8是为了从 ASCII 里面的顺序映射到里面的顺序 。
28.9将个字符映射为一个字符串 。
28.10 nodes 要先把所有的节点存储在一个数组里面,然后再链接它们 。
28.11 构造方法仅初始化了 ,读入了文件 。
28.12采用了最简单粗暴的方式 。还可以有其它的逐行读入的方式 。
28.13 要自己弄个文本文件,里面存放一个字符串之类,或者几行英文文本 。
代码:
package 日撸Java300行_21_30;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Paths;import java.util.Arrays;import java.util.stream.Collectors;/*** Huffman tree, encoding, and decoding. For simplicity, only ASCII charecters* are supported.* @time 2022/4/15* @author Hui Xiao*/public class Huffman {/*** An inner class for Huffman nodes.*/class HuffmanNode {/*** The char. Only valid for leaf nodes.*/char character;/*** Weight. It can also be double.*/int weight;/*** The left child.*/HuffmanNode leftChild;/*** The right child.*/HuffmanNode rightChild;/*** The parent, It helps constructing the Huffman code of each character.*/HuffmanNode parent;/********************** The first constructr********************/public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild,HuffmanNode paraRightChild, HuffmanNode paraprarent) {character = paraCharacter;weight = paraWeight;leftChild = paraLeftChild;rightChild = paraRightChild;parent = paraprarent;} // Of HuffmanNode/********************** To string.********************/public String toString() {String resultString = "(" + character + ", " + weight + ")";return resultString;} // Of toString} // Of class HuffmanNode/*** The number of characters. 256 for ASCII.*/public static final int NUM_CHARS = 256;/*** The input text. It is stored in a string. for simplicity.*/String inputText;/*** The length of the alghabet, alse the number of leaves.*/int alphabetLength;/*** The alphabet*/char[] alphabet;/*** The count of chars. The length is 2 * alphbetLength - 1 to include* non-leaf nodes.*/int[] charCounts;/*** The mapping of chars to the indices in the alphabet.*/int[] charMapping;/*** Codes for each char int the alphbet. It should have the same length as* alphabet.*/String[] huffmanCodes;/*** All nodes. The last node is the root.*/HuffmanNode[] nodes;/********************** The first constructor.* @param paraFilename*The test filename.********************/public Huffman(String paraFilename) {charMapping = new int[NUM_CHARS];readText(paraFilename);} // Ofthe first constructor/********************** Read text.* * @param paraFilename*The text filename.********************/public void readText(String paraFilename) {try {inputText = Files.newBufferedReader(Paths.get(paraFilename),StandardCharsets.UTF_8).lines().collect(Collectors.joining("\n"));} catch (Exception ee) {System.out.println(ee);System.exit(0);} // Of trySystem.out.println("The text is:\r\n" + inputText);} // Of readText/********************** Construct the alphabet. The results are stored in the menber variables* charMapping and alpgabet.********************/public void constructAlphabet() {//Initialize.Arrays.fill(charMapping, -1);// The count for each char. At most NUM_CHARS chars.int[] tempCharCounts = new int[NUM_CHARS];// The index of the char in the ASCII charset.int tempCharIndex;// Step 1. Scan the string to obtain the counts.char tempChar;for (int i = 0; i