• Post Reply Bookmark Topic Watch Topic
  • New Topic

binary trees  RSS feed

 
sudde gameeef
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting a error:
Exception in thread "main" java.lang.StackOverflowError
at D06_Binary_Tree$node.access$3(D06_Binary_Tree.java:22)
at D06_Binary_Tree.inOrderTraversal(D06_Binary_Tree.java:116)

andy idea how can i fix this problem?



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The direct cause of your error is line 94. You're throwing away the value that was passed in to the method inOrderTraversal, because you always set current_node to root_node there. This leads to an infinite recursive call in line 102. Remove line 94.

Other remarks about your code:

Why are you using an octal number literal "07" in line 15?

Why do you have member variables root_node, current_node, parent_node and new_node (lines 33 - 36)? Don't make something a member variable unless it really needs to be a member variable; make it a local variable if possible. (For example, new_node does not need to be a member variable).

There is a naming convention for Java classes, methods and variables that almost everybody uses: CamelCase for classes, camelCase (with the first letter lower-case) for methods and variables. Don't use underscores in names. I suggest you use the standard naming convention (Node instead of node, leftChild and rightChild instead of left_child, right_child etc.).
 
sudde gameeef
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. i almost got it to working. just one problem its print tree is always empty in if system. i am not sure why.

and by local vairbales you be change it to public right? than it will be local.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudde gameeef wrote:and by local vairbales you be change it to public right? than it will be local.

No, a public member variable is not the same as a local variable.

A local variable is a variable that is declared inside a method instead of at class level - it only exists inside a method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!