數據結構2-樹

 

目錄

二叉樹

順序二叉樹

線索化二叉樹

赫夫曼樹

霍夫曼編碼

數據壓縮

數據解壓

赫夫曼文件解壓與壓縮

二叉排序樹

查找、添加、遍歷、刪除

平衡二叉樹


 

二叉樹

滿二叉樹 完全二叉樹

前序 中序  後序

查找 刪除

class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //刪除結點
    public void delNode(int no) {
        if(root != null) {
            //如果只有一個root結點, 這裏立即判斷root是不是就是要刪除結點
            if(root.getNo() == no) {
                root = null;
            } else {
                //遞歸刪除
                root.delNode(no);
            }
        }else{
            System.out.println("空樹,不能刪除~");
        }
    }

    public void preOder(){
        if (this.root!=null)
            this.root.preOder();
        else
            System.out.println("二叉樹爲空,無法遍歷");
    }
    //中序遍歷
    public void infixOrder() {
        if(this.root != null) {
            this.root.infixOder();
        }else {
            System.out.println("二叉樹爲空,無法遍歷");
        }
    }
    //後序遍歷
    public void postOrder() {
        if(this.root != null) {
            this.root.postOder();
        }else {
            System.out.println("二叉樹爲空,無法遍歷");
        }
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        if (root!=null)
            return root.preOderSearch(no);
        else
            return null;
    }
    //中序遍歷
    public HeroNode infixOrderSearch(int no) {
        if(root != null) {
            return root.infixOderSearch(no);
        }else {
            return null;
        }
    }
    //後序遍歷
    public HeroNode postOrderSearch(int no) {
        if(root != null) {
            return this.root.postOderSearch(no);
        }else {
            return null;
        }
    }
}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //遞歸刪除結點
    //1.如果刪除的節點是葉子節點,則刪除該節點
    //2.如果刪除的節點是非葉子節點,則刪除該子樹
    public void delNode(int no) {
        //思路
		/*
		 * 	1. 因爲我們的二叉樹是單向的,所以我們是判斷當前結點的子結點是否需要刪除結點,而不能去判斷當前這個結點是不是需要刪除結點.
			2. 如果當前結點的左子結點不爲空,並且左子結點 就是要刪除結點,就將this.left = null; 並且就返回(結束遞歸刪除)
			3. 如果當前結點的右子結點不爲空,並且右子結點 就是要刪除結點,就將this.right= null ;並且就返回(結束遞歸刪除)
			4. 如果第2和第3步沒有刪除結點,那麼我們就需要向左子樹進行遞歸刪除
			5.  如果第4步也沒有刪除結點,則應當向右子樹進行遞歸刪除.
		 */
        //2. 如果當前結點的左子結點不爲空,並且左子結點 就是要刪除結點,就將this.left = null; 並且就返回(結束遞歸刪除)
        if(this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //3.如果當前結點的右子結點不爲空,並且右子結點 就是要刪除結點,就將this.right= null ;並且就返回(結束遞歸刪除)
        if(this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //4.我們就需要向左子樹進行遞歸刪除
        if(this.left != null) {
            this.left.delNode(no);
        }
        //5.則應當向右子樹進行遞歸刪除
        if(this.right != null) {
            this.right.delNode(no);
        }
    }

    //前序遍歷
    public void preOder(){
        System.out.println(this);
        if (this.left!=null)
            this.left.preOder();
        if (this.right!=null)
            this.right.preOder();

    }

    //中序
    public void infixOder(){
        if (this.left!=null)
            this.left.infixOder();
        System.out.println(this);
        if (this.right!=null)
            this.right.infixOder();
    }
    //後序
    public void postOder(){
        if (this.left!=null)
            this.left.postOder();
        if (this.right!=null)
            this.right.postOder();
        System.out.println(this);
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        System.out.println("前序遍歷查找");
        if (this.no==no)
            return this;
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.preOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.preOderSearch(no);
        return resNode;
    }

    //中序查找
    public HeroNode infixOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.infixOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("中序遍歷查找");
        if (this.no==no)
            return this;
        if (this.right!=null)
            resNode=this.right.infixOderSearch(no);
        return resNode;
    }

    //又序查找
    public HeroNode postOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("進入後序查找");
        //如果左右子樹都沒有找到,就比較當前結點是不是
        if(this.no == no) {
            return this;
        }
        return resNode;
    }

}

順序二叉樹

class ArrBinaryTree{
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }
    //重載preOrder
    public void preOder() {
        this.preOder(0);
    }

//前序
    public void preOder(int index){
        if(arr == null || arr.length == 0) {
            System.out.println("數組爲空,不能按照二叉樹的前序遍歷");
        }
        System.out.println(arr[index]);

        //向左遞歸遍歷
        if((index * 2 + 1) < arr.length) {
            preOder(2 * index + 1 );
        }
        //向右遞歸遍歷
        if((index * 2 + 2) < arr.length) {
            preOder(2 * index + 2);
        }

    }
}

線索化二叉樹

class threadedBinaryTree{
    private HeroNode root;
    //需要創建指向前驅節點的指針
    private HeroNode pre=null;

    //重載一把threadedNodes方法
    public void threadedNodes() {
        this.threadedNodes(root);
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void threadedList(){
        HeroNode node =root;
        while (node!=null){
            while (node.getLeftType()==0){
                node=node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType()==1){
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();
        }
    }
    public void threadedNodes(HeroNode node){
        if (node==null)
            return;
        threadedNodes(node.getLeft());

        //(二)線索化當前結點[有難度]
        //處理當前結點的前驅結點
        if (node.getLeft()==null){
            //讓當前節點左指針指向前驅節點
            node.setLeft(pre);
            node.setLeftType(1);  //指向前驅節點
        }
        //處理後繼結點
        if(pre!=null&&pre.getRight()==null){
            pre.setRight(node);
            pre.setRightType(1);
        }
        //!!! 每處理一個結點後,讓當前結點是下一個結點的前驅結點
        pre=node;

        threadedNodes(node.getRight());
    }
    //刪除結點
    public void delNode(int no) {
        if(root != null) {
            //如果只有一個root結點, 這裏立即判斷root是不是就是要刪除結點
            if(root.getNo() == no) {
                root = null;
            } else {
                //遞歸刪除
                root.delNode(no);
            }
        }else{
            System.out.println("空樹,不能刪除~");
        }
    }
    public void preOder(){
        if (this.root!=null)
            this.root.preOder();
        else
            System.out.println("二叉樹爲空,無法遍歷");
    }
    //中序遍歷
    public void infixOrder() {
        if(this.root != null) {
            this.root.infixOder();
        }else {
            System.out.println("二叉樹爲空,無法遍歷");
        }
    }
    //後序遍歷
    public void postOrder() {
        if(this.root != null) {
            this.root.postOder();
        }else {
            System.out.println("二叉樹爲空,無法遍歷");
        }
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        if (root!=null)
            return root.preOderSearch(no);
        else
            return null;
    }
    //中序遍歷
    public HeroNode infixOrderSearch(int no) {
        if(root != null) {
            return root.infixOderSearch(no);
        }else {
            return null;
        }
    }
    //後序遍歷
    public HeroNode postOrderSearch(int no) {
        if(root != null) {
            return this.root.postOderSearch(no);
        }else {
            return null;
        }
    }
}


class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    //說明
    //1. 如果leftType == 0 表示指向的是左子樹, 如果 1 則表示指向前驅結點
    //2. 如果rightType == 0 表示指向是右子樹, 如果 1表示指向後繼結點
    private int leftType;
    private int rightType;
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //遞歸刪除結點
    //1.如果刪除的節點是葉子節點,則刪除該節點
    //2.如果刪除的節點是非葉子節點,則刪除該子樹
    public void delNode(int no) {
        //思路
            /*
             * 	1. 因爲我們的二叉樹是單向的,所以我們是判斷當前結點的子結點是否需要刪除結點,而不能去判斷當前這個結點是不是需要刪除結點.
                2. 如果當前結點的左子結點不爲空,並且左子結點 就是要刪除結點,就將this.left = null; 並且就返回(結束遞歸刪除)
                3. 如果當前結點的右子結點不爲空,並且右子結點 就是要刪除結點,就將this.right= null ;並且就返回(結束遞歸刪除)
                4. 如果第2和第3步沒有刪除結點,那麼我們就需要向左子樹進行遞歸刪除
                5.  如果第4步也沒有刪除結點,則應當向右子樹進行遞歸刪除.
             */
        //2. 如果當前結點的左子結點不爲空,並且左子結點 就是要刪除結點,就將this.left = null; 並且就返回(結束遞歸刪除)
        if(this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //3.如果當前結點的右子結點不爲空,並且右子結點 就是要刪除結點,就將this.right= null ;並且就返回(結束遞歸刪除)
        if(this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //4.我們就需要向左子樹進行遞歸刪除
        if(this.left != null) {
            this.left.delNode(no);
        }
        //5.則應當向右子樹進行遞歸刪除
        if(this.right != null) {
            this.right.delNode(no);
        }
    }

    //前序遍歷
    public void preOder(){
        System.out.println(this);
        if (this.left!=null)
            this.left.preOder();
        if (this.right!=null)
            this.right.preOder();

    }
    //中序
    public void infixOder(){
        if (this.left!=null)
            this.left.infixOder();
        System.out.println(this);
        if (this.right!=null)
            this.right.infixOder();
    }
    //後序
    public void postOder(){
        if (this.left!=null)
            this.left.postOder();
        if (this.right!=null)
            this.right.postOder();
        System.out.println(this);
    }
    //前序查找
    public HeroNode preOderSearch(int no){
        System.out.println("前序遍歷查找");
        if (this.no==no)
            return this;
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.preOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.preOderSearch(no);
        return resNode;
    }
    //中序查找
    public HeroNode infixOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.infixOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("中序遍歷查找");
        if (this.no==no)
            return this;
        if (this.right!=null)
            resNode=this.right.infixOderSearch(no);
        return resNode;
    }

    //又序查找
    public HeroNode postOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("進入後序查找");
        //如果左右子樹都沒有找到,就比較當前結點是不是
        if(this.no == no) {
            return this;
        }
        return resNode;
    }
}

赫夫曼樹

public class HuffmanTree {
    public static void main(String[] args) {
        int arr[] = { 13, 7, 8, 3, 29, 6, 1 };
        Node node=createHuffmanTree(arr);

        preOrder(node);

    }
    //編寫一個前序遍歷的方法
    public static void preOrder(Node root) {
        if(root != null) {
            root.preOrder();
        }else{
            System.out.println("是空樹,不能遍歷~~");
        }
    }
    public static Node createHuffmanTree(int[] arr){
        // 第一步爲了操作方便
        // 1. 遍歷 arr 數組
        // 2. 將arr的每個元素構成成一個Node
        // 3. 將Node 放入到ArrayList中
        List<Node> nodes=new ArrayList<>();
        for (int value : arr) {
            nodes.add(new Node(value));
        }

        while (nodes.size()>1){
            Collections.sort(nodes);
            System.out.println("nodes =" + nodes);

            Node leftNode=nodes.get(0);
            Node rightNode=nodes.get(1);

            //(3)構建一顆新的二叉樹
            Node parent = new Node(leftNode.value + rightNode.value);
            parent.left = leftNode;
            parent.right = rightNode;

            //(4)從ArrayList刪除處理過的二叉樹
            nodes.remove(leftNode);
            nodes.remove(rightNode);
            //(5)將parent加入到nodes
            nodes.add(parent);
        }
        return nodes.get(0);
    }
}

// 創建結點類
// 爲了讓Node 對象持續排序Collections集合排序
// 讓Node 實現Comparable接口
class Node implements Comparable<Node> {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
    }
    //寫一個前序遍歷
    public void preOrder() {
        System.out.println(this);
        if(this.left != null) {
            this.left.preOrder();
        }
        if(this.right != null) {
            this.right.preOrder();
        }
    }
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    @Override
    public int compareTo(Node node) {

        return this.value-node.value;
    }
}

