|
From: Per W. <pw...@ia...> - 2003-06-09 14:34:18
|
The only thing you might check for is of "origTree.root == root". In that
case either both trees are empty, or you have done "mytree = mytree;"
Without the test, your code would always destroy the tree for the
"mytree = mytree;" case.
/Per W
On Mon, 9 Jun 2003, Socha, David F wrote:
> With an assignment operator for a user defined class, a binary Search
> Tree class for example, would it be a problem if my overloaded
> assignment operator only handled cases where you were assigning one tree
> to another.
>
> Here is some sample code:
>
> /****************************************************************************
> Overloaded assignment operator
> Used for: assigning the value of one tree to another tree
> Sent: A reference to the original tree
> Returns: A reference to a tree
> Works by: First checking to see if the tree bieng copyied to has data in it.
> If it does, the deleteTree function is called to empty the tree/free up all the
> memory in the tree.
> Second, checks to see if the tree being copyied has any data in it. If it
> does, the copyTree function is called to copy the data from the original tree to
> the copy tree. If the original tree does not have data on it (root == NULL)
> then the root of the copy tree will be set to null.
> ****************************************************************************/
>
> template<typename T> BST& BST<T>::operator= (const BST& origTree){
> if(root){
> destroyTree(root);
> }
>
> if(orig.root){
> copyTree(origTree.root, root);
> }
> else{
> root=NULL;
> }
>
> return *this;
> }
> //End operator=
>
> /****************************************************************************
> copyTree function
> Used by the copy constructor / asignment operator 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
>
> I am using the version 4.9.8.0 of Dev C++
>
> Should this work or will I have a problem with this.
> Thanks,
> 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
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|