Re: [Dev-C++] Copy Constructor for a BST Class 2
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-06-08 19:08:12
|
The code below should work ok for a copy constructor. My note about
assigning to a non-empty tree is based on the assumption that you would
like to use copyTree() to implement an assignment operator too, an then
the target BST might already contain a tree.
/Per W
On Sun, 8 Jun 2003, Socha, David F wrote:
> Would a copy constructor as such work:
>
>
> template <typename T> BST<T>::BST(const BST& origTree){ //copy constructor
>
>
> if(origTree.root){
>
> copyTree(origTree.root, root);
>
> }
>
> else{
>
> root = NULL;
>
> }
>
> }
>
>
> also, what do you mean by assigning to a non-empty tree. I would think that the copy constructor would simply make a fresh (new) copy of the tree. I dont understand why you would have to worry about releasing memory for a non empty tree
>
> thanks,
>
> David Socha
>
> here is the copy tree code again
>
>
> > > /****************************************************************************
> > >
> > > 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
>
>
>
>
> -------------------------------------------------------
> 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
>
|