霍夫曼編碼

數據壓縮

    private static byte[] huffmanZip(byte[] bytes) {
        List<Node> nodes = getNodes(bytes);
        //根據 nodes 創建的赫夫曼樹
        Node huffmanTreeRoot = createHuffmanTree(nodes);
        //對應的赫夫曼編碼(根據 赫夫曼樹)
        Map<Byte, String> huffmanCodes = getCodes(huffmanTreeRoot);
        //根據生成的赫夫曼編碼,壓縮得到壓縮後的赫夫曼編碼字節數組
        byte[] huffmanCodeBytes = zip(bytes, huffmanCodes);
        return huffmanCodeBytes;
    }

    //編寫一個方法,將字符串對應的byte[] 數組,通過生成的赫夫曼編碼表,返回一個赫夫曼編碼 壓縮後的byte[]
    /**
     *
     * @param bytes 這時原始的字符串對應的 byte[]
     * @param huffmanCodes 生成的赫夫曼編碼map
     * @return 返回赫夫曼編碼處理後的 byte[]
     * 舉例: String content = "i like like like java do you like a java"; =》 byte[] contentBytes = content.getBytes();
     * 返回的是 字符串 "1010100010111111110010001011111111001000101111111100100101001101110001110000011011101000111100101000101111111100110001001010011011100"
     * => 對應的 byte[] huffmanCodeBytes  ,即 8位對應一個 byte,放入到 huffmanCodeBytes
     * huffmanCodeBytes[0] =  10101000(補碼) => byte  [推導  10101000=> 10101000 - 1 => 10100111(反碼)=> 11011000= -88 ]
     * huffmanCodeBytes[1] = -88
     */
    private static byte[] zip(byte[] bytes, Map<Byte, String> huffmanCodes) {

        //1.利用 huffmanCodes 將  bytes 轉成  赫夫曼編碼對應的字符串
        StringBuilder stringBuilder = new StringBuilder();
        //遍歷bytes 數組
        for(byte b: bytes) {
            stringBuilder.append(huffmanCodes.get(b));
        }
        //System.out.println("測試 stringBuilder~~~=" + stringBuilder.toString());
        //將 "1010100010111111110..." 轉成 byte[]

        //統計返回  byte[] huffmanCodeBytes 長度
        //一句話 int len = (stringBuilder.length() + 7) / 8;
        int len;
        if(stringBuilder.length() % 8 == 0) {
            len = stringBuilder.length() / 8;
        } else {
            len = stringBuilder.length() / 8 + 1;
        }
        //創建 存儲壓縮後的 byte數組
        byte[] huffmanCodeBytes = new byte[len];
        int index = 0;//記錄是第幾個byte
        for (int i = 0; i < stringBuilder.length(); i += 8) { //因爲是每8位對應一個byte,所以步長 +8
            String strByte;
            if(i+8 > stringBuilder.length()) {//不夠8位
                strByte = stringBuilder.substring(i);
            }else{
                strByte = stringBuilder.substring(i, i + 8);
            }
            //將strByte 轉成一個byte,放入到 huffmanCodeBytes
            huffmanCodeBytes[index] = (byte)Integer.parseInt(strByte, 2);
            index++;
        }
        return huffmanCodeBytes;
    }
    //生成赫夫曼樹對應的赫夫曼編碼
    //思路:
    //1. 將赫夫曼編碼表存放在 Map<Byte,String> 形式
    //   生成的赫夫曼編碼表{32=01, 97=100, 100=11000, 117=11001, 101=1110, 118=11011, 105=101, 121=11010, 106=0010, 107=1111, 108=000, 111=0011}
    static Map<Byte, String> huffmanCodes = new HashMap<Byte,String>();
    //2. 在生成赫夫曼編碼表示,需要去拼接路徑, 定義一個StringBuilder 存儲某個葉子結點的路徑
    static StringBuilder stringBuilder =new StringBuilder();

    //爲了調用方便,我們重載 getCodes
    private static Map<Byte, String> getCodes(Node root) {
        if(root == null) {
            return null;
        }
        //處理root的左子樹
        getCodes(root.left, "0", stringBuilder);
        //處理root的右子樹
        getCodes(root.right, "1", stringBuilder);
        return huffmanCodes;
    }
    /**
     * 功能:將傳入的node結點的所有葉子結點的赫夫曼編碼得到,並放入到huffmanCodes集合
     * @param node  傳入結點
     * @param code  路徑: 左子結點是 0, 右子結點 1
     * @param stringBuilder 用於拼接路徑
     */
    private static void getCodes(Node node, String code, StringBuilder stringBuilder){
        StringBuilder stringBuilder2 = new StringBuilder(stringBuilder);
        stringBuilder2.append(code);
        if (node!=null){
            if (node.data==null){
                getCodes(node.left, "0", stringBuilder2);
                getCodes(node.right, "1", stringBuilder2);

            }else {
                huffmanCodes.put(node.data,stringBuilder2.toString());
            }
        }
    }

    //前序遍歷的方法
    private static void preOrder(Node root) {
        if(root != null) {
            root.preOrder();
        }else {
            System.out.println("赫夫曼樹爲空");
        }
    }

    //可以通過List 創建對應的赫夫曼樹
    private static Node createHuffmanTree(List<Node> nodes) {

        while(nodes.size() > 1) {
            //排序, 從小到大
            Collections.sort(nodes);
            //取出第一顆最小的二叉樹
            Node leftNode = nodes.get(0);
            //取出第二顆最小的二叉樹
            Node rightNode = nodes.get(1);
            //創建一顆新的二叉樹,它的根節點 沒有data, 只有權值
            Node parent = new Node(null, leftNode.weight + rightNode.weight);
            parent.left = leftNode;
            parent.right = rightNode;

            //將已經處理的兩顆二叉樹從nodes刪除
            nodes.remove(leftNode);
            nodes.remove(rightNode);
            //將新的二叉樹,加入到nodes
            nodes.add(parent);

        }
        //nodes 最後的結點,就是赫夫曼樹的根結點
        return nodes.get(0);

    }

    /**
     *
     * @param bytes 接收字節數組
     * @return 返回的就是 List 形式   [Node[date=97 ,weight = 5], Node[]date=32,weight = 9]......],
     */
    public static List<Node> getNodes(byte[] bytes){
        //1創建一個ArrayList
        ArrayList<Node> nodes = new ArrayList<Node>();
        //遍歷 bytes , 統計 每一個byte出現的次數->map[key,value]
        Map<Byte,Integer> counts=new HashMap<>();
        for (byte b:bytes) {
            Integer count=counts.get(b);
            if (count == null) { // Map還沒有這個字符數據,第一次
                counts.put(b, 1);
            } else {
                counts.put(b, count + 1);
            }
        }
        //把每一個鍵值對轉成一個Node 對象,並加入到nodes集合
        //遍歷map
        for (Map.Entry<Byte,Integer> entry:counts.entrySet()) {
            nodes.add(new Node(entry.getKey(), entry.getValue()));
        }
        return nodes;
    }
class Node implements  Comparable<Node>{
    Byte data; // 存放數據(字符)本身,比如'a' => 97 ' ' => 32
    int weight; //權值, 表示字符出現的次數
    Node left;//
    Node right;

    public Node(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    @Override
    public int compareTo(Node node) {
        return this.weight-node.weight;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", weight=" + weight +
                '}';
    }

    //前序遍歷
    public void preOrder() {
        System.out.println(this);
        if(this.left != null) {
            this.left.preOrder();
        }
        if(this.right != null) {
            this.right.preOrder();
        }
    }
}

數據解壓

    //完成數據的解壓
    //思路
    //1. 將huffmanCodeBytes [-88, -65, -56, -65, -56, -65, -55, 77, -57, 6, -24, -14, -117, -4, -60, -90, 28]
    //   重寫先轉成 赫夫曼編碼對應的二進制的字符串 "1010100010111..."
    //2.  赫夫曼編碼對應的二進制的字符串 "1010100010111..." =》 對照 赫夫曼編碼  =》 "i like like like java do you like a java"
    //編寫一個方法,完成對壓縮數據的解碼
    /**
     *
     * @param huffmanCodes 赫夫曼編碼表 map
     * @param huffmanBytes 赫夫曼編碼得到的字節數組
     * @return 就是原來的字符串對應的數組
     */
    private static byte[] decode(Map<Byte,String> huffmanCodes,byte[] huffmanBytes){
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < huffmanBytes.length; i++) {
            byte b=huffmanBytes[i];
            //判斷是不是最後一個字節
            boolean flag = (i == huffmanBytes.length - 1)&&huffmanBytes[i]>=0;
            stringBuilder.append(byteToBitString(!flag, b));
        }
        //把字符串安裝指定的赫夫曼編碼進行解碼
        //把赫夫曼編碼表進行調換,因爲反向查詢 a->100 100->a
        Map<String, Byte>  map = new HashMap<String,Byte>();
        for(Map.Entry<Byte, String> entry: huffmanCodes.entrySet()) {
            map.put(entry.getValue(), entry.getKey());
        }
        //創建要給集合,存放byte
        List<Byte> list = new ArrayList<>();
        for(int  i = 0; i < stringBuilder.length(); ){
            int count=1;
            boolean flag=true;
            Byte b=null;

            while (flag){
                String key = stringBuilder.substring(i, i+count);//i 不動,讓count移動,指定匹配到一個字符
                b = map.get(key);
                if(b == null) {//說明沒有匹配到
                    count++;
                }else {
                    //匹配到
                    flag = false;
                }
            }
            list.add(b);
            i+=count;
        }
        //當for循環結束後,我們list中就存放了所有的字符  "i like like like java do you like a java"
        //把list 中的數據放入到byte[] 並返回
        byte b[] = new byte[list.size()];
        for(int i = 0;i < b.length; i++) {
            b[i] = list.get(i);
        }
        return b;
    }


    /**
     * 將一個byte 轉成一個二進制的字符串, 如果看不懂,可以參考我講的Java基礎 二進制的原碼,反碼,補碼
     * @param b 傳入的 byte
     * @param flag 標誌是否需要補高位如果是true ,表示需要補高位,如果是false表示不補, 如果是最後一個字節,無需補高位
     * @return 是該b 對應的二進制的字符串,(注意是按補碼返回)
     */
    private static String byteToBitString(boolean flag, byte b) {
        //使用變量保存 b
        int temp = b; //將 b 轉成 int
        //如果是正數我們還存在補高位
        if(flag) {
            temp |= 256; //按位與 256  1 0000 0000  | 0000 0001 => 1 0000 0001
        }
        String str = Integer.toBinaryString(temp); //返回的是temp對應的二進制的補碼
        if(flag) {
            return str.substring(str.length() - 8);
        } else {
            return str;
        }
    }

