Discussion:
[Dev-C++] Copy Constructor for a BST Class
Socha, David F
2003-06-08 12:31:07 UTC
Permalink
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
Per Westermark
2003-06-08 13:51:04 UTC
Permalink
Your copy constructor - and your copyTree() method - would not work.

What if you make assign to a non-empty tree. In that case, you have to
release the old tree before assigning the new.

Also, you only handle the case where origTree does contain anything. If it
doesn't, then the target tree should have the same pointer set to NULL.

/Per W
Post by Socha, David F
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 SF.net email is sponsored by: Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Per Westermark
2003-06-08 14:05:01 UTC
Permalink
Slight clarification.

Your copy constructor doesn't have to be prepared to release any old
memory - but should be prepared to assign NULL values.

However, if you want to use copyTree() to implement an assignment
operator, then you must also think about releasing an old tree.

/Per W
Post by Per Westermark
Your copy constructor - and your copyTree() method - would not work.
What if you make assign to a non-empty tree. In that case, you have to
release the old tree before assigning the new.
Also, you only handle the case where origTree does contain anything. If it
doesn't, then the target tree should have the same pointer set to NULL.
/Per W
Post by Socha, David F
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 SF.net email is sponsored by: Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Ioannis Vranos
2003-06-09 01:07:04 UTC
Permalink
-----Original Message-----
Of Socha, David F
Sent: Sunday, June 08, 2003 5:26 PM
To: Dev C++ Mailing List
Subject: [Dev-C++] Copy Constructor for a BST Class
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
The code looks safe with the serious exception that you do not handle memory
allocation failure (an std::bad_alloc exception).





Ioannis Vranos

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

Loading...