1

I'm trying to code a class for binary tree representation. Each node has a value (key), an index, and a Node* pointer for parent(p), left-child(left) and right-child(right). The problem is on pointers. It's easier giving my example problem than explaining.

I coded a print() function that prints out each node in the tree. Here is the class header file. And here is the main test file.

The problem is, when i call T.print(), it prints only 10, 5 and 7.

9
  • have you run the debugger already? Commented Jul 1, 2011 at 8:22
  • lines 62 and 68 should have n-2 instead of n-1. Commented Jul 1, 2011 at 8:32
  • Uhm... no, they shouldn't. It go in a forever loop if i do this. n is actually the number of nodes. They are stored in a vector, so, if the node 3 is stored, i can access through T[n-1] = 2. Commented Jul 1, 2011 at 8:35
  • why do you obviously ignore comments? Commented Jul 1, 2011 at 8:37
  • have you tested the return values of your insert calls to make sure they're returning true? Commented Jul 1, 2011 at 8:43

2 Answers 2

1

The problem is that you're using a vector, which internally (re-)allocates storage as needed.

So, when pushing back into a vector, it could very well be that the whole internal data is copied to some other memory location - making all pointers that still point to the old locations invalid.

An easy "fix" would be to reserve a certain amount of space for the vector, so you can at least store a certain amount of nodes in it without re-allocation.

For example by adding this at the start of your RootedTree constructors :

T.reserve(64);

Note that that is not a robust solution (if you try to put more than 64 nodes in the vector, you're still likely to have the same problem) - but it'll confirm the above analysis.

Sign up to request clarification or add additional context in comments.

1 Comment

@user824476 : I do not recommend the reserve approach though - for the reasons stated at the end of the answer. You might be better off storing the nodes explicitly as a tree - without using a vector for storage.
0

You have to decide what kind of structure you want to represent your binary tree.

Either of

  • a vector
  • pointers

Not both!

So your options are

  1. Removing all pointer related stuff and stick with the vector, treat the Node at index i as the parent to the Nodes at i*2, i*2 + 1. and the opposite for the child -> parent relationship.
  2. Stop using the vector. Have a root node Node * root inside your RootedTree class.

They two approaches have different strengths and weaknesses, in particular, the array case lets you get rid of all the pointers, in the pointer case you don't have to worry about the performance of reallocations, and trickier manipulations like deletion is a lot easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.