Can anyone help me with an explanation for these codes ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Can anyone help me with an explanation for these codes ?

These methods are from binary tree implementation code and prints input data in order. I just don't understand this part. How these methods works ? public void inOrder(){ inOrder(root); } public void inOrder(Node akar){ if(akar != null){ inOrder(akar.left); System.out.print(akar.data+" "); inOrder(akar.right); } }

28th Apr 2019, 3:02 PM
Poe
Poe - avatar
2 Respostas
+ 7
Poe firstly in order traversal is worked in LRR manner first left subtree is visited and printed then root is visited and printed then right subtree is visited and printed when inOrder() method is called according to your posted code if the value of any occurred node "akar" is null then no node is present in tree or no any node is visited yet. So it first start visiting left node via inOrder(akar.left) and then inOrder(root) is traversed then inOrder(akar.right) is traversed and printed.
28th Apr 2019, 4:56 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
Thx for the explaination bro so it can be functioned as a loop to traverse and print the visited node, am i right ?
5th May 2019, 6:27 AM
Poe
Poe - avatar