Binary Search Tree Copy Constructor Question

Jan 21, 2012 at 2:53pm
Before I post, I want to note that I've been searching for 4 hours and tinkering with this and to no avail. I know it's a common question but please bare with me.

The important part of my code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// The BST.h file
#ifndef BST_H
#define BST_H

#include <iostream>
#include "Object.h"
using namespace std;

class BST {

public:
	BST();					// Default Constructor
	BST(const BST &);		// Copy-Constructor
	~BST();					// Destructor

        Node *copyHelper(const Node*); //The recursive helper

private:
	struct Node {
		Generic *item; //Another written class that's a generic item
		Node *right;
		Node *left;
	};
	Node *root;
};
#endif BST_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//The BST.cpp

#include "BST.h"

// Default Constructor
BST::BST(){
	this->root = NULL;	
}

// Copy-Constructor
BST::BST(const BST &toCopy){
	root = copyHelper(toCopy.root);
}

// Destructor
BST::~BST(){
	this->makeEmpty(); // A working method I left out for readability
}

//Helper Method
BST::Node* BST::copyHelper(const Node *toCopy){
	if (toCopy == NULL)
		return NULL;
	Node *copyNode = new Node;
	copyNode->item = toCopy->item;
	copyNode->ticker = toCopy->ticker;
	copyNode->left = copyHelper(toCopy->left);
	copyNode->right = copyHelper(toCopy->right);
	return copyNode;
}
//End cpp 


In my main, I call something to this effect:
1
2
3
   BST treeA, treeB;
   constructBST(infile, treeA); //A helper to build BST from file
   BST copyCat(treeA);


Everything is fine and good, it compiles, it runs, even copies and passes an equal test, but then come the error messages.

I'm using MS Visual Studio 2010, and the window pops up and says
Microsoft Visual C++ Debug Library

Debug Assertion Failed!

Program: ...visual studio
2010\Projects\Muski_CS_BST\Debug\Muski_CS_BST.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line:52

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can cause assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

--Then it gives me abort, retry or ignore options. Retry just crashes it, abort crashes it, and ignore sens another message the same type, but the expression line is:

Expression:_CrtIsValidHeapPointer(pUserData)

Then if I hit ignore again it shows the original message and then crashes.

I am so beyond lost after days of tearing my hair out over this. I know the logic is right and I could write it without recursion easily but we're here to learn :)

Any help at all would be GREATLY appreciated! Thanks so much!
Jan 21, 2012 at 7:02pm
Without looking too deeply in your code, I'd say that line 25 is the guilty party. item is a pointer, meaning that line 25 simply duplicates the address of the item. I suppose that makeEmpty() deletes the object pointed to by item, so there's your isssue.
Jan 22, 2012 at 2:06am
That's definitely the culprit here. Any idea how I'd just get to the data. Basically the item is another class which holds a pointer to its data and I can't figure out how to get that deep when I copy it. The Generic class holds a private char *ptr which points to a char array and I can't seem to dereference that far.

It looks like I'm sending a Node to copy, but the Node has a pointer to a Generic which has a pointer to the actual data.

What I'm trying to do is something like
1
2
3
Generic copied(//Then the actual data to copy)
Node *copyNode = new Node;
copyNode->item = copied;


Just not sure how to do that.
Topic archived. No new replies allowed.