Exemple #1
0
/**
 * @brief helper for copy-constructor
 * @details recursively copy nodes from a tree pre-order
 * 
 * @param origNode Node on the original tree to be copied over
 */
void TBST::recursiveCopy(Node* origNode) {
    if(origNode != NULL) { 
        if(origNode->isLeaf()) {
            NodeData* d = origNode->getData();
            insert(d->getToken(), d->getCount());
            d = NULL;
        } else if(!origNode->hasLeftChild()) {
            // 1: have right child, no left child
            // copy the node
            NodeData* d = origNode->getData();
            insert(d->getToken(), d->getCount());
            d = NULL;
            // then recurse down and copy the right child
            recursiveCopy(origNode->getRight());
        } else if(!origNode->hasRightChild()) {
            // 2: have left child, no right child
            NodeData* d = origNode->getData();
            insert(d->getToken(), d->getCount());
            d = NULL;
            // then recurse down and copy the left child
            recursiveCopy(origNode->getLeft());
        } else {
            // 3: have children on both sides
            NodeData* d = origNode->getData();
            insert(d->getToken(), d->getCount());
            d = NULL;
            // then recurse down and copy both children
            recursiveCopy(origNode->getLeft());
            recursiveCopy(origNode->getRight());
        }
    }
}
Exemple #2
0
NodeData testSetters(NodeData& B) {
    NodeData A("a");
    A.decreaseCount();
    assert(A.getCount() == 0);
    
    int bCount = 7;
    while(B.getCount() < bCount) {
        B.increaseCount();
    }
    assert(B.getCount() == bCount);

    assert(A.getToken() < B.getToken());

    A = B;
    assert(A.getToken() == B.getToken());    
    return B;
}
Exemple #3
0
NodeData testConstructors() {
    NodeData A;
    assert(A.getToken() == "");
    assert(A.getCount() == 1);
    NodeData B("hello world");
    assert(B.getToken() == "hello world");
    NodeData C(B);
    assert(C.getToken() == "hello world");
    return B;
}
Exemple #4
0
/** 
 * @brief copy constructor
 * @details creates a copy of the passed in TBST
 * 
 * @param other TBST tree to copy
 */
TBST::TBST(const TBST& other) {
    root = NULL;
    nodeCount = 0;
    if(other.root != NULL) {
        // handle root separately
        assert(root == NULL); // make sure that tree is empty
        // then add the root
        NodeData* rootData = other.root->getData();
        insert(rootData->getToken(), rootData->getCount());

        // recursively copy the left and right subtrees
        recursiveCopy(other.root->getLeft());
        recursiveCopy(other.root->getRight());
    }
    else {
        root = NULL;
    }
}