RE: [Dev-C++] Copy Constructor for a BST Class
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-06-09 03:06:56
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> 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
>=20
>=20
> I am using the most current version of DEV C++
> =20
> This is the code I am currently using for copy constructor on=20
> my Binary Search Tree code. I don't believe this should=20
> cause any problems but if anyone can give me help on this I=20
> would greatly appricate it. Keep in mind that the copy Tree=20
> function works on a recursive basis
>=20
> =20
>=20
> template <typename T> BST<T>::BST(const BST& origTree){ =20
> //copy constructor
>=20
> if(origTree.root){
>=20
> copyTree(origTree.root, root);
>=20
> }
>=20
> }
>=20
> =20
>=20
> =20
>=20
> /*************************************************************
> ***************
>=20
> copyTree function
>=20
> Used by the copy constructor to copy the tree
>=20
> Sent: pointer to the original node, pointer to the node being=20
> copied to
>=20
> Returns: Nothing
>=20
> Works by: Creating a new node on the tree being copied, copy=20
> the data to the
>=20
> node, checks to see if a left node exists on the tree, if yes
>=20
> repeat the process using on the left node, if not, do=20
> nothing. Then checks
>=20
> the right, if it exists repeat process on the right side, if=20
> not, do nothing
>=20
> NOTE: This function is a recursive process
>=20
> **************************************************************
> **************/
>=20
> =20
>=20
> template<typename T> void BST<T>::copyTree(Node* origNode,=20
> Node* copyNode){
>=20
> copyNode =3D new Node(origNode->data);
>=20
> =20
>=20
> if(origNode->left){
>=20
> copyTree(origNode->left, copyNode->left);
>=20
> }
>=20
> =20
>=20
> if(origNode->right){
>=20
> copyTree(origNode->right, copyNode->right);
>=20
> }
>=20
> }
>=20
> //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
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|