树与哈夫曼树

package tree;   
  
public class TreeNode {   
    TreeNode llink;   
    TreeNode rlink;   
    int info;   
}  
package tree;   
  
public class Tree {   
  
    TreeNode root;   
  
    public Tree() {   
    }   
  
    public boolean isEmpty() {   
        return root == null;   
    }   
  
    // 插入   
    public void insertBinaryTree(int info) {   
        TreeNode node = new TreeNode();   
        node.info = info;   
        if (root == null) {   
            root = node;   
        } else {   
            TreeNode currentNode = root;   
            TreeNode parent;   
            while (currentNode != null) {   
                parent = currentNode;   
                if (info > currentNode.info) {   
                    currentNode = currentNode.rlink;   
                    if (currentNode == null) {   
                        parent.rlink = node;   
                    }   
                } else if (info < currentNode.info) {   
                    currentNode = currentNode.llink;   
                    if (currentNode == null) {   
                        parent.llink = node;   
                    }   
                }   
            }   
        }   
    }   
  
    // 删除   
    public void treeDelete(int info) {   
        TreeNode tNode = serach(info);   
        TreeNode deleteNode = new TreeNode();   
        TreeNode dNodeChild = new TreeNode();   
        if (tNode == null) {   
            System.out.println("node is not exists");   
        } else if (tNode.llink == null || tNode.rlink == null) {   
            deleteNode = tNode;   
        } else {   
            deleteNode = treeSuccessor(tNode);   
        }   
        if (deleteNode.llink != null) {   
            dNodeChild = deleteNode.llink;   
        } else {   
            dNodeChild = deleteNode.rlink;   
        }   
        if (getFatherNode(deleteNode) == null) {   
            root = dNodeChild;   
        } else {   
            if (deleteNode == getFatherNode(deleteNode).llink) {   
                getFatherNode(deleteNode).llink = dNodeChild;   
            } else {   
                getFatherNode(deleteNode).rlink = dNodeChild;   
            }   
        }   
        if (deleteNode != tNode) {   
            tNode.info = deleteNode.info;   
        }   
    }   
  
    // 搜索   
    public TreeNode serach(int info) {   
        return treeSerach(root, info);   
    }   
  
    // 搜索   
    public TreeNode treeSerach(TreeNode tNode, int info) {   
        if (tNode == null || tNode.info == info) {   
            return tNode;   
        }   
        if (info < tNode.info) {   
            return treeSerach(tNode.llink, info);   
        } else {   
            return treeSerach(tNode.rlink, info);   
        }   
    }   
  
    // 最大节点   
    public TreeNode treeMaxiMum(TreeNode tNode) {   
        while (tNode.rlink != null) {   
            tNode = tNode.rlink;   
        }   
        return tNode;   
    }   
  
    // 最小节点   
    public TreeNode treeMiniMun(TreeNode tNode) {   
        while (tNode.llink != null) {   
            tNode = tNode.llink;   
        }   
        return tNode;   
    }   
  
    // 找后继   
    public TreeNode treeSuccessor(TreeNode tNode) {   
        if (tNode.rlink != null) {   
            return treeMiniMun(tNode.rlink);   
        }   
        TreeNode currentNode = getFatherNode(tNode);   
        while (currentNode != null && tNode == currentNode.rlink) {   
            tNode = currentNode;   
            currentNode = getFatherNode(tNode);   
        }   
        return currentNode;   
    }   
  
    // 找前驱   
    public TreeNode treePrecursor(TreeNode tNode) {   
        if (tNode.llink != null) {   
            return treeMaxiMum(tNode.llink);   
        }   
        TreeNode currentNode = getFatherNode(tNode);   
        while (currentNode != null && tNode == currentNode.llink) {   
            tNode = currentNode;   
            currentNode = getFatherNode(tNode);   
        }   
        return currentNode;   
    }   
  
    // 找节点的父节点   
    public TreeNode getFatherNode(TreeNode tNode) {   
        TreeNode current = root;   
        TreeNode trailCurrent = null;   
        while (current != null) {   
            if (current.info == tNode.info) {   
                break;   
            }   
            trailCurrent = current;   
            if (tNode.info < current.info) {   
                current = current.llink;   
            } else {   
                current = current.rlink;   
            }   
        }   
        return trailCurrent;   
    }   
  
    // 中序遍历   
    public void inOrder(TreeNode tNode) {   
        if (tNode != null) {   
            inOrder(tNode.llink);   
            System.out.print(tNode.info + " ");   
            inOrder(tNode.rlink);   
        }   
    }   
  
    // 打印树   
    public void printTree() {   
        System.out.println("inOrder");   
        inOrder(root);   
        System.out.println();   
        System.out.println("preOrder");   
        preOrder(root);   
        System.out.println();   
        System.out.println("postOrder");   
        postOrder(root);   
        System.out.println();   
    }   
  
    // 前序遍历   
    public void preOrder(TreeNode tNode) {   
        if (tNode != null) {   
            System.out.print(tNode.info + " ");   
            preOrder(tNode.llink);   
            preOrder(tNode.rlink);   
        }   
    }   
  
    // 后序遍历   
    public void postOrder(TreeNode tNode) {   
        if (tNode != null) {   
            postOrder(tNode.llink);   
            postOrder(tNode.rlink);   
            System.out.print(tNode.info + " ");   
        }   
    }   
  
    public static void main(String[] args) {   
        Tree tree = new Tree();   
        System.out.println("insert tree start");   
        tree.insertBinaryTree(15);   
        tree.insertBinaryTree(5);   
        tree.insertBinaryTree(16);   
        tree.insertBinaryTree(3);   
        tree.insertBinaryTree(12);   
        tree.insertBinaryTree(20);   
        tree.insertBinaryTree(10);   
        tree.insertBinaryTree(13);   
        tree.insertBinaryTree(18);   
        tree.insertBinaryTree(23);   
        tree.insertBinaryTree(6);   
        tree.insertBinaryTree(7);   
        System.out.println("insert tree end");   
        System.out.println("print tree start");   
        tree.treeDelete(15);   
        tree.printTree();   
        System.out.println("print tree end");   
  
    }   
}     


哈夫曼树
package tree;   
  
/**  
 * @author B.Chen  
 */  
public class HuffmanTree {   
  
    /**  
     * 根节点  
     */  
    public TreeNode root;   
  
    /**  
     * 数组下表  
     */  
    public int nodeSize;   
  
    /**  
     * 长度  
     */  
    public int length;   
  
    /**  
     * 哈夫曼节点数组  
     */  
    public TreeNode[] nodeList;   
       
    /**  
     * 访问控制  
     */  
    public boolean[] visited;   
  
    /**  
     * @param length  
     */  
    public HuffmanTree(int length) {   
        this.length = length;   
        nodeList = new TreeNode[2 * length-1];   
        visited = new boolean[2*length-1];   
    }   
  
    /**  
     * @param info  
     */  
    public void addNode(int info) {   
        TreeNode tNode = new TreeNode();   
        tNode.info = info;   
        if (nodeSize < length) {   
            nodeList[nodeSize++] = tNode;   
        } else {   
            System.out.println("out of size");   
        }   
    }   
       
    /**  
     * 构造哈夫曼树  
     */  
    public void createHuffmanTree() {   
        int j = length;   
        while (nodeList[2*length -2] == null) {   
            TreeNode lNode = getMiniMumNode();   
            TreeNode rNode = getMiniMumNode();   
            TreeNode tNode = new TreeNode();   
            System.out.println(lNode.info + " " + rNode.info);   
            tNode.llink = lNode;   
            tNode.rlink = rNode;   
            tNode.info = lNode.info + rNode.info;   
            nodeList[j++] = tNode;   
        }   
        root = nodeList[2 * length - 2];   
    }   
       
    /**  
     * @return TreeNode  
     */  
    public TreeNode getMiniMumNode() {   
        TreeNode tNode = null;   
        int size = 0;   
        for(int i=0;i<2*length-1;i++) {   
            if(!visited[i] && nodeList[i] != null) {   
                tNode = nodeList[i];   
                size = i;   
                for(int j=0;j<2*length-1;j++) {   
                    if(!visited[j] && nodeList[j] != null) {   
                        if(tNode.info > nodeList[j].info) {   
                            tNode = nodeList[j];   
                            size = j;   
                        }   
                    }   
                }   
            }   
        }   
        visited[size] = true;   
        return tNode;   
    }   
       
    /**  
     * 中序遍历  
     *   
     * @param tNode  
     */  
    public void inOrder(TreeNode tNode) {   
        if (tNode != null) {   
            inOrder(tNode.llink);   
            System.out.print(tNode.info + " ");   
            inOrder(tNode.rlink);   
        }   
    }   
       
    /**  
     * 打印  
     */  
    public void printHuffmanTree() {   
        System.out.println("inOrder");   
        inOrder(root);   
    }   
       
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        HuffmanTree hTree = new HuffmanTree(6);   
        hTree.addNode(4);   
        hTree.addNode(4);   
        hTree.addNode(4);   
        hTree.addNode(4);   
        hTree.addNode(5);   
        hTree.addNode(6);   
        hTree.createHuffmanTree();   
        hTree.printHuffmanTree();   
    }   
}  

你可能感兴趣的:(J#)