Welcome Guest [Log In] [Register]
Welcome to UCR CS 14 Klefstad.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
finding parent node
Topic Started: Feb 21 2014, 07:40 PM (64 Views)
rdinh002
No Avatar

I'm working on a function called findParent which returns the parent of a certain node. When implementing in my remove function it gives me a seg fault and I don't understand why I get one.
Spoiler: click to toggle
Offline Profile Quote Post Goto Top
 
jdomi008
No Avatar

instead of calling find in the remove function then calling findParent, you can just write the exact same code for find inside remove but with a couple added lines to keep track of the parent.
Offline Profile Quote Post Goto Top
 
klefstad
No Avatar
Administrator
Calling another function to find the parent is far too expensive and wasteful wince you've already done the work.

If you really need to know, keep the parent pointer (like a previous pointer in a singly linked list) as you traverse.

Or better yet, use reference parameter for the TreeNode* parameter, then you can assign into it to modify the pointer passed in.

Or have your function return the pointer and at each appropriate point, assign the value returned by the function, like remove, back into the pointer you are removing from.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Homework 6 · Next Topic »
Add Reply