树的Java实现

用Java实现树的基本遍历操作,包括递归和非递归方式:

Tree结点类:
package dataStruct;

/**
 * 树的结点
 * @author WQC
 *
 */
public class TreeNode{
    public int value;
    public TreeNode leftChild;
    public TreeNode rightChild;
    public TreeNode() {
    }

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

    public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
        super();
        this.value = value;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }


}
Tree实现类:
package dataStruct;

import java.util.Scanner;
import java.util.Stack;

public class Tree {

    /**
     * 前序遍历二叉树--递归
     * 
     * @param tree
     * @param function
     * @return
     */
    public static boolean preOrderTree(TreeNode tree, Function function) {

        if (function == null) {
            return false;
        }
        if (tree != null) {
            if (function.visit(tree)) {
                if (preOrderTree(tree.leftChild, function)) {
                    if (preOrderTree(tree.rightChild, function)) {
                        return true;
                    }
                }
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 中序遍历二叉树--递归
     * 
     * @param tree
     * @param function
     * @return
     */
    public static boolean inOrderTree(TreeNode tree, Function function) {
        if (function == null) {
            return false;
        }
        // 检查结点是否为空
        if (tree != null) {
            // 中序遍历左子树
            if (inOrderTree(tree.leftChild, function)) {
                // 打印根节点
                if (function.visit(tree)) {
                    // 中序遍历右子树
                    if (inOrderTree(tree.rightChild, function)) {
                        return true;
                    } else {
                        function.visit(tree);
                    }
                }
            } else {
                function.visit(tree);
                return false;
            }
        }
        return true;
    }

    /**
     * 后续遍历树--递归
     * 
     * @param treeRoot
     * @param function
     */
    public static boolean postOrderTree(TreeNode treeRoot, Function function) {
        if (function == null) {
            return false;
        }

        // 检查结点是否为空
        if (treeRoot != null) {
            // 后续遍历左子树
            if (postOrderTree(treeRoot.leftChild, function)) {
                //后续遍历右子树
                if (postOrderTree(treeRoot.rightChild, function)) {
                    if (function.visit(treeRoot)) {
                        return true;
                    }
                }else{
                    function.visit(treeRoot);
                    return true;
                }

            } else {
                function.visit(treeRoot);
                return false;
            }
        }
        return true;

    }

    /**
     * 中序遍历--非递归
     * @param root
     * @param function
     * @return
     */
    public static boolean inOrderNoRecursion(TreeNode root,Function function){

        if(root==null || function == null){
            return false;
        }

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);

        while(!stack.isEmpty()){
            //向左走到尽头
            while(stack.peek()!=null ){
                stack.push(root.leftChild);
                root = root.leftChild;
            }
            //空指针退栈
            root = stack.pop();
            if(!stack.isEmpty()){
                root = stack.pop();
                if(!function.visit(root)){
                    return false;
                }
                root = root.rightChild;
                stack.push(root.rightChild);
            }
        }
        return true;
    }


    /**
     * 先序遍历创建一棵树
     * 
     * @return
     */
    public static TreeNode createTreeByPre() {
        TreeNode node;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入节点值");
        String dataStr = scanner.next();
        if (dataStr.equals("$")) {
            node = null;
        } else {
            node = new TreeNode();
            int data = Integer.valueOf(dataStr);
            node.value = data;
            node.leftChild = createTreeByPre();
            node.rightChild = createTreeByPre();
        }

        return node;

    }

    /**
     * 对树的结点的操作接口
     * @author WQC
     *
     */
    public interface Function {
        public boolean visit(TreeNode treeNode);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈夫曼是一种带权路径长度最短的,常用于数据压缩。在Java中,可以通过构建哈夫曼实现哈夫曼编码。下面是一个简单的哈夫曼Java实现的步骤: 1. 定义一个Node类作为哈夫曼的节点,包含权值和左右子节点等属性。 2. 构建哈夫曼,可以通过优先队列(PriorityQueue)来实现。首先将所有节点加入队列中,然后每次取出权值最小的两个节点,合并成一个新节点,再将新节点加入队列中,直到队列中只剩下一个节点,即为哈夫曼的根节点。 3. 根据哈夫曼生成哈夫曼编码,可以通过递归遍历哈夫曼实现。从根节点开始遍历,每次向左走为0,向右走为1,直到叶子节点,即可得到该节点对应的哈夫曼编码。 下面是一个简单的Java代码实现: ```java public class HuffmanTree { private Node root; private class Node implements Comparable<Node> { int weight; Node left; Node right; public Node(int weight) { this.weight = weight; } public int compareTo(Node other) { return this.weight - other.weight; } public boolean isLeaf() { return left == null && right == null; } public String toString() { return "Node(" + weight + ")"; } } public HuffmanTree(int[] weights) { PriorityQueue<Node> pq = new PriorityQueue<>(); for (int weight : weights) { pq.offer(new Node(weight)); } while (pq.size() > 1) { Node left = pq.poll(); Node right = pq.poll(); Node parent = new Node(left.weight + right.weight); parent.left = left; parent.right = right; pq.offer(parent); } root = pq.poll(); } public Map<Character, String> getCodes() { Map<Character, String> codes = new HashMap<>(); getCode(root, "", codes); return codes; } private void getCode(Node node, String code, Map<Character, String> codes) { if (node.isLeaf()) { codes.put((char) node.weight, code); } else { getCode(node.left, code + "0", codes); getCode(node.right, code + "1", codes); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值