typename TreeHelper<T>::Node * TreeHelper<T>::copySubTree(const Node* node) { if (nullptr == node) { return nullptr; } Node* left = copySubTree(node->left()); Node* right = copySubTree(node->right()); return new Node(node->value(), left, right); }
typename TreeHelper<T>::Tree* TreeHelper<T>::copyTree(const Tree* tree) { if (tree != nullptr) { Tree* treeCopy = new Tree(); treeCopy->root_ = copySubTree(tree->root()); return treeCopy; } return nullptr; }
BinarySearchTree<Key, Value>::BinarySearchTree (const BinarySearchTree<Key, Value>& other) { m_root=CreateNode(Key(), Value()); if (other.m_root->m_left!=NULL) { m_root->m_left=copySubTree(other.m_root->m_left); } }
BinarySearchTree<Key, Value>& BinarySearchTree<Key, Value>::operator= (const BinarySearchTree<Key, Value>& other) { if (this!=&other) { deleteSubTree(m_root->m_left); m_root->m_left=copySubTree(other.m_root->m_left); } return *this; }
typename BinarySearchTree<Key, Value>::BSTnode* BinarySearchTree<Key, Value>::copySubTree (typename BinarySearchTree<Key, Value>::BSTnode* subtree) { BSTnode* left=NULL, *right=NULL, *parent=NULL; if (subtree==NULL) { return NULL; } if (subtree->m_left==NULL) { left=NULL; } else { left=copySubTree(subtree->m_left); } if (subtree->m_right==NULL) { right=NULL; } else { right=copySubTree(subtree->m_right); } parent=CreateNode(subtree->m_key, subtree->m_value); parent->m_left=left; parent->m_right=right; if (left!=NULL) { left->m_parent=parent; } if (right!=NULL) { right->m_parent=parent; } return parent; }
inline BinarySearchTree<Key, Value> BinarySearchTree<Key, Value>::CopySubTree (typename BinarySearchTree<Key, Value>::Node& subTree) { BinarySearchTree<Key, Value> tmp; tmp.m_root->m_left=copySubTree(subTree->getSelf()); return tmp; }