Example #1
0
int main(int argc, const char *argv[])
{
	BST *bst = new BST();
	int array[] = {6, 4, 9, 2, 5, 8, 10, 1, 3, 7 };
	for (int i = 0; i < sizeof(array)/sizeof(int); ++i) {
		bst->insert(new BSTNode(array[i]));
	}
	std::cout << "In-Order:" << std::endl;
	inorder(bst->root());
	std::cout << "Pre-Order:" << std::endl;
	preorder(bst->root());
	std::cout << "Post-Order:" << std::endl;
	postorder(bst->root());
	std::cout << "MAX: " << bst->max()->key() << std::endl;
	std::cout << "MIN: " << bst->min()->key() << std::endl;
	BSTNode *notexist = bst->search(11);
	std::cout << "Key 11 is " << (notexist == NULL ? "NOT existed" : "existed") << std::endl;
	BSTNode *nodes = bst->search(7);
	std::cout << "Successor of " << nodes->key() << " is " << bst->successor(nodes)->key() << std::endl;
	BSTNode *nodep = bst->search(3);
	std::cout << "Predecessor of " << nodep->key() << " is " << bst->predecessor(nodep)->key() << std::endl;
	BSTNode *suMAX = bst->successor(bst->max());
	std::cout << "MAX Successor " << (suMAX == NULL ? "PASS" : "FAIL") << std::endl;
	BSTNode *preMIN = bst->predecessor(bst->min());
	std::cout << "MIN Predecessor " << (preMIN == NULL ? "PASS" : "FAIL") << std::endl;

	bst->remove(bst->search(11));
	std::cout << "In-Order:" << std::endl;
	inorder(bst->root());

	delete bst;
	return 0;
}
Example #2
0
void SplayTree::zigzigzigzag(BSTNode* aNode){
    BSTNode* parent = aNode->parent();
    BSTNode* grandParent = parent->parent();
    if(parent ==NULL || grandParent==NULL){
	return;
    }
    if(grandParent->right() == parent && parent->left() ==aNode){
	cout<<"performing zigLRzagRL "<<endl;
	zigLRzagRL( aNode, parent, grandParent);
	return;
    }
    else if(grandParent->left() == parent && parent->right() ==aNode){
	cout<<"performing zagRLzigLR "<<endl;
	zagRLzigLR ( aNode, parent, grandParent);
	return;
    }
    else if(grandParent->left() == parent && parent->left() ==aNode){
	cout<<"performing zigLRzigLR "<<endl;
	zigLRzigLR( aNode, parent, grandParent);
	return;
    }
    else if(grandParent->right() == parent && parent->right() ==aNode){
	cout<<"performing zigRLzigRL "<<endl;
	zigRLzigRL ( aNode, parent, grandParent);
	return;
    }
    else 
	cout<<"sum sing wong: "<<aNode->key()<<"'s parent: "<<parent->key()<<" grandparent: "<<grandParent->key()<<endl;
    return;

} 
Example #3
0
void insertWithGrouping(SplayTree<string, Array<string> >& aSplayTree,
			string aArtist,
			string aMusicEntry)
{
    static int debug_count = 0;
    ostringstream os;
    os << "debug" << debug_count++ << ".dot";
    string filename = os.str();
 
    // Here aArtist is the key and aMusicEntry contributes toward the value
    BSTNode<string, Array<string> >* place = aSplayTree.findInsertionPoint(aArtist);
    if (place == nullptr) {
        // First node to be inserted
        Array<string> musicList;
        musicList.add(aMusicEntry);
        aSplayTree.setRoot(new BSTNode<string, Array<string> >(aArtist, musicList));
    } else if (aArtist == place->key()) {
        // Key matched
        // aArtist exists -- just group the current aMusicEntry with the existing
        // associated musicList
        Array<string> musicList = place->value();
        // Aggregation
        musicList.add(aMusicEntry);
        // Replace the value with the updated musicList
        place->setValue(musicList);
	// Splay this node as it is just augmented
	aSplayTree.splay(place);
    } else {
        // Key not matched
        // So, this is going to be the first music entry for aArtist
        Array<string> musicList;
        musicList.add(aMusicEntry);
        BSTNode<string, Array<string> >* newNode =
            new BSTNode<string, Array<string> >(aArtist, musicList,
                                                nullptr, nullptr, place);

        if (aArtist < place->key()) {
            // Node on the left of place
            place->setLeft(newNode);
        } else {
            // Node on the right of place
            place->setRight(newNode);
        }
	// dumpSplayTree(aSplayTree, filename);
        aSplayTree.splay(newNode);
    }
}
Example #4
0
void SplayTree::search(string aKey) { //search a musician
    BSTNode* node = BST::find(aKey);//this is the splayTree find()
    if(node ==NULL){
	cout<<"playlist is currently empty"<<endl;
	return;
    }
  
    splay(node); //splay it to the root
  
    if(node->key() == aKey){ //using internal overloaded string comparison
	cout<<"Yes, the musician: "<<aKey<<" is in your playlist and plays: ";
	node->outputSong(); //print the songs
	cout<<endl;
    } else{
	cout<<aKey<<" is currently not in the playlist"<<endl;
	cout<<"The closest search result is: "<<node->key()<<endl;
    }        
}
Example #5
0
//delete the entry
void SplayTree::cut(string aKey){
    cout<<"<<<------internal transcript----->>>"<<endl;
    BSTNode* node = BST::find(aKey); //the node contains the exact key
    if((node->key()!= aKey) || node ==NULL ){ 
	splay(node); //splay it anyway [2]
	cout<<"\nsorry, the musician: "<<aKey<<" is not in the list"<<endl;
	return;
    }
    if(node->parent() != NULL  && (node->key() ==aKey)){
	cout<<"node: "<<node->key()<<" has parent"<<endl; 
	node = node->parent(); //node now pting to its parent
	cout<<"node now pting to its parent: "<<node->key()<<endl;
	BST::remove(aKey); //remove the key
	splay(node); //splay the parent to the root [2]
    }else if (node->parent() ==NULL){ //in the case node is the root
	BST::remove(aKey);
    }
    cout<<"delete sucessful!"<<endl;
}
Example #6
0
    //------------->>>>splay tree funcs
bool SplayTree::find(string aKey){ //find a node, internal function, without splay
    cout<<"in splaytree::find()"<<endl;
    BSTNode* node = BST::find(aKey); //return the node or a node nearby
    cout<<"returning a node"<<endl;
    if(node ==NULL){ //in the case of empty tree
	return false;
    }
    if(node->key()==aKey){
	return true;
    }
    return false;
}