void BSTree<DataType, KeyType>::copyHelper( BSTreeNode* parent, BSTreeNode* &destParent) { if(parent == NULL) { destParent = NULL; return; } destParent->dataItem = parent->dataItem; copyHelper(parent->left, destParent->left); copyHelper(parent->right, destParent->right); }
// overloaded assignment operator = BinTree& BinTree::operator=(const BinTree &rhs){ // check for self-assignment if(this != &rhs){ makeEmpty(); root = copyHelper( rhs.root ); } return *this; }
// copyHelper, recursive helper function for copy constructor BinTree::Node* BinTree::copyHelper( const Node* otherNode ) { Node* tempNode = new Node; // create a new Node NodeData* tempData = new NodeData; // create a new NodeData to put the data in *tempData = *otherNode->data; // copy over the data tempNode->data = tempData; if(otherNode->left != NULL){ tempNode->left = copyHelper(otherNode->left); // recursively copy left } else { tempNode->left = NULL; } if(otherNode->right){ tempNode->right = copyHelper(otherNode->right); // recursively copy right } else { tempNode->right = NULL; } return tempNode; // Returns after child nodes have been linked to newNode. }
QMimeData *ModelRewriter::copy(const QList<InternalNodeState::Pointer> &nodeStates) const { if (modificationGroupActive()) throw ModificationGroupException(__LINE__, Q_FUNC_INFO, __FILE__); const QString sourceCode = m_textModifier->text(); CopyHelper copyHelper(sourceCode); return copyHelper.createMimeData(nodeStates); }
CondimentListNode* CondimentList::copyHelper(CondimentListNode* &clNode) { if(head==nullptr) { return nullptr; } CondimentListNode* newNode = new CondimentListNode(clNode->condiment); newNode->next = copyHelper(clNode->next); return newNode; }
BSTree<DataType, KeyType>& BSTree<DataType, KeyType>::operator=( const BSTree<DataType,KeyType>& other ) { if(this == &other) { return *this; } clear(); copyHelper(other.root, root); return *this; }
const Pair<T1, T2>& Pair<T1, T2>:: operator=(const Pair<T1, T2>& rhsObj) { // Self-assignment check if (this == &rhsObj) { return (*this); } // Copy new memory copyHelper(rhsObj); return (*this); }
CondimentList::CondimentList(CondimentList* &cl) { head = copyHelper(cl->head); }
Pair<T1, T2>:: Pair(const Pair<T1, T2>& srcObj) { copyHelper(srcObj); }
// copy constructor BinTree::BinTree( const BinTree& otherTree ) { if (this != &otherTree) { root = copyHelper( otherTree.root ); // Starts recursively copying Nodes from // otherTree, starting with root Node. } }