Skip to content
Advertisement

Binary Search Tree Traversals failed to execute when using recursion

I am new to Java programming and data structures. Nevertheless I could implement the following code after so many efforts. There I need to insert values to nodes, and print the values in each node to demonstrate all 03 types of depth first traversal techniques with the help of recursion.

  1. PreOrder traversal
  2. PostOrder traversal
  3. InOrder traversal

I have developed 03 separate methods to implement above 03 traversal methods. (I have commented the particular sections in the code given at the end of the question)

But when I try to run the code I get the following Error message Click here to view And for the reference I provided the error message in text format as below.

JavaScript

I do not understand why I am getting the above error. Because in my perspective my approach is correct.

Following is the complete code I implemented

JavaScript

Can somebody please look through my code and please be kind to show any stupid mistake I have made for not to run the code successfully, and the reason why I am getting above mentioned error? I am really sucked at this point.

Thanks in advance.

Advertisement

Answer

You’re missing base condition of your recursive traversal functions which is if (currentNode == null) return;

Example:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement