Socha, David F
2003-06-08 12:31:07 UTC
I am using the most current version of DEV C++
This is the code I am currently using for copy constructor on my Binary Search Tree code. I don't believe this should cause any problems but if anyone can give me help on this I would greatly appricate it. Keep in mind that the copy Tree function works on a recursive basis
template <typename T> BST<T>::BST(const BST& origTree){ //copy constructor
if(origTree.root){
copyTree(origTree.root, root);
}
}
/****************************************************************************
copyTree function
Used by the copy constructor to copy the tree
Sent: pointer to the original node, pointer to the node being copied to
Returns: Nothing
Works by: Creating a new node on the tree being copied, copy the data to the
node, checks to see if a left node exists on the tree, if yes
repeat the process using on the left node, if not, do nothing. Then checks
the right, if it exists repeat process on the right side, if not, do nothing
NOTE: This function is a recursive process
****************************************************************************/
template<typename T> void BST<T>::copyTree(Node* origNode, Node* copyNode){
copyNode = new Node(origNode->data);
if(origNode->left){
copyTree(origNode->left, copyNode->left);
}
if(origNode->right){
copyTree(origNode->right, copyNode->right);
}
}
//End copyTree
Thanks for all the help,
David Socha
This is the code I am currently using for copy constructor on my Binary Search Tree code. I don't believe this should cause any problems but if anyone can give me help on this I would greatly appricate it. Keep in mind that the copy Tree function works on a recursive basis
template <typename T> BST<T>::BST(const BST& origTree){ //copy constructor
if(origTree.root){
copyTree(origTree.root, root);
}
}
/****************************************************************************
copyTree function
Used by the copy constructor to copy the tree
Sent: pointer to the original node, pointer to the node being copied to
Returns: Nothing
Works by: Creating a new node on the tree being copied, copy the data to the
node, checks to see if a left node exists on the tree, if yes
repeat the process using on the left node, if not, do nothing. Then checks
the right, if it exists repeat process on the right side, if not, do nothing
NOTE: This function is a recursive process
****************************************************************************/
template<typename T> void BST<T>::copyTree(Node* origNode, Node* copyNode){
copyNode = new Node(origNode->data);
if(origNode->left){
copyTree(origNode->left, copyNode->left);
}
if(origNode->right){
copyTree(origNode->right, copyNode->right);
}
}
//End copyTree
Thanks for all the help,
David Socha