Data Structures and Algorithms (binary)

First language

The establishment of a binary tree

Ordinary binary treeExtended Binary Tree

Code

 /**
     * 通过前序遍历的数据序列反向生成二叉树
     *           A
     *      B          C
     *   D      E   #      F
     * #   #  #   #     #     #
     * <p>
     * ABD##E##C#F##
     */
    public void createBinaryTreePre(ArrayList<String> data){
        createBinaryTree(data.size(),data);
    }
    public TreeNode createBinaryTree(int size, ArrayList<String> data) {
        if (data.size() == 0) {
            return null;
        }
        String d = data.get(0);
        TreeNode node;
        int index = size - data.size();
        if (d.equals("#")) {
            node = null;
            data.remove(0);
            return node;
        }
        node = new TreeNode(index, d);
        if (index == 0) {
            //创建根节点
            root = node;
        }
        data.remove(0);
        node.leftChild = createBinaryTree(size, data);
        node.rightChild = createBinaryTree(size, data);
        return node;

    }

Tree into a binary tree

  • 1, plus line. Plus a connection between all the sibling nodes.
  • 2, to the wire. Each node in the tree, leaving only its connection with the first child node, delete the connection between it and the other child nodes.
  • 3, level adjustment. To the root of the tree as the axis, the rotation of the whole tree a certain angle clockwise, so that the structure structured. Note that the first child is left child node of a binary tree, brothers conversion over children is the right child node.
    Tree into a binary tree

Forest conversion binary tree

  • 1, to convert each binary tree.
  • 2, fixed on a binary tree, starting from the second binary tree, the binary tree sequentially to the right child of the root node as a root node of a binary tree before, with the connecting lines. When after all the binary tree connected by the forest has been converted to a binary tree.
    Forest conversion binary tree

Convert binary tree

  • 1, plus line. If the left child node of a node exists, it is the left child of the right child node, right child of the right child node, right child of the right child of the right child node ............ Ha, anyway, It is the left child of n.
  • 2, to the wire. Delete the original binary tree nodes to connect all of its right child nodes.
  • 3, level adjustment. So that structure structured.
    Convert binary tree

Binary tree forest conversion

  • Determining a binary tree can be converted into a tree or forest, the standard is very simple, it is just by looking tree binary tree root has no right child, there is the forest, not a tree is. So if it is the conversion of forest, as follows:
    1, start from the root, if the right child exists, the connection put the child node and the right to delete, and then view the binary tree after the separation, if the right child exists, then delete the connection ········ until all children the right connections are deleted so far, been the separation of a binary tree.
    2, each of them then the separated binary tree can be converted to.
    Binary tree forest conversion

Huffman tree

  • Uncle Huffman said configuration path between two nodes from the tree to a node between the other branch nodes, the number of branches on path is called the path length. In a binary tree, the root node to the node D path length is 4, the binary tree root node to the node b, D a path length of 2. Is the path length from the root of the tree to each node, and the path length. A binary tree on a path length of 1 + 1 + 2 + 3 + 2 + 3 + 4 + 4 = 20. B binary tree on the path length of 1 + 3 + 3 + 2 + 2 + 2 + 2 + 1 = 16.
  • Considering junction weighted, weighted path length is the product of node weights from that node to the path length between the root and the node. Weighted path length tree tree is weighted in the path length and all leaf nodes. Suppose there are n weights {w₁, w₂, w₃, ····, wn}, there is configured a binary tree of n leaf nodes, each leaf node with weights wk, each leaf of the path length lk we generally denoted, wherein the weighted minimum path length WPL called binary Huffman tree. For example as follows:

  • We simplify this first binary two leaf nodes into binary weighted, wherein A represents failed, B represents pass, C represents moderate, D denotes good, E is excellent. Digital branch line of each leaf is we have just mentioned five-point scale score accounted logarithmic scale.
  • A binary tree of WPL = 5 × 1 + 15 × 2 + 40 × 3 + 30 × 4 + 10 × 4 = 315
  • The binary tree b WPL = 5 × 3 + 15 × 3 + 40 × 2 + 30 × 2 + 10 × 2 = 220

Huffman tree structure

  • 1, first leaf nodes in accordance with the right values ​​arranged in ascending order of an ordered sequence, i.e. A5, E10, B15, D30, C40.
  • 2, take the first two junction minimum weight as a new node of the two child nodes N₁, note left child is relatively small, this is the left child N1 is A, E is the right child of N1, as FIG. 6-12-5, the new right node is a leaf of the two weights and 5 + 10 = 15.
  • 3, replacing A with the N₁ E, ordered sequence is inserted, held in ascending order. That N₁15, B15, D30, C40.
  • 4. Repeat step 2. The B N₁ and N₂ as a new node of the two child nodes. 6-12-6 shown in FIG. Weights N₂ = 15 + 15 = 30.
  • 5, replacing the N₂ and N₁ B, ordered sequence insertion, holding in ascending order. That N₂30, D30, C40.
  • 6. Repeat step 2. The N₂ D with a new node N₃ as the two child nodes. 6-12-7 shown in FIG. N₃ weights = 30 + 30 = 60.
  • 7, replacing the N₂ and N₃ D, ordered sequence is inserted, held in ascending order. Namely: C40, N₃60.
  • 8. Repeat step 2. The C and N₃ as a new node T of the two sub-nodes, as shown in FIG 6-12-8. Since that is the root of T, to complete the Huffman tree structure.

Huffman tree construction process

  • Just by step, we can come to construct Huffman tree Huffman algorithm description.
  • 1、根据给定的n个权值{w₁,w₂,····,wn}构成n棵二叉树的集合F={T₁,T₂,·····,Tn},其中每棵二叉树T₁中只有一个带权为w₁根结点,其左右子树均为空。
  • 2、在F中选取两棵根结点的权值最小的树作为左右子树构造一棵新的二叉树,且置新的二叉树的根结点的权值为其左右子树上根结点的权值之和。
  • 3、在F中删除这两棵树,同时将新得到的二叉树加入F中。
  • 4、重复2和3步骤,知道F只含一棵树为止,这棵树便是赫夫曼树。

赫夫曼编码

  • 当然,赫夫曼研究这种最优树的目的不是为了我们可以转化一下成绩。 他的更大目的是为了解决当年远距离通信(主要是电报)的数据传输的最优化问题。
  • 比如我们有一-段文字内容为“ BADCADFEED”要网络传输给别人,显然用二进制的数字(0和1)来表示是很自然的想法。我们现在这段文字只有六个字母ABCDEF,那么我们可以用相应的二进制数据表示,如图6-12-2所示。
  • 假设六个字母的频率为A27,B8,C15,D15,E30,F5,合起来正好是100%。那就意味着,我们完全可以重新按照赫夫曼树来规划它们。
  • 图6-12-9 左图为构造赫夫曼树的过程的权值显示。右图为将权值左分支改为0,右分支改为1后的赫夫曼树。

  • 此时,我们对这六个字母用其从树根到叶子所经过路径的0或1来编码,可以得到如表6-12-3所示这样的定义。
  • 我们将文字内容为“BADCADFEED”再次编码,对比可以看到结果串变小了。
  • ■原编码二进制串 : 00100001101000011101100100011 (共 30个字符)
  • ■新编码二进制串: 100101001010000111100 (共25个字符)
  • 也就是说,我们的数据被压缩了,节约了大约17%的存储或传输成本。

二叉查找树

  • Binary search tree (Binary Search Tree), (Also: a binary search tree, binary sort tree) which is either an empty tree or a binary tree with the following properties: if its left subtree is not empty, then left values ​​of all the sub-tree nodes are less than the value of the root node; if its right subtree is not empty, then the right sub-tree, all the nodes which are greater than the value of the root; its left , they were also right subtree binary sort tree.

Code

/**
     * 创建查找二叉树,添加结点
     *
     * @param data
     * @return
     */
    public TreeNode put(int data) {
        TreeNode node = null;
        TreeNode parent = null;
        if (root == null) {
            node = new TreeNode(0, data);
            root = node;
            root = node;
        }
        node = root;
        while (node != null) {
            parent = node;
            if (data > node.data) {
                node = node.rightChild;
            } else if (data < node.data) {
                node = node.leftChild;
            } else {
                return node;
            }
        }
        //表示将此结点添加到相应位置
        node = new TreeNode(0, data);
        if (data < parent.data) {
            parent.leftChild = node;
        } else {
            parent.rightChild = node;
        }
        node.parent = parent;
        return node;
    }
 /**
     * 中序遍历
     * @param node
     */
    public void midOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            midOrder(node.leftChild);
            System.out.println(node.data);
            midOrder(node.rightChild);
        }
    }

Binary tree node deletion

  • Is divided into four cases, no left and right subtrees node (leaf node); no left subtree node; no right subtree of nodes; node has left and right subtrees.
  • 1 to remove the node is a leaf node: delete.
    Leaf node
  • 2, delete the sub-tree node exists only left or right subtree: delete the parent node directly to the sub-tree node.
  • 3, delete nodes exist left subtree and right subtree: will replace the deleted node node leftmost rightmost node in the left subtree delete nodes or right subtree, replace and delete nodes, and then delete nodes to sub-tree node.

Code

//删除节点
    public void Delete(DeleteBinaryTree root, int value) {
        DeleteBinaryTree temp;
        temp = Select(root, value);
        if (temp.value == 0) {
            System.out.println("你要删除的数值" + value + "不存在");
        } else {
            DeleteBinaryTree p = new DeleteBinaryTree();
            DeleteBinaryTree node;
            p = p.Select(root, value);    //p是要删除的结点
            node = p;
            if (p != null) {
                if (p.left != null) {
                    p = node.left;
                    while (p.right != null) {
                        p = p.right;
                    }
                    node.value = p.value;
                    if (node.left.right == null) {
                        node.left = p.left;
                    } else {
                        p.parent.right = p.left;
                    }
                } else if (p.right != null) {
                    p = p.right;
                    node.value = p.value;
                    node.left = p.left;
                    node.right = p.right;
                } else {
                    if (p.equals(p.parent.left)) {
                        p.parent.left = null;
                    }
                    if (p.equals(p.parent.right)) {
                        p.parent.right = null;
                    }
                }
                System.out.println("");
                System.out.println("数据" + value + "删除成功");
            }
        }
    }

Modified binary node

//修改节点
    public void Update(DeleteBinaryTree root, int value, int update) {
        DeleteBinaryTree temp = new DeleteBinaryTree();
        temp = temp.Select(root, value);
        if (temp.value == value) {
            Delete(root, value);
            DeleteBinaryTree a = new DeleteBinaryTree(update);
            insert(root, a);
            System.out.println("数据" + value + "成功更新成" + update);
        } else {
            System.out.println("数据" + value + "更新失败");
        }

    }
Published 16 original articles · won praise 65 · views 30000 +

Guess you like

Origin blog.csdn.net/yang_study_first/article/details/104185986