非二叉树高度

时间:2012-11-20 15:29:18

标签: data-structures tree

有没有办法找到不一定是二进制的树的高度?对于二叉树的高度有许多算法,但它们都不适用于非二进制树。

4 个答案:

答案 0 :(得分:3)

是的,有。递归方法可能类似于:

public class TreeNode<T>{
    private List<TreeNode<T>> children = new ArrayList<TreeNode<T>>();
    private T data = null;

    public TreeNode(T data){
        this.data = data;
    }       

    public List<TreeNode<T>> getChildren(){
        return children;
    }   

    public void setChild(TreeNode<T> children){
        this.children.add(children);
    }   

    public Integer getHeight(TreeNode<T> root){
        if(root == null) return 0;
        Integer h=0;

        for(TreeNode<T> n : root.getChildren()){
            h = Math.max(h, getHeight(n));
        }
        return h+1;
    }
}

测试:

public static void main(String[] args){
    TreeNode<Integer> root = new TreeNode<Integer>(50);
    TreeNode<Integer> c1 = new TreeNode<Integer>(100);
    TreeNode<Integer> c2= new TreeNode<Integer>(10);
    TreeNode<Integer> c3 = new TreeNode<Integer>(-5);
    TreeNode<Integer> c4 = new TreeNode<Integer>(0);
    TreeNode<Integer> c5 = new TreeNode<Integer>(33);
    TreeNode<Integer> c6 = new TreeNode<Integer>(1);
    TreeNode<Integer> c7 = new TreeNode<Integer>(2);
    TreeNode<Integer> c8 = new TreeNode<Integer>(3);
    TreeNode<Integer> c9 = new TreeNode<Integer>(300);
    TreeNode<Integer> c10 = new TreeNode<Integer>(350);

    root.setChild(c1);
    root.setChild(c2);
    c2.setChild(c3);
    c3.setChild(c4);
    c3.setChild(c5);
    c3.setChild(c6);
    c3.setChild(c7);
    c3.setChild(c8);

    c1.setChild(c9);
    c1.setChild(c10);

    System.out.print("Pre order: \n");
    root.dfs(root, 0);
    System.out.print("\nPost order: \n");
    root.dfs(root, 1);
    System.out.print("\nBFS: \n");
    root.bfs(root);
    System.out.println();
    System.out.print("\nHeigth: \n");
    System.out.println(root.getHeight(root));
}

结果:

Heigth: 
4

编辑:如前所述,返回4而不是3

答案 1 :(得分:1)

任何树的定义都是相同的。树的高度是其任何子节点的高度(加一)。因此,如果您有三个孩子,则检查所有三个孩子并以递归方式取最大值+ 1作为您的身高。

答案 2 :(得分:1)

有可能。我们可以通过以下方法来完成。

package com.ds;

import java.util.Arrays;

public class TreeNode {

    private Integer data;
    private TreeNode[] children;

    public TreeNode() {
        super();
    }

    public TreeNode(Integer data) {
        super();
        this.data = data;
    }


    public void setChildren(TreeNode[] children) {
        this.children = children;
    }

    public Integer maxDepth(TreeNode treeNode) {
        Integer depth = 0;
        if (treeNode.children != null) {
            if (treeNode.children.length == 0) {
                return depth;
            } else {
                for (int i = 0; i < treeNode.children.length; i++) {
                    depth = Math.max(depth, this.maxDepth(treeNode.children[i]));
                }
                return depth + 1;
            }
        } else {
            return depth;
        }
    }

    @Override
    public String toString() {
        return "TreeNode [data=" + data + ", children=" + Arrays.toString(children) + "]";
    }

    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        TreeNode t7 = new TreeNode(7);
        TreeNode t8 = new TreeNode(8);
        TreeNode t9 = new TreeNode(9);
        TreeNode t10 = new TreeNode(10);
        TreeNode t11 = new TreeNode(11);
        TreeNode t12 = new TreeNode(12);

        TreeNode t101 = new TreeNode(101);

        TreeNode[] childOf1 = { t2, t3 };
        TreeNode[] childOf2 = { t4, t5, t101 };
        TreeNode[] childOf3 = { t6, t7 };
        TreeNode[] childOf4 = { t8, t9 };
        TreeNode[] childOf6 = { t10 };
        TreeNode[] childOf10 = { t11, t12 };
        t1.setChildren(childOf1);
        t2.setChildren(childOf2);
        t3.setChildren(childOf3);
        t4.setChildren(childOf4);
        t6.setChildren(childOf6);
        t10.setChildren(childOf10);

        TreeNode obj = new TreeNode();
        Integer depth = obj.maxDepth(t1);
        System.out.println("Depth- " + depth);

    }

}

答案 3 :(得分:0)

通常,您可以将二进制树的大多数算法扩展为非二进制。

例如,对于2-tree:

h(node) = max(h(left-child-of(node)) , h(right-child-of(node)))+1

可以扩展为:

h(node) = max(h(for-all-child-of(node)) + 1