void BinarySearchTree<T>::copyTree(Node<T> *curNode) { if(curNode) { insert(curNode->getElement()); copyTree(curNode->getLeft()); copyTree(curNode->getRight()); } }
BinaryNodeTree<ItemType>::BinaryNodeTree(const ItemType& rootItem, const BinaryNodeTree<ItemType>* leftTreePtr, const BinaryNodeTree<ItemType>* rightTreePtr) { rootPtr = new BinaryNode<ItemType>(rootItem, copyTree(leftTreePtr->rootPtr), copyTree(rightTreePtr->rootPtr)); } // end constructor
// create a copy of a binary tree TreeNode* copyTree(TreeNode* root) { if(root == NULL) return NULL; TreeNode* p = (TreeNode*)malloc(sizeof(TreeNode)); p->val = root->val; p->left = copyTree(root->left); p->right = copyTree(root->right); return p; }
// Walking down the tree and linearize it void copyTree(Node * a, struct Line * l) { if (a == NIL) return; l->m_items[l->pos].count = a->count; l->m_items[l->pos].barCode = a->data; ++l->pos; copyTree(a->left, l); copyTree(a->right, l); }
void BSTree::copyTree(Node *treeP,Node *&newP){ if(treeP != NULL){ newP = new Node(treeP->customer,NULL,NULL); copyTree(treeP->left,newP->left); copyTree(treeP->right,newP->right); } else{ newP = NULL;//copy empty tree. } }//end copytree
KDNode* KDTree::copyTree(KDNode *node) { if(node == NULL) return NULL; KDNode *newNode = new KDNode; newNode->objects = node->objects; newNode->split_pos = node->split_pos; newNode->is_leaf = node->is_leaf; newNode->left = copyTree(node->left); newNode->right = copyTree(node->right); return newNode; }
//---------------------------------------------------------------------------- // copyTree // Makes an exact copy of the BST using a preorder traversal void BinTree::copyTree(Node* nodePtr, Node*& newNodePtr) const { if (nodePtr != NULL) { newNodePtr = new Node; // create new memory for Node NodeData* temp = new NodeData(*nodePtr->data); // new memory for data newNodePtr->data = temp; // copy data copyTree(nodePtr->left, newNodePtr->left); copyTree(nodePtr->right, newNodePtr->right); } else newNodePtr = NULL; // copy empty tree }
void CTree<type>::copyTree(TreeNode_t<type>** dst,TreeNode_t<type>* src) { TreeNode_t<type>* newNode; if(src==nullptr) { *dst=nullptr; return; } *dst=new TreeNode_t<type>; m_cpyRoute(&(*dst)->elem,&src->elem); copyTree(&(*dst)->firstChild,src->firstChild); copyTree(&(*dst)->nextSibling,src->nextSibling); }
void BST::copyTree(TreeNode *treePtr, TreeNode *& newTreePtr) const { // preorder traversal if (treePtr != NULL) { // copy node newTreePtr = new TreeNode(treePtr->item, NULL, NULL); copyTree(treePtr->leftChildPtr, newTreePtr->leftChildPtr); copyTree(treePtr->rightChildPtr, newTreePtr->rightChildPtr); } else newTreePtr = NULL; // copy empty tree } // end copyTree
struct Node* copyTree(struct Node *x) { if(x==NULL)return NULL; struct Node *y=newNode(x->ch,NULL,NULL); if(y->ch<256) { isinput[y->ch]=1; y->n=++cc; C[y->n]=y->ch; } y->left=copyTree(x->left); y->right=copyTree(x->right); return y; }
BinaryNode<ItemType>* BinaryNodeTree<ItemType>::copyTree(const BinaryNode<ItemType>* treePtr) const { BinaryNode<ItemType>* newTreePtr = nullptr; // Copy tree nodes during a preorder traversal if (treePtr != nullptr) { // Copy node newTreePtr = new BinaryNode<ItemType>(treePtr->getItem(), nullptr, nullptr); newTreePtr->setLeftChildPtr(copyTree(treePtr->getLeftChildPtr())); newTreePtr->setRightChildPtr(copyTree(treePtr->getRightChildPtr())); } // end if return newTreePtr; } // end copyTree
void binaryTreeType<elemType>::copyTree(nodeType<elemType>* &copiedTreeNode,nodeType<elemType>* otherTreeNode) { if(otherTreeNode==NULL) { copiedTreeNode=NULL; return; } copiedTreeNode=new nodeType<elemType>; copiedTreeNode->info=otherTreeNode->info; copyTree(copiedTreeNode->lLink,otherTreeNode->lLink); copyTree(copiedTreeNode->rLink,otherTreeNode->rLink); return; }
BinaryTree* copyTree(BinaryTree * tree){ BinaryTree* res = malloc(sizeof(BinaryTree)); res->value = tree->value; if(leftChild(tree) != NULL) res->Left = copyTree(tree->Left); else res->Left = NULL; if(rightChild(tree) != NULL) res->Right = copyTree(tree->Right); else res->Right = NULL; return res; }
void CBinaryTree<type>::copyTree(nodeType<type>** copiedTreeNode,nodeType<type>* otherTreeNode) { if(otherTreeNode==NULL) { *copiedTreeNode=NULL; return; } *copiedTreeNode=new nodeType<type>; memcpy(&(*copiedTreeNode)->elem,&otherTreeNode->elem,sizeof(type)); copyTree(&(*copiedTreeNode)->lLink,otherTreeNode->lLink); copyTree(&(*copiedTreeNode)->rLink,otherTreeNode->rLink); return; }
BinaryNode* BinaryTree::copyTree(const BinaryNode* nodePtr) { if (nodePtr == 0) { return 0; } else { // make a new node BinaryNode* newNode = new BinaryNode(nodePtr->getWebsite()); newNode->setLeftPtr(copyTree(nodePtr->getLeftPtr())); newNode->setRightPtr(copyTree(nodePtr->getRightPtr())); return newNode; } }
BinaryNodeTree<ItemType>& BinaryNodeTree<ItemType>::operator=( const BinaryNodeTree<ItemType>& rightHandSide) { if (!isEmpty()) clear(); this = copyTree(&rightHandSide); return *this; } // end operator=
BST& BST::operator=(const BST& rhs) { if (this != &rhs) { destroyTree(root); // deallocate left-hand side copyTree(rhs.root, root); // copy right-hand side } // end if return *this; } // end operator=
KDTree & KDTree::operator=(const KDTree &other) { if(this != &other) { deleteTree(this->root); this->root = copyTree(other.root); this->bounds = other.bounds; } return *this; }
//---------------------------------------------------------------------------- // operator= // Creates a deep copy of RHS. BinTree& BinTree::operator=(const BinTree& rhs) { if (this != &rhs) { destroyTree(root); // destroy LHS copyTree(rhs.root, root); // make a deep copy } return *this; }
binaryTreeType<elemType>::binaryTreeType(const binaryTreeType<elemType>& otherTree) { if(otherTree.root==NULL) { root=NULL; return; } copyTree(root,otherTree); }
CBinaryTree<type>::CBinaryTree(const CBinaryTree<type>& otherTree) { if(otherTree.m_root==NULL) { m_root=NULL; return; } m_printRoute=otherTree.m_printRoute; copyTree(&m_root,otherTree); }
// Linearizing the tree into array of lCouple structures struct Line * treeLine(void) { struct Line * res = malloc(sizeof(struct Line)); if (!res) err(EMEM); res->count = lengthTree(root); res->m_items = malloc(res->count * sizeof(struct lCouple)); res->pos = 0; copyTree(root, res); return res; }
BinaryTree & BinaryTree::operator= (const BinaryTree & sourceTree) { // clear data first(if the left object is not empty) if (!this->isEmpty()) { this->clear(); } rootPtr = copyTree(sourceTree.rootPtr); count = sourceTree.count; return *this; }
CTree<type>::CTree(const CTree<type>& object) { if(object.IsEmpty()) { m_root=nullptr; return; } copyTree(&m_root,object.m_root); return; }
BinaryTree::BinaryTree(const BinaryTree & tree) { // check if the tree is valid if (tree.isEmpty()) { rootPtr = 0; count = 0; } else { // make the pointer pointing to the tree pointer. rootPtr = copyTree(tree.rootPtr); } }
//return a tree resulted of applying resolution rule tree resolution(tree t1, tree t2, tree model, int axiom_id) { tree result = malloc(sizeof(tree_node)); int i; bool temp = model -> childs[0] -> negative; n_resolutions++; //change to match if trees are equal model -> childs[0] -> negative = !model -> childs[0] -> negative; //head of the tree strcpy(result -> label, "@"); result -> n_childs = 0; result -> axiom_id = axiom_id + 1; //copy to the result tree all the clauses that dont start with the label for(i = 0; i < t1 -> n_childs; i++) { if(!compareTrees(t1 -> childs[i], model)) { result -> childs[result -> n_childs] = copyTree(t1 -> childs[i], NULL); result -> childs[result -> n_childs] -> axiom_id = axiom_id + 1; result -> n_childs++; } } for(i = 0; i < t2 -> n_childs; i++) { if(!compareTrees(t2 -> childs[i], model)) { result -> childs[result -> n_childs] = copyTree(t2 -> childs[i], NULL); result -> childs[result -> n_childs] -> axiom_id = axiom_id + 1; result -> n_childs++; } } model -> childs[0] -> negative = temp; return result; }
const binaryTreeType<elemType>& binaryTreeType<elemType>::operator=(const binaryTreeType<elemType>& otherTree) { if(this==otherTree) return this; if(root!=NULL) destroy(root); if(otherTree.root==NULL) { root=NULL; return; } copyTree(root,otherTree); }
const CTree<type>& CTree<type>::operator=(const CTree<type>& object) { if(this==&object) return *this; destroy(&m_root); if(object.IsEmpty()) { m_root=nullptr; return *this; } copyTree(&m_root,object.m_root); return *this; }
const CBinaryTree<type>& CBinaryTree<type>::operator=(const CBinaryTree<type>& otherTree) { if(this==otherTree) return this; if(m_root!=NULL) destroy(&m_root); if(otherTree.m_root==NULL) { m_root=NULL; return; } m_printRoute=otherTree.m_printRoute; copyTree(m_root,otherTree); }
BinarySearchTree<T>& BinarySearchTree<T>::operator=(BinarySearchTree& bb) { destroyTree(); Node<T> *curNode = bb.getRoot(); copyTree(curNode); }