• 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

Tree Traversal

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help! I'm writing code to traverse a tree, find a given character, and then print out it's path labels. It's basically for Huffman coding. I've written the following, but it just goes on and on. Can someone help? Thanks!

 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean that when you run it, it runs forever without terminating?

I find your code hard to read with (all those brackets). Try evaluating r.getLeft() and r.getRight() before that if-statement, and assign them to variables. And I don't understand how you can compare something.getChar() to null and then to a char, and still have the program compile. If getChar() returns a char then you can't compare it to null, and if it returns an object then you can't compare it to a char.

And your traverse() method returns a String, so I suppose you are building that string from the nodes where you find something? If so then you should do something with whatever it is that the recursive calls to traverse() return, shouldn't you?
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic