示例#1
0
文件: tbst.cpp 项目: jamesus95/Cpp
/**
 * @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());
        }
    }
}
示例#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;
}
示例#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;
}
示例#4
0
文件: tbst.cpp 项目: jamesus95/Cpp
/**
 * @brief Recursively query for max NodeDatas from left/right subtrees
 * and compare.
 * @details private recursive method. Compares maximum candidates from the root
 * node, the left subtree, and the right subtree. O(n) running time.
 * 
 * @param subroot Node pointer to the root of the subtree currently recursed on
 * @return NodeData with maximum secondary value among the 3 candidates
 */
NodeData* TBST::getMaxByFrequency(Node* subroot) {
    if(subroot != NULL) {
        NodeData* maxData = subroot->getData(); // default max
        NodeData* leftMax = NULL; NodeData* rightMax = NULL;

        if(subroot->hasLeftChild())
            leftMax = getMaxByFrequency(subroot->getLeft());
        if(subroot->hasRightChild())
            rightMax = getMaxByFrequency(subroot->getRight());
        
        if(leftMax != NULL && maxData->getCount() < leftMax->getCount())
            maxData = leftMax;
        // break ties in favor of the right branch since we desire an
        // increasing sort order
        if(rightMax != NULL && maxData->getCount() <= rightMax->getCount())
            maxData = rightMax;
        
        return maxData;
    }
    else return NULL;
}
示例#5
0
文件: tbst.cpp 项目: jamesus95/Cpp
/**
 * @brief Recursively query for max NodeDatas from left/right subtrees
 * and compare.
 * @details private recursive method. Compares minimum candidates from the root
 * node, the left subtree, and the right subtree. Runs in O(n) time.
 * 
 * @param subroot Node pointer to the root of the subtree currently recursed on
 * @return NodeData with minimum secondary value among the 3 candidates
 */
NodeData* TBST::getMinByFrequency(Node* subroot) {
    if(subroot != NULL) {
        NodeData* minData = subroot->getData(); // default min
        NodeData* leftMin = NULL; NodeData* rightMin = NULL;

        if(subroot->hasLeftChild())
            leftMin = getMinByFrequency(subroot->getLeft());
        if(subroot->hasRightChild())
            rightMin = getMinByFrequency(subroot->getRight());
        
        // break ties in favor of the node on the left since we want increasing
        // sort order
        if(leftMin != NULL && minData->getCount() >= leftMin->getCount()) {
            minData = leftMin;
        }
        if(rightMin != NULL && minData->getCount() > rightMin->getCount()) {
            minData = rightMin;
        }
        return minData;
    }
    else return NULL;
}
示例#6
0
文件: tbst.cpp 项目: jamesus95/Cpp
/** 
 * @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;
    }
}