Josh Haverford

Greenhorn
+ Follow
since Feb 16, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Josh Haverford

Stephan van Hulst wrote:Yes, you will have to repeat the process for the right leaf. Also, if the left or right node you are considering is not a leaf, it means it is a root of a subtree, which contains leaves you may want to delete.

I might as well show you the final solution now. Let us know whether you understand it.


[edited 10.000 times for typos, arghghgh]



Let me get this right, so in the else statement, deleteLeaves(root.left, right) is just going to the next node making the one just checked its parent if the left/right has no children(meaning it is not a leaf), and then checking the child of that?

Im getting a Null pointer exception when i call the deleteleaves method in my main
deleteLeaves(tree.root);


am i calling it right? how do i show that they have been deleted correctly?



Also, this may sound like a stupid question, but is the height of this tree 5? I dont think my height code is working properly

13 years ago

Stephan van Hulst wrote:I'll start you off:



dont i need to set the right side to null as well?


for what to put after the else statement, if it is not null i have it return and quit?
13 years ago

Paul Clapham wrote:I'm confused about what you expect that line of code to do:

What that does (or would do if it compiled) is this:

  • Assign null to the test.next variable
  • Call deleteLeaves(null)


  • Personally I wouldn't write that line of code, since it's rather obscure. If I wanted to do those two things I would write the appropriate two lines of code to do them. (But I suspect you don't quite want to do those two things.)




    Yeah, i realize what i have to do. I need to test whether a node is a leaf from its parent so that the
    reference to this leaf can be set to null, which deletes the leaf, but i dont understand the proper code to implement that. I am a first year java student. I know there are so many options out there but i feel so limited due to my lack of experience and knowledge..
    13 years ago



    How does this look? it is saying next isnt a field, is there another way i am supposed to be looking at the child node from the parent ?
    13 years ago



    so your saying that code?


    I thought leaves meant there were no children ie. empty


    I think i need a depth-first traversal
    13 years ago


    i am having trouble doing this - any suggestions?
    13 years ago