| Author |
Question about deleting a node from a binary tree (non-API binary tree; hand-coded)
|
James Brooks
Gunslinger
Ranch Hand
Joined: Aug 17, 2006
Posts: 165
|
|
Hi again,
I've got something that's been bugging me. For this binary tree deleteNode function, when a node is found, the method deleteFromTree is called on it. I understand everything, except: this may sound silly, but it seems that the method passes the target node on in a roundabout way. As you see, when found == true, we don't simply pass current as the method parameter, but rather tempCurrent.lLink or tempCurrent.rLink. Why is this? I know that there must be a reason that I'm not seeing (my text surely doesn't do it just for the heck of it), but it seems that this wastes computing time creating a reference to tempCurrent, then comparing it to deleteItem, then passing the appropriate child of tempCurrent to the deleteFromTree method, when current could simply be sent as the parameter, as it is the target node upon found == true. Thanks in advance!
|
Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
This is a great question for experimenting!
So, have you tried passing in current? What happens? See if you can print out the tree before and after in both cases (using the existing way and you're passing in current way). Use a small tree but make sure it as a depth of at least 3-4 so you can test deleting root, leaf, 'ordinary node with parent and children'.
Let us know how you get on!
|
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
|
 |
James Brooks
Gunslinger
Ranch Hand
Joined: Aug 17, 2006
Posts: 165
|
|
Martijn Verburg wrote:This is a great question for experimenting!
So, have you tried passing in current? What happens? See if you can print out the tree before and after in both cases (using the existing way and you're passing in current way). Use a small tree but make sure it as a depth of at least 3-4 so you can test deleting root, leaf, 'ordinary node with parent and children'.
Let us know how you get on!
Sorry about the double post, it didn't look like it took the first time. I plan to do that, and I'll be back later today with the results after work.
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
Patrick Brooks wrote:Sorry about the double post, it didn't look like it took the first time. I plan to do that, and I'll be back later today with the results after work.
No probs, we've been experiencing a few glitches of late
|
 |
 |
|
|
subject: Question about deleting a node from a binary tree (non-API binary tree; hand-coded)
|
|
|