Show TreeSet in Scala / Java

66 Views Asked by At

Is there a way, in Scala, to print a TreeSet in a way it stored in the memory? For example, in case I initialize a tree

val tree_1: TreeSet[Int] = TreeSet(4, 2, 1)

I would expect to see something like -

    4
   /
  2
 /
1

But when I try to approach to root, all I can see is -

println(tree_1.head) //>> 1
println(tree_1.firstKey) //>> 1

Same happens for

val tree_2: TreeSet[Int] = TreeSet(2, 1, 4)

I would expect to see -

    2
   / \
  1   4

But I get the same result -

println(tree_2.head) //>> 1
println(tree_2.firstKey) //>> 1

Edit - If I wasn't clear, I want to print everything from top to bottom in the way it was initialized in the memory, not in ascending order. I know that .head or .firstKey are supposed to return only a value and not a Tree, I would like it to return the root value, then go to the right/left branch, print the value and keep going.

0

There are 0 best solutions below