2

Hey to all Stackoverflowers!:D

This is my first question ever on stackoverflow, I appreciate any advice for my next questions!

I want to print out the data of all nodes in a tree data structure. I already have some code, but I get weird results.

-Every node can have multiple children, those are stored in a List of nodes
-I always start with the root

Here is my Code:

    public static void printTree(Knoten blatt){      
    Aufgabe1.Gewichte = Aufgabe1.Gewichte + blatt.Gewicht;
    System.out.println(blatt.Gewicht);

    for(int i=0;i<blatt.children.size();i++) {
        blatt = blatt.children.get(i);
        printTree(blatt);
    } 
}

But if I call this function my programm does not print out all nodes. I know this since I print the data while creating the nodes, I get this:

3.0
27.0
-6.0
-7.0
10.0
-5.0
-47.0
-13.0
-5.0

If I print it out with my recursive function, I get this:

3.0
27.0
-6.0
-7.0
10.0
-5.0
-47.0
-13.0

-47 is the parent, with the children -13 and -5, -5 just doesnt want to get printed tho.

If I print it out manually, like root.children.children...get(i) whatever, I can access both kids, with the right data... I seriously have no clue where my fault is in my code, would be great if someone can help me... I am sure im just kinda blind :D

I also tried some other trees, sometimes this error wont show up, sometimes it will...

The Tree-Structure looks like this: enter image description here

Thanks for your help, I hope my question is understandable

9
  • oh, yes, I made a mistake with the formatting, thanks! Commented Apr 19, 2017 at 16:20
  • 1
    Can you also post the tree structure you tested? I suspect that you may be confused by the output due to the order in which everything is being printed. Commented Apr 19, 2017 at 16:20
  • Do you mean the data of the tree, or my tree data structure?:D Commented Apr 19, 2017 at 16:21
  • 1
    I am trying to correlate the linear output you showed us with the actual structure of the tree. Otherwise, how can anyone know if there is even a real problem? Commented Apr 19, 2017 at 16:24
  • here you go, I hope this helps you Commented Apr 19, 2017 at 16:31

1 Answer 1

2

You are changing the current node variable (= blatt) while you are iterating the children by re-assigning blatt in the loop. This means that you continue at the child's second child (instead of the second direct child) after the first child was printed. Fix:

public static void printTree(Knoten blatt) {      
  Aufgabe1.Gewichte = Aufgabe1.Gewichte + blatt.Gewicht;
  System.out.println(blatt.Gewicht);

  for(int i = 0; i < blatt.children.size(); i++) {
    Knoten kind = blatt.children.get(i);  // Don't overwrite blatt here
    printTree(kind);
  } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

blatt is just a reference, so would reassigning it to something else change the underlying data to which it points?
@TimBiegeleisen No, and I did not intend to claim so (edited the text to be less ambiguous)

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.