3

I made a basic tree where all the nodes have a name and a set of children.

public class Tree {
    String data;
    Tree parent = null;
    HashSet children = new HashSet();

    public Tree(String nodeName) {
        this.data = nodeName;
    }

    public void parent(Tree parent) {
        this.parent = parent
    }

    public void addAChild(Tree child) {
        this.children.add(child);
        child.parent(this);
    }

And to use this class

Tree a = new Tree("root");
Tree b = new Tree("n1");
Tree c = new Tree("n2");
Tree d = new Tree("n3");
Tree e = new Tree("n4");
Tree f = new Tree("n5");

a.addAChild(b);
a.addAChild(c);
a.addAChild(d);

d.addAChild(e);
e.addAChild(f);

This makes sense to me but I'd like a visual representation of the tree so that I can quickly test to see if the children and nodes are in the right place.

I'm trying to make the output look like this:enter image description here

Or something similar.

3
  • When you say print, do you mean in command line? If so maybe you should try to print it like a directory tree. Commented Oct 28, 2015 at 5:31
  • "Or something similar" - Do you mean it has to be a graphic represenation? Or would an indented representation such as you can find in file browsers be OK? Commented Oct 28, 2015 at 5:32
  • I'm looking for command line output so i can see if the nodes are in the correct positions. And @Linus yes I believe what I have here is similar to a directory tree, i'll look into it. Commented Oct 28, 2015 at 5:34

2 Answers 2

6

A quick and dirty way of printing the tree would involve adding a method like this to your Tree class:

public void print(int level) {
    for (int i = 1; i < level; i++) {
        System.out.print("\t");
    }
    System.out.println(data);
    for (Tree child : children) {
        child.print(level + 1);
    }
}

level represents the level of the node in the tree, which is defined as 1 + (the number of connections between the node and the root). It dictates how much the node will be indented in the output.

Then you can print the tree by printing the root (the level of the root is 1):

a.print(1);

Getting output like this:

root
    n1
    n2
    n3
        n4
            n5
Sign up to request clarification or add additional context in comments.

2 Comments

This answer was really helpful. However, I would change "indent" for "level", since the level in the tree is the real meaning of the tabs.
@randombee Good point, thanks. I've also changed the numbers accordingly, since root is defined to have a level of 1.
0

This method works in python, and is recursive. Basically, it prints the name of each node, increases the indent and then calls the function on each child of each node. You will have to add an if statement before the for loop to determine if you are at the end of the branch so it doesn't call the function again.

Add it to your tree class.

 def display(self,indent=0):
    print( (' '*indent)+self.name)        
    for c in self.children:
      c.display(indent+1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.