TreeNode* CopyTree(TreeNode* root) { if(root == NULL) return NULL; TreeNode* newRoot = new TreeNode(root->val); newRoot->left = CopyTree(root->left); newRoot->right = CopyTree(root->right); return newRoot; }
//二叉树的拷贝 SearchTree CopyTree(SearchTree tree) { if (NULL == tree) return NULL; SearchTree pleft = CopyTree(tree->left); SearchTree pright = CopyTree(tree->right); tree->left = pleft; tree->right = pright; return tree; }
Node<T>* RBTree<T>::CopyTree(Node<T>* sourcenode, Node<T>* parentnode){ if (sourcenode == NULL) { return NULL; } Node<T>* new_node = new Node<T>(sourcenode->data); new_node->p = parentnode; new_node->left = CopyTree(sourcenode->left, new_node); new_node->right = CopyTree(sourcenode->right, new_node); return new_node; }
//////////////////////////////////////////////////////////////////////////////////////////////////// // CopyTree: Copy Helper function copies a tree recursively // Pre Conditions: Trees are properly initialized // Post Conditions: current node is set to a copy of source Node //////////////////////////////////////////////////////////////////////////////////////////////////// void BinTree::CopyTree(Node* ¤t, const Node* sourceCurrent) { if(sourceCurrent == NULL) { current = NULL; } else { current = new Node; current->data = sourceCurrent->data; current->left = NULL; current->right = NULL; CopyTree(current->left, sourceCurrent->left); CopyTree(current->right, sourceCurrent->right); } }
Node<T>* RedBlackTree<T>::CopyTree(Node<T>* sourcenode, Node<T>* parentnode) { Node<T>* copynode; if (sourcenode != NULL) { copynode = new Node<T>(sourcenode->data); copynode->p = parentnode; copynode->is_black = sourcenode->is_black; //left and right copynode->left = CopyTree(sourcenode->left, copynode); copynode->right = CopyTree(sourcenode->right, copynode); return copynode; } //return NULL if sourcenode == NULL return NULL; }
/** * 习题6.46,用递归法复制一棵二叉树T到Tc */ void CopyTree(BiTree T, BiTree &Tc) { if (T) { Tc = (BiTree)malloc(sizeof(BiTNode)); Tc = T; Tc->data = T->data; Tc->lchild = T->lchild; Tc->rchild = T->rchild; if (T->lchild) //复制左子树 CopyTree(T->lchild, Tc->lchild); if (T->rchild) //复制右子树 CopyTree(T->rchild, Tc->rchild); } }
void CopyTree(TreeNode*& copy, const TreeNode* originalTree) // Post: copy is the root of a tree that is a duplicate // of originalTree. { if (originalTree == NULL) copy = NULL; else { copy = new TreeNode; copy->info = originalTree->info; CopyTree(copy->left, originalTree->left); CopyTree(copy->right, originalTree->right); } }
//////////////////////////////////////////////////////////////////////////////////////////////////// // Assignment Operator: assigns a tree (via CopyTree). // Pre Conditions: Trees are properly initialized // Post Conditions: copies the source tree into its own tree if they are not the same //////////////////////////////////////////////////////////////////////////////////////////////////// BinTree& BinTree::operator=(const BinTree &source) { if (&source != this) { makeEmptyHelper(this->root); CopyTree(this->root, source.root); } return *this; }
RedBlackTree<T>& RedBlackTree<T>::operator=(const RedBlackTree& rbtree) { if (this != &rbtree) { // Check to see that there is no self assignment. RemoveAll(); // Clean the entire tree. CopyTree(GetRoot(), rbtree.GetRoot(), rbtree.GetRoot()); // Copy everything from rbtree to this tree. } size = rbtree.Size(); // Explicitly changing this->size to be exactely like rbtree.Size() (Size is a getter method for size counter). return *this; }
Node<T>* RedBlackTree<T>::CopyTree(Node<T>* thisnode, Node<T>* sourcenode, Node<T>* parentnode) { Node<T>* nd = NULL; if (sourcenode != NULL) { // Do normal pre-order binary search tree insertion to make sure that I have the same // structure as the passed in tree, with a little addition of copying the passed in tree colors // to make an identical red-black tree copy. nd = BSTInsert(sourcenode->data); ++size; nd->is_black = sourcenode->is_black; CopyTree(NULL, sourcenode->left, parentnode); // I did not use thisnode argument for this function, CopyTree(NULL, sourcenode->right, parentnode); // so I call the next recursive function with NULL } // for thisnode argument. // I used parentnode as an indicator of the root for of rbtree so to make sure I set // the right root for this tree. if (sourcenode == parentnode) { root = nd; } return NULL; }
RedBlackTree<T> & RedBlackTree<T>::operator=(const RedBlackTree<T> & rbtree) { if (this != &rbtree) { RemoveAll(); this->root = CopyTree(rbtree.root, NULL); this->size = rbtree.size; } return *this; }
void TreeType::operator= (const TreeType& originalTree) // Calls recursive function CopyTree to copy originalTree // into root. { { if (&originalTree == this) return; // Ignore assigning self to self Destroy(root); // Deallocate existing tree nodes CopyTree(root, originalTree.root); } }
vector<TreeNode*> generateTreesInternal(int n) { vector<TreeNode*> ret; if(n == 0) { ret.push_back(NULL); return ret; } if(n == 1) { ret.push_back(new TreeNode(1)); return ret; } for(int i = 0; i <= n - 1; ++i) { vector<TreeNode*> leftRoot = generateTreesInternal(i); vector<TreeNode*> rightRoot = generateTreesInternal(n-1-i); for(int i = 0; i < leftRoot.size(); ++i) { for(int j = 0; j < rightRoot.size(); ++j) { TreeNode* root = new TreeNode(i+1); root->left = CopyTree(leftRoot[i]); root->right = CopyTree(rightRoot[j]); ret.push_back(root); } } for(int i = 0; i < leftRoot.size(); ++i) { ReleaseTree(leftRoot[i]); } for(int i = 0; i < rightRoot.size(); ++i) { ReleaseTree(rightRoot[i]); } } return ret; }
int MoveDirectory(const char* src_filename, const char* dest_filename) { char src_path[MAXPATH], src_file[MAXPATH]; char dest_path[MAXPATH],dest_file[MAXPATH]; char drive[MAXDRIVE], dir[MAXDIR], fname[MAXFILE], ext[MAXEXT]; SplitPath(src_filename, drive, dir, fname, ext); strcpy(src_path, drive); strcat(src_path, dir); strcpy(src_file, fname); strcat(src_file, ext); SplitPath(dest_filename, drive, dir, fname, ext); strcpy(dest_path, drive); strcat(dest_path, dir); strcpy(dest_file, fname); strcat(dest_file, ext); /* DRR: Ensure that I can move a dir to another drive * and with another name */ if (stricmp(src_file, dest_file)) { strcat(dest_path, dest_file); strcat(dest_path, DIR_SEPARATOR); strcpy(dest_file, src_file); } if (dir_exists(dest_filename)) if (!DelTree(dest_filename)) return 0; if (!CopyTree(src_path, src_file, dest_path, dest_file)) { DelTree(dest_filename); return 0; } return DelTree(src_filename); }
Tree& operator=(const Tree& tree) { Delete(root); CopyTree(tree); return *this; }
Tree(const Tree& tree) : root() { CopyTree(tree); }
/*-------------------------------------------------------------------------*/ static int CopyTree(const char *src_pathname, const char *src_filename, const char *dest_pathname, const char *dest_filename) { char filepattern[MAXPATH], new_src_pathname[MAXPATH], new_dest_pathname[MAXPATH], src_path_filename[MAXPATH], dest_path_filename[MAXPATH], tmp_filename[MAXFILE + MAXEXT], tmp_pathname[MAXPATH]; struct ffblk fileblock; int fileattrib, done; /* copy files in subdirectories */ strmcpy(filepattern, src_pathname, sizeof(filepattern)); strmcat(filepattern, src_filename, sizeof(filepattern)); done = findfirst(filepattern, &fileblock, FA_DIREC); while (!done) { if (fileblock.ff_attrib == FA_DIREC && strcmp(fileblock.ff_name, ".") != 0 && strcmp(fileblock.ff_name, "..") != 0) { /* build source pathname */ strmcpy(new_src_pathname, src_pathname, sizeof(new_src_pathname)); strmcat(new_src_pathname, fileblock.ff_name, sizeof(new_src_pathname)); strmcat(new_src_pathname, DIR_SEPARATOR, sizeof(new_src_pathname)); /* build destination pathname */ strmcpy(new_dest_pathname, dest_pathname, sizeof(new_dest_pathname)); strmcat(new_dest_pathname, fileblock.ff_name, sizeof(new_dest_pathname)); strmcat(new_dest_pathname, DIR_SEPARATOR, sizeof(new_dest_pathname)); if (!CopyTree(new_src_pathname, "*.*", new_dest_pathname, "*.*")) return 0; } done = findnext(&fileblock); } fileattrib = FA_RDONLY+FA_ARCH+FA_HIDDEN+FA_SYSTEM; /* find first source file */ strmcpy(filepattern, src_pathname, sizeof(filepattern)); strmcat(filepattern, src_filename, sizeof(filepattern)); done = findfirst(filepattern, &fileblock, fileattrib); /* check if destination directory must be created */ if (!dir_exists(dest_pathname)) { strmcpy(tmp_pathname, dest_pathname, sizeof(tmp_pathname)); if (makedir(tmp_pathname) != 0) { #ifdef USE_KITTEN error(1,28,"Unable to create directory"); #else error("Unable to create directory"); #endif return 0; } } /* copy files */ while (!done) { /* build source filename including path */ strmcpy(src_path_filename, src_pathname, sizeof(src_path_filename)); strmcat(src_path_filename, fileblock.ff_name, sizeof(src_path_filename)); /* build destination filename including path */ strmcpy(dest_path_filename, dest_pathname, sizeof(dest_path_filename)); build_filename(tmp_filename, fileblock.ff_name, dest_filename); strmcat(dest_path_filename, tmp_filename, sizeof(dest_path_filename)); if (!xcopy_file(src_path_filename, dest_path_filename)) return 0; done = findnext(&fileblock); } return 1; }
RedBlackTree<T>::RedBlackTree(const RedBlackTree<T>& rbtree) { root = CopyTree(rbtree.root, NULL); size = rbtree.size; }
RBTree<T>::RBTree(const RBTree<T>& rbtree) : size(0) { root = CopyTree(rbtree.GetRoot(), NULL); size = rbtree.Size(); }
RBTree<T>& RBTree<T>::operator=(const RBTree<T>& rbtree){ RemoveAll(); root = CopyTree(rbtree.GetRoot(), NULL); size = rbtree.Size(); return *this; }
RedBlackTree<T>::RedBlackTree(const RedBlackTree& rbtree) : root(NULL), size(0) { CopyTree(GetRoot(), rbtree.GetRoot(), rbtree.GetRoot()); size = rbtree.Size(); }
TreeType::TreeType(const TreeType& originalTree) // Calls recursive function CopyTree to copy originalTree // into root. { CopyTree(root, originalTree.root); }
//////////////////////////////////////////////////////////////////////////////////////////////////// // Copy Constructor: Makes a deep copy of an existing tree by calling helper function (CopyTree) // Pre Conditions: Source Tree is initialized properly // Post Conditions: New Binary tree is created //////////////////////////////////////////////////////////////////////////////////////////////////// BinTree::BinTree(const BinTree &source) { CopyTree(this->root, source.root); }
void CDragDropTreeCtrl::MoveTree(HTREEITEM hDest, HTREEITEM hSrc) { CopyTree(hDest, hSrc); DeleteItem(hSrc); }