數據結構-樹1

//樹基本構造
public class TreeNode<T> {
    T value;

    TreeNode<T> leftChild;
    TreeNode<T> rightChild;

    TreeNode(T value) {
        this.value = value;
    }

    TreeNode() {
    }

    /**
     * 增加左子節點
     * addLeft:
     *
     * @param value void  返回類型
     */
    public void addLeft(T value) {
        TreeNode<T> leftChild = new TreeNode<T>(value);
        this.leftChild = leftChild;
    }

    /**
     * addRight: 增加右子節點
     *
     * @param value void  返回類型
     */
    public void addRight(T value) {
        TreeNode<T> rightChild = new TreeNode<T>(value);
        this.rightChild = rightChild;
    }


    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (!(obj instanceof TreeNode)) {
            return false;
        }
        return this.value.equals(((TreeNode<?>) obj).value);
    }


    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return this.value.hashCode();
    }

    @Override
    public String toString() {
        return this.value == null ? "" : this.value.toString();
    }
    
}
//簡單方法
public class Tools {

    //訪問根結點
    private static <T> void visitNode(TreeNode<T> node) {
        System.out.println(node.value + "\t");
    }

    //樹的節點個數
    public static <T> int getTreeNum(TreeNode<T> root) {
        if (root == null) {
            return 0;
        }
        return getTreeNum(root.leftChild) + getTreeNum(root.rightChild) + 1;
    }

    //樹的深度
    public static <T> int getTreeDepth(TreeNode<T> root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = getTreeDepth(root.leftChild) + 1;
        int rightDepth = getTreeDepth(root.rightChild) + 1;
        return Math.max(leftDepth, rightDepth);
    }

    //前序遍歷
    public static <T> void preOrderTravel(TreeNode<T> root) {
        if (root == null) {
            return;
        }
        visitNode(root);
        preOrderTravel(root.leftChild);
        preOrderTravel(root.rightChild);
    }

    //中序遍歷
    public static <T> void midOrderTravel(TreeNode<T> root) {
        if (root == null) {
            return;
        }
        midOrderTravel(root.leftChild);
        visitNode(root);
        midOrderTravel(root.rightChild);
    }

    //後序遍歷
    public static <T> void backOrderTraval(TreeNode<T> root) {
        if (root == null) {
            return;
        }
        backOrderTraval(root.leftChild);
        backOrderTraval(root.rightChild);
        visitNode(root);
    }

    //層次遍歷
    public static <T> void levelTravel(TreeNode<T> root) {
        Queue<TreeNode<T>> q = new LinkedList<>();
        //向隊列尾添加一個元素
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode<T> temp = q.poll();
            visitNode(temp);
            if (temp.leftChild != null) {
                q.offer(temp.leftChild);
            }
            if (temp.rightChild != null) {
                q.offer(temp.rightChild);
            }
        }
    }

    //求第K層節點個數
    public static <T> int getNumForKlevel(TreeNode<T> root, int k) {
        if (root == null || k < 1) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int leftNum = getNumForKlevel(root.leftChild, k - 1);
        int rightNum = getNumForKlevel(root.rightChild, k - 1);
        return leftNum + rightNum;
    }

    //二叉樹中葉子節點的個數
    public static <T> int getLeafNum(TreeNode<T> root) {
        if (root == null) {
            return 0;
        }
        if (root.leftChild == null && root.rightChild == null) {
            return 1;
        }
        int leafNum = getLeafNum(root.leftChild);
        int rightNum = getLeafNum(root.rightChild);
        return leafNum + rightNum;
    }

    //交換根節點的左右子樹
    public static <T> TreeNode<T> exchange(TreeNode<T> root) {
        if (root == null) {
            return null;
        }
        TreeNode<T> left = exchange(root.leftChild);
        TreeNode<T> right = exchange(root.rightChild);
        root.leftChild = right;
        root.rightChild = left;
        return root;
    }

    //查看node是否是root的子節點
    public static <T> Boolean nodeIsChild(TreeNode<T> root, TreeNode<T> node) {
        if (root == null || node == null) {
            return false;
        }
        if (root == node) {
            return true;
        }
        Boolean isFine = nodeIsChild(root.leftChild, node);
        if (!isFine) {
            isFine = nodeIsChild(root.rightChild, node);
        }

        return isFine;
    }

    //返回兩個節點lnode和rnode的以root爲根節點的公共子節點
    public static <T> TreeNode<T> findAllFatherNode(TreeNode<T> root, TreeNode<T> lNode, TreeNode<T> rNode) {
        if (lNode == root || rNode == root) {
            return root;
        }
        if (root == null || rNode == null || rNode == null) {
            return null;
        }

        if (nodeIsChild(root.leftChild, lNode)) {
            if (nodeIsChild(root.rightChild, rNode)) {
                return root;
            } else {
                return findAllFatherNode(root.leftChild, lNode, rNode);
            }
        } else {
            if (nodeIsChild(root.leftChild, rNode)) {
                return root;
            } else {
                return findAllFatherNode(root.rightChild, lNode, rNode);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章