Hi Everyone,

I need help understanding how these traversals work :s

On this page they have the code for these tree traversals, but I don't quite get it. For example, printing using the in-order traversal (using the example tree provided on the site) should give:

A, B, C, D, E, F, G, H, I (left, root, right)

With the code being:

  if node.left  ≠ null then inorder(node.left)
  print node.value
  if node.right ≠ null then inorder(node.right)

But I fail to understand the algorithm. The method receives the root node and checks the nodes on the left successively to see if if the node on its left is not empty. When a node whos left node is empty is found it displays the contents of that node. What next??

I am very stuck here lol. Supposedly, the value should be printed and then the method checks to see if the node on the right is not empty, but there is NO node on the right. What follows??

Thanks in advance for your help!

Nevermind folks! I figured it out with a little effort hehe.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.