赫夫曼文件解壓與壓縮

    //編寫一個方法,完成對壓縮文件的解壓
    /**
     *
     * @param zipFile 準備解壓的文件
     * @param dstFile 將文件解壓到哪個路徑
     */
    public static void unZipFile(String zipFile, String dstFile){
        //定義文件輸入流
        InputStream is = null;
        //定義一個對象輸入流
        ObjectInputStream ois = null;
        //定義文件的輸出流
        OutputStream os = null;

        try {
            //創建文件輸入流
            is = new FileInputStream(zipFile);
            //創建一個和  is關聯的對象輸入流
            ois = new ObjectInputStream(is);
            //讀取byte數組  huffmanBytes
            byte[] huffmanBytes = (byte[])ois.readObject();
            //讀取赫夫曼表
            Map<Byte,String> huffmanCodes = (Map<Byte,String>)ois.readObject();
            //解碼
            byte[] bytes = decode(huffmanCodes, huffmanBytes);
            //將bytes 數組寫入到目標文件
            os = new FileOutputStream(dstFile);
            //寫數據到 dstFile 文件
            os.write(bytes);


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //編寫方法,將一個文件進行壓縮
    /**
     *
     * @param srcFile 你傳入的希望壓縮的文件的全路徑
     * @param dstFile 我們壓縮後將壓縮文件放到哪個目錄
     */
    public static void zipFile(String srcFile, String dstFile){
        //創建輸出流
        OutputStream os = null;
        ObjectOutputStream oos = null;
        //創建文件的輸入流
        FileInputStream is = null;

        try {
            //創建文件輸入流
            is=new FileInputStream(srcFile);
            //創建一個和源文件大小一樣的byte[]
            byte[] b = new byte[is.available()];
            //讀取文件
            is.read(b);
            //直接對源文件壓縮
            byte[] huffmanBytes=huffmanZip(b);
            //創建文件的輸出流, 存放壓縮文件
            os=new FileOutputStream(dstFile);
            //創建一個和文件輸出流關聯的ObjectOutputStream
            oos=new ObjectOutputStream(os);
            //把 赫夫曼編碼後的字節數組寫入壓縮文件
            oos.writeObject(huffmanBytes);
            //這裏我們以對象流的方式寫入 赫夫曼編碼,是爲了以後我們恢復源文件時使用
            //注意一定要把赫夫曼編碼 寫入壓縮文件
            oos.writeObject(huffmanCodes);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
                oos.close();

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

    }

二叉排序樹

查找、添加、遍歷、刪除

class BinarySortTree{
    private Node root;

    //查找要刪除的結點
    public Node search(int value) {
        if(root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }
    //查找父結點
    public Node searchParent(int value) {
        if(root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
    //編寫方法:
    //1. 返回的 以node 爲根結點的二叉排序樹的最小結點的值
    //2. 刪除node 爲根結點的二叉排序樹的最小結點
    /**
     *
     * @param node 傳入的結點(當做二叉排序樹的根結點)
     * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
     */
    public int delRightTreeMin(Node node){
        Node target = node;
        while (target.left!=null){
            target=target.left;
        }
        delNode(target.value);
        return target.value;
    }
    //刪除結點
    public void delNode(int value){
        if (root==null)
            return;
        else {
            //1.需求先去找到要刪除的結點  targetNode
            Node targetNode = search(value);
            if(targetNode == null) {
                return;
            }
            if (root.left==null&&root.right==null){
                root=null;
                return;
            }
            Node parent=searchParent(value);
            //如果要刪除的結點是葉子結點
            if (targetNode.left==null&&targetNode.right==null){
                if (parent.left!=null&&parent.left.value==value)
                    parent.left=null;
                else if (parent.right !=null&&parent.right .value==value)
                    parent.right =null;
            }else if(targetNode.left!=null&&targetNode.right!=null){
                int minVal=delRightTreeMin(targetNode.right);
                targetNode.value=minVal;
            }
            else {
                // 刪除只有一顆子樹的結點
                //如果要刪除的結點有左子結點
                if (targetNode.left!=null){
                    if (parent!=null){
                        if (parent.left.value==value)
                            parent.left=targetNode.left;
                        else parent.right=targetNode.left;
                    }else {
                        root=targetNode.left;
                    }

                }else {
                    if (parent!=null){
                        if (parent.left.value==value)
                            parent.left=targetNode.right;
                        else parent.right=targetNode.right;
                    }
                    else {
                        root=targetNode.right;
                    }
                }
            }
        }
    }

    public void add(Node node){
        if (root==null)
            root=node;
        else
            root.add(node);
    }
    public void infixOder(){
        if (root!=null)
            root.infixOder();
        else
            System.out.println("二叉排序樹爲空,無法遍歷");
    }
}
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public Node search(int value){
        if (value==this.value)
            return this;
        else if(value<this.value){
            if (this.left==null)
                return null;
            return this.left.search(value);
            }else{
            if (this.right==null)
                return null;
            return this.right.search(value);
        }
    }
    //查找要刪除結點的父結點
    /**
     * @param value 要找到的結點的值
     * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
     */
    public Node searchParent(int value){
        if((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        }else {
            if (value<this.value&&this.left!=null)
                return this.left.searchParent(value);
            else if (value>=this.value&&this.right!=null)
                return this.right.searchParent(value);
            else
                return null;
        }
    }
    //添加結點的方法
    //遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
    public void add(Node node){
        if (node==null){
            return;
        }

        //判斷傳入的結點的值,和當前子樹的根結點的值關係
        if (node.value<this.value){
            if (this.left==null)
                this.left=node;
            else
                this.left.add(node);
        }else {
            if (this.right==null)
                this.right=node;
            else
                this.right.add(node);
        }
    }
    public void infixOder(){
        if (this.left!=null)
            this.left.infixOder();
        System.out.println(this);
        if (this.right!=null)
            this.right.infixOder();
    }
}

平衡二叉樹

在加入節點的時候,考慮左旋轉、右旋轉

class AVLTree{
    private Node root;
    public Node getRoot() {
        return root;
    }
    public void add(Node node){
        if (root==null)
            root=node;
        else
            root.add(node);
    }
    // 查找要刪除的結點
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }
    // 查找父結點
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
    // 編寫方法:
    // 1. 返回的 以node 爲根結點的二叉排序樹的最小結點的值
    // 2. 刪除node 爲根結點的二叉排序樹的最小結點
    /**
     *
     * @param node
     *            傳入的結點(當做二叉排序樹的根結點)
     * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        // 循環的查找左子節點,就會找到最小值
        while (target.left != null) {
            target = target.left;
        }
        // 這時 target就指向了最小結點
        // 刪除最小結點
        delNode(target.value);
        return target.value;
    }

    // 刪除結點
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            // 1.需求先去找到要刪除的結點 targetNode
            Node targetNode = search(value);
            // 如果沒有找到要刪除的結點
            if (targetNode == null) {
                return;
            }
            // 如果我們發現當前這顆二叉排序樹只有一個結點
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }

            // 去找到targetNode的父結點
            Node parent = searchParent(value);
            // 如果要刪除的結點是葉子結點
            if (targetNode.left == null && targetNode.right == null) {
                // 判斷targetNode 是父結點的左子結點,還是右子結點
                if (parent.left != null && parent.left.value == value) { // 是左子結點
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {// 是由子結點
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { // 刪除有兩顆子樹的節點
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;

            } else { // 刪除只有一顆子樹的結點
                // 如果要刪除的結點有左子結點
                if (targetNode.left != null) {
                    if (parent != null) {
                        // 如果 targetNode 是 parent 的左子結點
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else { // targetNode 是 parent 的右子結點
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else { // 如果要刪除的結點有右子結點
                    if (parent != null) {
                        // 如果 targetNode 是 parent 的左子結點
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else { // 如果 targetNode 是 parent 的右子結點
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }

            }

        }
    }


    // 中序遍歷
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序樹爲空,不能遍歷");
        }
    }
}
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //左旋轉方法
    private void leftRotate(){
        //創建新的結點,以當前根結點的值
        Node newNode = new Node(value);
        //把新的結點的左子樹設置成當前結點的左子樹
        newNode.left = left;
        //把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
        newNode.right = right.left;
        //把當前結點的值替換成右子結點的值
        value = right.value;
        //把當前結點的右子樹設置成當前結點右子樹的右子樹
        right = right.right;
        //把當前結點的左子樹(左子結點)設置成新的結點
        left = newNode;
    }
    //右旋轉
    private void rightRotate() {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }
    // 返回左子樹的高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    // 返回右子樹的高度
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }
    // 返回 以該結點爲根結點的樹的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    // 中序遍歷
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    // 查找要刪除的結點
    /**
     *
     * @param value
     *            希望刪除的結點的值
     * @return 如果找到返回該結點,否則返回null
     */
    public Node search(int value) {
        if (value == this.value) { // 找到就是該結點
            return this;
        } else if (value < this.value) {// 如果查找的值小於當前結點,向左子樹遞歸查找
            // 如果左子結點爲空
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else { // 如果查找的值不小於當前結點,向右子樹遞歸查找
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }

    }
    //添加結點的方法
    //遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
    public void add(Node node){
        if (node==null){
            return;
        }

        //判斷傳入的結點的值,和當前子樹的根結點的值關係
        if (node.value<this.value){
            if (this.left==null)
                this.left=node;
            else
                this.left.add(node);
        }else {
            if (this.right==null)
                this.right=node;
            else
                this.right.add(node);
        }

        //當添加完一個結點後,如果: (右子樹的高度-左子樹的高度) > 1 , 左旋轉
        if(rightHeight() - leftHeight() > 1){
            //如果它的右子樹的左子樹的高度大於它的右子樹的右子樹的高度
            if(right != null && right.leftHeight() > right.rightHeight()){
                //先對右子結點進行右旋轉
                right.rightRotate();
                //然後在對當前結點進行左旋轉
                leftRotate(); //左旋轉..
            }else {
                //直接進行左旋轉即可
                leftRotate();
            }
            return;
        }
        //當添加完一個結點後,如果 (左子樹的高度 - 右子樹的高度) > 1, 右旋轉
        if(leftHeight() - rightHeight() > 1) {
            //如果它的左子樹的右子樹高度大於它的左子樹的高度
            if(left != null && left.rightHeight() > left.leftHeight()) {
                //先對當前結點的左結點(左子樹)->左旋轉
                left.leftRotate();
                //再對當前結點進行右旋轉
                rightRotate();
            } else {
                //直接進行右旋轉即可
                rightRotate();
            }
        }
    }

    // 查找要刪除結點的父結點
    /**
     *
     * @param value
     *            要找到的結點的值
     * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
     */
    public Node searchParent(int value) {
        // 如果當前結點就是要刪除的結點的父結點,就返回
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            // 如果查找的值小於當前結點的值, 並且當前結點的左子結點不爲空
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value); // 向左子樹遞歸查找
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value); // 向右子樹遞歸查找
            } else {
                return null; // 沒有找到父結點
            }
        }

    }

}

圖的構建 廣度遍歷 深度遍歷

package tree.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * @project_name: DateConstruct
 * @description:
 * @author: ZZW
 * @time: 2019/12/27 12:49
 */
public class Graph {
    private ArrayList<String> vertexList; //存儲頂點集合
    private int[][] edges; //存儲圖對應的鄰結矩陣
    private int numOfEdges; //表示邊的數目
    //定義給數組boolean[], 記錄某個結點是否被訪問
    private boolean[] isVisited;
    public static void main(String[] args) {
//測試一把圖是否創建ok
        int n = 8;  //結點的個數
        //String Vertexs[] = {"A", "B", "C", "D", "E"};
        String Vertexs[] = {"1", "2", "3", "4", "5", "6", "7", "8"};

        //創建圖對象
        Graph graph = new Graph(n);
        //循環的添加頂點
        for(String vertex: Vertexs) {
            graph.insertVertex(vertex);
        }
//        添加邊
//        A-B A-C B-C B-D B-E
//		graph.insertEdge(0, 1, 1); // A-B
//		graph.insertEdge(0, 2, 1); //
//		graph.insertEdge(1, 2, 1); //
//		graph.insertEdge(1, 3, 1); //
//		graph.insertEdge(1, 4, 1); //


        //更新邊的關係
        graph.insertEdge(0, 1, 1);
        graph.insertEdge(0, 2, 1);
        graph.insertEdge(1, 3, 1);
        graph.insertEdge(1, 4, 1);
        graph.insertEdge(3, 7, 1);
        graph.insertEdge(4, 7, 1);
        graph.insertEdge(2, 5, 1);
        graph.insertEdge(2, 6, 1);
        graph.insertEdge(5, 6, 1);

        //顯示一把鄰結矩陣
        graph.showGraph();

        //測試一把,我們的dfs遍歷是否ok
        System.out.println("深度遍歷");
        graph.dfs(); // A->B->C->D->E [1->2->4->8->5->3->6->7]
//		System.out.println();

        System.out.println("廣度優先!");
        graph.bfs(); // A->B->C->D-E [1->2->3->4->5->6->7->8]



    }
    //構造器
    public Graph(int n) {
        //初始化矩陣和vertexList
        edges = new int[n][n];
        vertexList = new ArrayList<String>(n);
        numOfEdges = 0;
    }


    //得到第一個鄰接結點的下標 w
    /**
     *
     * @param index
     * @return 如果存在就返回對應的下標,否則返回-1
     */
    public int getFirstNeighbor(int index) {
        for(int j = 0; j < vertexList.size(); j++) {
            if(edges[index][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    //根據前一個鄰接結點的下標來獲取下一個鄰接結點
    public int getNextNeighbor(int v1, int v2) {
        for(int j = v2 + 1; j < vertexList.size(); j++) {
            if(edges[v1][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    //深度優先遍歷算法
    //i 第一次就是 0
    private void dfs(boolean[] isVisited, int i) {
        //首先我們訪問該結點,輸出
        System.out.print(getValueByIndex(i) + "->");
        //將結點設置爲已經訪問
        isVisited[i] = true;
        //查找結點i的第一個鄰接結點w
        int w = getFirstNeighbor(i);
        while(w != -1) {//說明有
            if(!isVisited[w]) {
                dfs(isVisited, w);
            }
            //如果w結點已經被訪問過
            w = getNextNeighbor(i, w);
        }

    }

    //對dfs 進行一個重載, 遍歷我們所有的結點,並進行 dfs
    public void dfs() {
        isVisited = new boolean[vertexList.size()];
        //遍歷所有的結點,進行dfs[回溯]
        for(int i = 0; i < getNumOfVertex(); i++) {
            if(!isVisited[i]) {
                dfs(isVisited, i);
            }
        }
    }

    //對一個結點進行廣度優先遍歷的方法
    private void bfs(boolean[] isVisited, int i) {
        int u ; // 表示隊列的頭結點對應下標
        int w ; // 鄰接結點w
        //隊列,記錄結點訪問的順序
        LinkedList queue = new LinkedList();
        //訪問結點,輸出結點信息
        System.out.print(getValueByIndex(i) + "=>");
        //標記爲已訪問
        isVisited[i] = true;
        //將結點加入隊列
        queue.addLast(i);

        while( !queue.isEmpty()) {
            //取出隊列的頭結點下標
            u = (Integer)queue.removeFirst();
            //得到第一個鄰接結點的下標 w
            w = getFirstNeighbor(u);
            while(w != -1) {//找到
                //是否訪問過
                if(!isVisited[w]) {
                    System.out.print(getValueByIndex(w) + "=>");
                    //標記已經訪問
                    isVisited[w] = true;
                    //入隊
                    queue.addLast(w);
                }
                //以u爲前驅點,找w後面的下一個鄰結點
                w = getNextNeighbor(u, w); //體現出我們的廣度優先
            }
        }

    }

    //遍歷所有的結點,都進行廣度優先搜索
    public void bfs() {
        isVisited = new boolean[vertexList.size()];
        for(int i = 0; i < getNumOfVertex(); i++) {
            if(!isVisited[i]) {
                bfs(isVisited, i);
            }
        }
    }

    //圖中常用的方法
    //返回結點的個數
    public int getNumOfVertex() {
        return vertexList.size();
    }
    //顯示圖對應的矩陣
    public void showGraph() {
        for(int[] link : edges) {
            System.err.println(Arrays.toString(link));
        }
    }
    //得到邊的數目
    public int getNumOfEdges() {
        return numOfEdges;
    }
    //返回結點i(下標)對應的數據 0->"A" 1->"B" 2->"C"
    public String getValueByIndex(int i) {
        return vertexList.get(i);
    }
    //返回v1和v2的權值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //插入結點
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }
    /**
     *
     * @param v1 表示點的下標即使第幾個頂點  "A"-"B" "A"->0 "B"->1
     * @param v2 第二個頂點對應的下標
     * @param weight 表示
     */
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
}

 

發佈了8 篇原創文章 · 獲贊 3 · 訪問量 4209
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章