• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary search tree remove node

 
Brian Lennon
Greenhorn
Posts: 3
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi if someone can help me it would be great!!! Basically I have a remove method which removes a specified node but what the method is doing is deleting the entire tree. I tried debugging but I cant see the problem.
Here is my code...


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

You say you tried debugging your code, but I don't see any println() calls that would be useful for that. Your first step should be to add a bunch to the remove method, to show what's happening at each step, what it's removing, and what the tree looks like before and after.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is not compilable, so whatever you are running is not the code you posted.
 
Brian Lennon
Greenhorn
Posts: 3
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Mack I just had a look and here is the revised code


 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are assigning head.right = tail in your remove routine and when it goes to traverse() there is nothing to traverse. You are making t = tail on line 113. You essential disconnected the whole tree as far as traverse() is concerned. I think you need to start over on your removal routine and think through what you need to do step by step and write it down in pseudo code before you code it. Other than that, there are probably hundreds of examples on the Internet of binary tree operations you should look at.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic