template <typename Key, typename Value> Node BST<Key, Value>::put( Node rootNode, Key newKey, Value newValue ) {//If a node with newKey exists in the tree rooted at rootNode, change the value of the node. If not, add it to the tree. if( newKey == null ) { return new Node( newKey, newValue, 1 ); } int compare = newKey.compareTo( rootNode.key ); if( compare < 0 ) { rootNode.left = put( rootNode.left, newKey, newValue ); } else if( compare > 0 ) { rootNode.right = put( rootNode.right, newKey, newValue ); } else { rootNode.value = newValue; } rootNode.N = size(rootNode.left) + size(rootNode.right) + 1; return rootNode; }
template <typename Key, typename Value> Key BST<Key, Value>::floor( Node rootNode, Key key ) { if( rootNode == null ) { return null; } int compare = key.compareTo( rootNode.key ); if( compare == 0 ) { return rootNode; } else if( compare < 0 ) { return floor( rootNode.left, key ); } Node t = floor( rootNode.right, key ); if( t != null ) { return t; } else { return rootNode; } }
template <typename Key, typename Value> Value Node<Key, Value>::get( Node subtreeNode, Key getKey ) {//Returns the value of the node with a specified key that is within the subtree rooted at subtreeNode. Returns null if the node isn't found. if( subtreeNode == null ) { return null; } int compare = getKey.compareTo( subtreeNode.key ); if( compare < 0 ) { return get( subtreeNode.left, getKey ); } else if( compare > 0 ) { return get( subtreeNode.right, getKey ); } else { return subtreeNode.value; } }
int LeafNode::insert(Record* record) { std::vector< Record * > result = find(record); if (result.size() != 0) return 3; //Key in level in inserted record ID Key* inRecordKey = record->getID()->getKey(level); bool inserted = false; std::vector<Record*>::iterator it = elements.begin(); while((it < elements.end()) && (!inserted)){ Key* elemKey = (*it)->getID()->getKey(level); if(inRecordKey->compareTo(elemKey) < 0){ elements.insert(it, record); numElements++; inserted = true; } else it++; } if(!inserted) { elements.push_back(record); numElements++; } occupiedSpace += record->size(); //Checks for overflow if (occupiedSpace > maxSize){ return 2; } return 1; }