• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

binary trees

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic