emory-courses / dsa-java

Data Structures and Algorithms in Java
https://emory.gitbook.io/dsa-java/
42 stars 55 forks source link

[QZ4] print tree #118

Closed brendacano closed 3 years ago

brendacano commented 3 years ago

When I try to print the tree using the toString function, it doesn't give me the numbers and instead gives me variations of BinaryNode@452459b3. How do I address this so I can check if my method actually works as well as to visualize AVLTree and RedBlackTree?

JonathanKimchi commented 3 years ago

What I did was print the value of the key using node.getKey();

This won't work for anything other than Integers or Strings I don't think, but I believe the test case is written with ints.

kristiey510 commented 3 years ago

I thinnk you can do something like this

Screen Shot 2020-10-13 at 9 05 06 PM
brendacano commented 3 years ago

I thinnk you can do something like this

Screen Shot 2020-10-13 at 9 05 06 PM

Thats what I did but it prints out as: 2020-10-13 (1)

kristiey510 commented 3 years ago

I thinnk you can do something like this

Screen Shot 2020-10-13 at 9 05 06 PM

Thats what I did but it prints out as: 2020-10-13 (1)

hmm thats weird mine shows something like this

Screen Shot 2020-10-13 at 9 12 03 PM
JonathanKimchi commented 3 years ago

^Yeah, you should use .getKey(). If you print the tree itself all you will get are the memory addresses of the nodes.

Kristi, did you write a .toString() method?

kristiey510 commented 3 years ago

^Yeah, you should use .getKey(). If you print the tree itself all you will get are the memory addresses of the pointers.

Kristi, did you write a .toString() method?

no i just imported the classes that Dr. Choi had in his test

Screen Shot 2020-10-13 at 9 13 15 PM
brendacano commented 3 years ago

^ yeah i did the same thing, weird how it doesn't work for everyone

JonathanKimchi commented 3 years ago

Oh, that IS strange.

brendacano commented 3 years ago

Okay, I figured it out. In AbstractBinaryNode.java there is supposed to be this function: @Override public String toString() { return key + " -> (" + left_child + ", " + right_child + ")"; } I just looked through the codes and checked if there was a discrepancy between the code I had written code with the code in the repository. I added the function and now it actually prints.

brendacano commented 3 years ago

Thanks for the help guys, it helped me find the root of the issue.