简体   繁体   English

按级别打印任何给定的二进制TreeSet -Java

[英]Print any given binary TreeSet by level -Java

I've seen a lot of questions ( How to print binary tree diagram? ) about how to print out a Tree by level and they all seem to be using nodes, but in the main they define each node and give it specific pointers by hand...I'm looking for a way to do the same thing, print out a tree by level, but you're given a TreeSet of type Integer (eg {3,12,28,40,41,58,83} ), and you don't know what's going to be in it until runtime. 我已经看到了很多有关如何按级别打印树的问题( 如何打印二叉树图? ),它们似乎都在使用节点,但主要是它们定义了每个节点并手动为其指定了特定的指针...我正在寻找一种方法来做同样的事情,逐级打印出一棵树,但是给了您一个整数类型的TreeSet(例如{3,12,28,40,41,58,83} ),直到运行时您都不知道其中会有什么。

For simplicity, output would look something like this: 为了简单起见,输出如下所示:

40
12    58
3    28    41    83

With or without the use of Nodes is fine, but I'd be interested to see a version without them. 使用或不使用Nodes都可以,但是我很想看看没有它们的版本。

EDIT: 编辑:

To clarify, I'm talking about java.util.TreeSet . 为了澄清,我正在谈论java.util.TreeSet

So I've been working on this since I posted it, and I came up with a method I call getRoot which takes a TreeSet<Integer> as a parameter and returns an Integer which is the middle of the Tree. 因此,自发布以来,我一直在研究此问题,并提出了一个名为getRoot的方法,该方法将TreeSet<Integer>作为参数并返回一个Integer ,该Integer位于Tree的中间。 So for instance if you called it on the example tree, it would return 40 . 因此,例如,如果您在示例树上调用它,它将返回40

So now I'm thinking there's probably a way to recursively do what I'm talking about, by calling getRoot on sub trees, eg tailSet and headSet of a tree, if that makes any sense. 所以现在我在想,也许有一种方法可以通过在子树(例如树的tailSetheadSet上调用getRoot来递归地执行我正在谈论的事情。 I'm having trouble making it follow through every branch of the tree, however. 但是,我无法使它遍历树的每个分支。 My code so far: 到目前为止,我的代码:

public TreeSet<Integer> recursivePlease(TreeSet<Integer> t){
        if (t.size()<=1){ //base case
            System.out.println(t.first());
            return t;
        }else{
            System.out.println(getRoot(t));
            return recursivePlease((TreeSet<Integer>) t.headSet(getRoot(t)));
        }
    }

and this...works. 这...有效。 But it just prints the left side of the tree. 但是它只是打印树的左侧。 Any help on making this method print the whole tree, or other ideas entirely, are greatly appreciated. 非常感谢您使用此方法可以打印出整棵树或其他想法。

Your code is an excellent starting point. 您的代码是一个很好的起点。 Your problem with printing only the left side of the tree comes from the fact than from the second line you have to print two trees, on the third line up to four trees, which your method cannot handle since it only takes one tree as argument. 您只打印树的左侧的问题来自于事实,而不是第二行,您必须打印两棵树,在第三行上最多打印四棵树,您的方法无法处理,因为它仅以一棵树作为参数。 So we will have to alter it to take any number of trees. 因此,我们将不得不对其进行更改以采用任意数量的树木。 I have chosen a List<SortedSet<Integer>> (where SortedSet is one of the interfaces that TreeSet implements). 我选择了List<SortedSet<Integer>> (其中SortedSetTreeSet实现的接口之一)。 This entails rewriting a part of the logic in the method to deal with more trees of course. 当然,这需要重写该方法中的一部分逻辑以处理更多的树。 I didn't see the need for returning anything, so I changed the return type to void. 我看不到需要返回任何东西,因此我将返回类型更改为void。 Here's what I ended up with: 我最终得到的是:

public void recursivePlease(List<SortedSet<Integer>> trees) {
    List<SortedSet<Integer>> nextLevelTrees = new ArrayList<>();
    for (SortedSet<Integer> t : trees) {
        if (t.size() <= 1) { //base case
            System.out.print("" + t.first() + ' ');
        } else {
            Integer root = getRoot(t);
            System.out.print("" + root + ' ');
            SortedSet<Integer> headSet = t.headSet(root);
            if (! headSet.isEmpty()) {
                nextLevelTrees.add(headSet);
            }
            SortedSet<Integer> tailSet = t.tailSet(root + 1);
            if (! tailSet.isEmpty()) {
                nextLevelTrees.add(tailSet);
            }
        }
    }
    System.out.println();
    if (! nextLevelTrees.isEmpty()) {
        recursivePlease(nextLevelTrees);
    }
}

For the first call you have only a single tree, so you may call it as: 对于第一次调用,您只有一棵树,因此可以将其称为:

    recursivePlease(Collections.singletonList(t));

On my computer it prints: 在我的计算机上打印:

40 
12 58 
3 28 41 83 

Depending on your getRoot() your result may differ. 根据您的getRoot()结果可能有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM