Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do you remove a node from a binary tree?

 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




did i do it right. first, make a function to check the head , whether to remove it, and then call for a function to remove nodes from the body, and transfer the chain underneath to another node..

one thing that i didnt understand is why do you need addUnit(right), addUnit(left) .. in the order described in the remove functions?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you add a node to both the left and right nodes?

And the usual advice: write out the algorithm, draw a picture, whatever helps you understand. Run through a few cases:

- No nodes
- A single node
- A few nodes

That should be enough to make the algorithm understandable.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I have deleted your post in which you question my usefulness so that you will not be banned from the site. Please refrain from insulting the staff and being inflammatory, and recognize that not only *am* I helping you, I am the *only* one helping you. Unfortunately, you refuse to help *yourself*, which is pretty much one of the only rules of JavaRanch, along with BeNice. If you fail to BeNice one more time, the ban hammer will be wielded.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic