c++ - Problem on my binary tree class' nodes -
i'm trying code class binary tree representation. each node has value (key
), index
, , node*
pointer parent(p
), left-child(left
) , right-child(right
). problem on pointers. it's easier giving example problem explaining.
i coded print()
function prints out each node in tree. here class header file. , here main test file.
the problem is, when call t.print()
, prints 10, 5 , 7.
the problem you're using vector, internally (re-)allocates storage needed.
so, when pushing vector, whole internal data copied other memory location - making pointers still point old locations invalid.
an easy "fix" reserve amount of space vector, can @ least store amount of nodes in without re-allocation.
for example adding @ start of rootedtree
constructors :
t.reserve(64);
note that not robust solution (if try put more 64 nodes in vector, you're still have same problem) - it'll confirm above analysis.
Comments
Post a Comment