简体   繁体   中英

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.

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.

EDIT:

To clarify, I'm talking about 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. So for instance if you called it on the example tree, it would return 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. 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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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