本文整理汇总了C++中BSTNode类的典型用法代码示例。如果您正苦于以下问题:C++ BSTNode类的具体用法?C++ BSTNode怎么用?C++ BSTNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BSTNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: breadthFirstTraversal
int BinarySearchTree :: breadthFirstTraversal (int number) const {
Queue queue;
BSTNode* current = root;
queue.add(root);
int count = 0;
while (current != NULL) {
//If there is right child, add it to queue
if (current -> getLeft() != 0)
queue.add(current -> getLeft());
//If there is right child, add it to queue
if (current -> getRight() != 0)
queue.add(current -> getRight());
queue.removeFromHead(current);
if (current->getValue() == number) {
return count;
}
current = getBSTNodeFromQueue(queue);
count++;
}
return count;
}// end breadthFirstTraversal
示例2: main
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;
}
示例3: splay
void SplayTree::splay(BSTNode* aNode){
if(aNode ==NULL || aNode->parent() ==NULL){ //two special cases
return;
}
BSTNode* parent = aNode->parent();
BSTNode* grandParent = parent->parent();
cout<<"splaying the node: "<<aNode->key()<<endl;
while(parent != NULL && grandParent != NULL){
//all zigzig zigzag
zigzigzigzag(aNode);
parent = aNode->parent(); //updating parent and grandparent
if(parent ==NULL){
break; //effectively end the function
}
grandParent = parent->parent();
if(grandParent == NULL){
break;
}
}
if(grandParent == NULL && parent != NULL){ //parent is the root
if(parent->left() ==aNode){
zigLR(aNode, parent);
}else{
zigRL(aNode, parent);
}
}
debug_print(); //finly, dump the tree structe in Graphviz format
}
示例4: if
/*
* traverses the tree and removes the node containing the target
* integer if present and returns true
* return false if target integer is not in tree (or the tree is empty)
*/
bool BSTree::Remove(int content, BSTNode*& node)
{
if(node == NULL)
{
return false;
}
else
{
if(node->contents() == content && size_ == 1) //checking if root needs to be removed
{
delete node;
node = NULL;
size_ = 0;
return true;
}
else if(content < node->contents()) //if less than node traverse to the left
{
Remove(content, node->left_child());
}
else if(content > node->contents()) //if greater than node traverse to the right
{
Remove(content, node->right_child());
}
else //if the node is equal to content
{
if(node->left_child() == NULL && node->right_child() == NULL) //if the node to remove has no left/right child
{
delete node;
node = NULL;
}
else if(node->left_child() == NULL) //node to be removed has a right subtree
{
BSTNode* temp = node;
node = node->right_child();
delete temp;
}
else if(node->right_child() == NULL) //node to be removed has a left subtree
{
BSTNode* temp = node;
node = node->left_child();
delete temp;
}
else if(root_->contents() == node->contents())
{
BSTNode* temp = new BSTNode(FindMin(node->left_child()));
node->set_contents(temp->contents());
delete temp;
node->left_child() = NULL;
}
else
{
BSTNode* temp = new BSTNode(FindMin(node->right_child()));
node->set_contents(temp->contents());
delete temp;
}
size_--;
return true;
}
}
}
示例5: mapNode
void WordIndex::Insert (const Word & word, const URL & url) {
bool containsWord = Map<Word, OccurrenceSet>::Contains(word);
if (true == containsWord) {
// The word is already in this index -- increment occurrence
OccurrenceSet dummySet;
MapNode<Word, OccurrenceSet> mapNode(word, dummySet);
BSTNode< MapNode<Word, OccurrenceSet> >* node =
BST< MapNode<Word, OccurrenceSet> >::Find(mapNode);
Occurrence wrapper(url);
BSTNode<Occurrence> * oNode = node->GetValue().GetValue().Find(wrapper);
if (NULL != oNode) {
// word occurred on a known web page
oNode->GetValue().increment();
} else {
// word has an occurrence on a new web page
bool wasInserted = node->GetValue().GetValue().Insert(wrapper);
assert(wasInserted == true);
}
} else {
// We need to add the word to this index
OccurrenceSet set;
Occurrence occurrence(url);
bool wasAdded = set.Insert(occurrence);
assert(wasAdded == true);
Map<Word, OccurrenceSet>::Insert(word, set);
}
}
示例6: BSTNode
/**
* creates a new BSTNode, Inserts it into the tree, and returns true
* if the integer is already in the tree, does not Insert, and returns false
*/
bool BSTree::Insert(int contents, BSTNode*& root)
{
BSTNode* newNode = new BSTNode(contents);
if (root == NULL)
{
root = newNode;
size_ += 1;
return true;
}
else if (newNode->contents() < root->contents())
{
// if (root->left_child() == NULL)
// {
// root->set_left_child(newNode);
// size_ += 1;
// }
// else
Insert(contents, root->left_child());
}
else if (newNode->contents() > root->contents())
{
Insert(contents, root->right_child());
}else
return false;
}
示例7: find
//------------->>>>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;
}
示例8: successor
BSTNode<Data>* successor() {
/* Determine if the right child or parent is successor. If neither, and
parent exists, find out if one of the parents ancestors is the successor.
If no successor exists, return 0. */
if (right!=NULL) {
return right->leftmostNode();
} else if (parent==NULL) {
return NULL;
} else if (this==parent->left) {
return parent;
} else {
return parent->ancestralSuccessor();
}
}
示例9: newNode
void Crawler::addWords(BST < Pair < string,int > >* newOccurrences, string url){
BSTIterator<Pair <string,int> > iter = newOccurrences->Iterator();
BSTNode<Pair<string,int> > newNode(Pair<string,int>("",-1));
BSTNode<Word>* oldNode;
Occurrence occ;
occ.setURL(url);
while(iter.hasNext()){
newNode = iter.next();
//is either a new node or an old node
oldNode = words->Insert(Word(newNode.GetValue().getFirst()));
occ.setOccurrences(newNode.GetValue().getSecond());
oldNode->GetValue().addOccurrence(occ);
}
}
示例10: zigLR
void SplayTree::zigLR(BSTNode* aNode, BSTNode* aParent){
BSTNode* rightChild = aNode->right();
aNode->setRight(aParent);
aParent->setParent(aNode);
aParent->setLeft(rightChild);
if(rightChild != NULL){
rightChild->setParent(aParent);
}
if(root() == aParent){ //updating the root
setRoot(aNode);
aNode->setParent(NULL);
}
// debug_print();
}
示例11: zigRL
void SplayTree::zigRL(BSTNode* aNode, BSTNode* aParent){
BSTNode* leftChild = aNode->left();
aNode->setLeft(aParent);
aParent->setParent(aNode);
aParent->setRight(leftChild);
if(leftChild != NULL){
leftChild->setParent(aParent);
}
if(root() == aParent){
setRoot(aNode);
aNode->setParent(NULL);
}
// debug_print();
}
示例12: load
void SplayTree::load(string aKey, string aSong){
cout<<"in splaytree load()"<<endl;
if(!SplayTree::find(aKey)){ //if the entry is already in the list
cout<<"key not in tree"<<endl;
BST::insert(aKey); //insert the key into BST
cout<<"inseted a new key!"<<endl;
}
BSTNode* node = BST::find(aKey);
cout<<"found the key"<<endl;
splay(node); //splay to the root
cout<<"splayed the node "<<endl;
node->addSong(aSong);
cout<<"song added"<<endl;
}
示例13: add
void add(int value) {
if (root) {
root->add(value);
} else {
root = new BSTNode(value);
}
}
示例14: leftmostNode
/** Return the leftmost node of the subtree.
* PRECONDITION: this BSTNode is a node in a BST.
* POSTCONDITION: the BST is unchanged.
* RETURNS: The BSTNode that is the leftmost node of the subtree.
*/
BSTNode<Data>* leftmostNode() {
/* if this has a left child, recurse on that. Otherwise, return this. */
if (left!=NULL) {
return left->leftmostNode();
} else {
return this;
}
}
示例15: successor
BSTNode<Data>* successor() {
if (right!= NULL){
return right->lastLeftNode();
}
else if (parent == NULL) { return 0; }
else if (this == parent ->left) { return parent; }
else { return parent -> lastSucc(); }
}