本文整理汇总了C++中AvlTree类的典型用法代码示例。如果您正苦于以下问题:C++ AvlTree类的具体用法?C++ AvlTree怎么用?C++ AvlTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AvlTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
std::cout << "initiating..." << std::endl;
AvlTree tree;
//open the input file
std::ifstream inputFile;
inputFile.open("hw4_Q4_input.txt");
if (!inputFile)
std::cout << "Error opening the input file " << std::endl;
//open the output file, outputFile is defined as a global variable
outputFile.open("hw4_Q4_output.txt");
if (!outputFile)
std::cout << "Error opening the output file " << std::endl;
//read operations from the input file
std::string op;
int x;
while(inputFile >> op)
{
if (op == "insert")
{
inputFile >> x; // read the value x for insert
tree.insert(x);
}
else if (op == "remove")
示例2: main
int main(void){
cout << "This is an avl tree" << endl;
AvlTree* tree = new AvlTree();
cout << "please enter the node and -1 means find and -2 means delete :\n";
int data;
while (true){
cin >> data;
if (data == -1){
//test tree
cin >> data;
if(tree->nodeFind(tree->root, data)){
cout << "find " << data << " succeed!\n";
}else{
cout << "find " << data << " fail!\n";
}
continue;
}
if (data == -2){
cin >> data;
if (tree -> nodeDelete(tree->root , data)){
cout << "delete succeed\n";
}else{
cout << "delete fail\n";
}
tree->nodePrint(tree->root, 0, tree->root->data);
continue;
}
示例3: main
int main() {
int i;
int data [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
AvlTree *tree = new AvlTree();
for (i = 0; i < 10; i++) {
tree->insert(data + i);
}
return 0;
}
示例4:
AvlTree<KEY, VALUE>& AvlTree<KEY, VALUE>::GiveNullObject()
{
// The NULLOBJECT method must be overwritten in the derived classes to
// actually return an instance of the respective class with the null byte
// set to null otherwise mismatched class objects will be returned.
static AvlTree<KEY, VALUE> _NULLOBJECT;
_NULLOBJECT.SetAsNullObject();
return _NULLOBJECT;
}
示例5: commonThreeWordPhrase
void commonThreeWordPhrase(string word, vector<string>&arr1, vector<string>&arr2, vector<string>&arr3, AvlTree& tree){
string concatenate = "";
if(tree.isEmpty() == true){
if(arr1.size() == 0 && arr2.size() == 0 && arr3.size() == 0)
arr1.push_back(word);
else if(arr1.size() == 1 && arr2.size() == 0 && arr3.size() == 0){
arr1.push_back(word);
arr2.push_back(word);
}else if (arr1.size() == 2 && arr2.size() == 1 && arr3.size() == 0){
arr1.push_back(word);
arr2.push_back(word);
arr3.push_back(word);
if(arr1.size() == 3){
for (int i = 0; i < arr1.size(); i++)
i == 0 ? concatenate = arr1[i] : concatenate = concatenate + " " + (string)arr1[i];
tree.insert(concatenate);
arr1.clear();
}
}
}else{
if(arr1.size()!= 3 && word.length() > 0)
arr1.push_back(word);
if(arr2.size() != 3 && word.length() > 0)
arr2.push_back(word);
if(arr3.size() != 3 && word.length() > 0)
arr3.push_back(word);
if(arr1.size() == 3) {
for (int i = 0; i < arr1.size(); i++)
i == 0 ? concatenate = arr1[i] : concatenate = concatenate + " " + (string)arr1[i];
tree.insert(concatenate);
arr1.clear();
}
if (arr2.size() == 3) {
for (int i = 0; i < arr2.size(); i++)
i == 0 ? concatenate = arr2[i] : concatenate = concatenate + " " + (string)arr2[i];
tree.insert(concatenate);
arr2.clear();
}
if (arr3.size() == 3) {
for (int i = 0; i < arr3.size(); i++)
i == 0 ? concatenate = arr3[i] : concatenate = concatenate + " " + (string)arr3[i];
tree.insert(concatenate);
arr3.clear();
}
}
}
示例6: destroy_right
/* --- Function: static void destroy_right(AvlTree tree, AvlTreeNode node) --- */
static void destroy_right(AvlTree tree, AvlTreeNode node)
{
AvlTreeNode *position;
/* Destruction of an empty tree is not allowed.. */
if (tree->size == 0)
return;
/* Determine where to destroy nodes... */
if (node == NULL)
position = &tree->root;
else
position = &node->right;
/* Destroy the nodes... */
if (*position != NULL)
{
destroy_left(tree, *position);
destroy_right(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
tree->destroy((*position)->data);
}
/* Now, free the node itself... */
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the destroyed node... */
tree->size--;
}
}
示例7: main
int main(void)
{
cout<<"\n\n*********************************************************************\n";
cout<<"***** autor: *********************************** data: **************\n";
cout<<"***** Mateusz Buczynski ************************ 29.IV.2015 *********\n";
cout<<"*********************************************************************\n";
cout<<"*********************************************************************\n";
cout<<"***** MATERIALY POMOCNICZE: *****************************************\n";
cout<<"**************************** -www.cs.uah.edu ****************\n";
cout<<"**************************** -ILO Tarnow ****************\n";
cout<<"**************************** -Geeksforgeek.com ****************\n";
cout<<"*********************************************************************\n";
cout<<"*********************************************************************\n\n";
//Testy ***************************************
InsertTest();
RemoveTest();
FindTest();
o.PrinterAvlTree(); // mozna tez o.Print(AVL->root);
cout<<"*********************************************************************\n\n";
cout<<"*************************** KONIEC **********************************\n\n";
cout<<"*********************************************************************\n\n";
return 0;
}
示例8: openFile
bool openFile(const string& fileName, vector<Soundtrack>& cdVector, AvlTree<Soundtrack>& avlTree)
{
ifstream inputFile(fileName);
bool validData, ableToAdd;
if (inputFile.fail())
return false;
else
{
while (!inputFile.eof())
{
Soundtrack* cd = new Soundtrack();
validData = readInput(inputFile, *cd);
if (validData)
{
cdVector.push_back(*cd);
ableToAdd = avlTree.add(*cd);
if (!ableToAdd)
cout << *cd << " is a duplicate";
}
delete cd;
cd = nullptr;
}
inputFile.close();
}
return true;
} // end openFile
示例9: print
void print(const AvlTree<int>& tree) {
std::cout << "printing tree:\n";
std::vector<std::shared_ptr<Node<int>>> next_level { tree.get_root() };
uint level = 0;
while(next_level.size()) {
std::vector<std::shared_ptr<Node<int>>> temp {};
for(auto node : next_level) {
if(!node) {
continue;
}
std::cout << std::to_string(node->get_value()) << " -> [";
if(node->has_lson()) {
temp.push_back(node->get_lson());
std::cout << std::to_string(node->get_lson()->get_value());
}
if(!node->is_leaf()) {
std::cout << "; ";
}
if(node->has_rson()) {
temp.push_back(node->get_rson());
std::cout << std::to_string(node->get_rson()->get_value());
}
std::cout << "]" << std::endl;
}
next_level = temp;
std::cout << "\n" << std::endl;
}
}
示例10: main
int main(){
AvlTree<int> l;
for(int i = 1 ; i <= 15 ; i++){
l.Insert(i);
}
l.PrintTree();
while(true){
int toDelete;
cout<<"请输入要删除节点的值:"<<endl;
cin>>toDelete;
l.Delete(toDelete);
cout<<"删除后的树为:"<<endl;
l.PrintTree();
}
return 0;
system("PAUSE");
}
示例11: main
int main()
{
AvlTree<int, int> k;
for(int i=1;i<100;i++)
k.insert(i,i*10);
for(int i=1;i<100;i++)
cout << k.lookupNode(i) << endl;
for(int i=100;i<200;i++)
k.insert(i,i*10);
for(int i=1;i<200;i++)
cout << k.lookupNode(i) << endl;
for(int i=1;i<100;i++)
k.remove(i);
for(int i=1;i<200;i++)
cout << k.lookupNode(i) << endl;
return 0;
}
示例12: main
// Test program
int main( )
{
AvlTree<int> t;
int NUMS = 2000000;
const int GAP = 37;
int i;
cout << "Checking... (no more output means success)" << endl;
for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )
t.insert( i );
t.remove( 0 );
for( i = 1; i < NUMS; i += 2 )
t.remove( i );
if( NUMS < 40 )
t.printTree( );
if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
cout << "FindMin or FindMax error!" << endl;
for( i = 2; i < NUMS; i += 2 )
if( !t.contains( i ) )
cout << "Find error1!" << endl;
for( i = 1; i < NUMS; i += 2 )
{
if( t.contains( i ) )
cout << "Find error2!" << endl;
}
AvlTree<int> t2;
t2 = t;
for( i = 2; i < NUMS; i += 2 )
if( !t2.contains( i ) )
cout << "Find error1!" << endl;
for( i = 1; i < NUMS; i += 2 )
{
if( t2.contains( i ) )
cout << "Find error2!" << endl;
}
cout << "End of test..." << endl;
return 0;
}
示例13: main
int main(){
AvlTree<char> avl;
avl.insert('a');
avl.insert('b');
avl.insert('c');
avl.remove('b');
//cout << avl.toString();
RBTree<char> rb;
rb.insert('a');
rb.insert('b');
rb.insert('c');
cout << rb.toString();
//AvlTree<char>::Node* a = new AvlTree<char>::Node('a');
//AvlTree<char>::Node* b = new AvlTree<char>::Node('b');
//AvlTree<char>::Node* c = new AvlTree<char>::Node('c');
//c->left = a;
//a->right = b;
//cout << c->toString() << endl;
//c->left = a->rotateLeft();
//cout << c->toString() << endl;
}
示例14: main
int main(){
//Define some data
int data[]={21,13,29,8,18,26,32,5,11,16};
int data2[]={55,56,57};
//Define a new AVL tree
AvlTree<int> tree;
//Insert data with loop
for(int i=0;i<10;i++)
tree.insert(data[i]);
//Insert data with built-in loop
tree.insert(data2,3);
//Print a representation of the tree
tree.qtreePrint(cout);
cout<<endl<<endl;
//Remove some nodes
tree.remove(13);
tree.remove(26);
//Print another representation of the tree
tree.qtreePrint(cout);
//Print the number of nodes in the tree
cout<<endl<<endl<<"Size: "<<tree.size()<<endl;
cout<<"Value of top node: "<<tree.top()<<endl;
tree.pop();
cout<<"Value of top node: "<<tree.top()<<endl;
return 0;
}
示例15: lookup
/* --- Function: static int lookup(AvlTree tree, AvlTreeNode node, void **data) --- */
static int lookup(AvlTree tree, AvlTreeNode node, void **data)
{
int cmpval, retval;
if (node == NULL)
{
/* Return that the data was not found... */
return -1;
}
cmpval = tree->compare(*data, node->data);
if (cmpval < 0)
{
/* Move to the left... */
retval = lookup(tree, node->left, data);
}
else if (cmpval > 0)
{
/* Move to the right... */
retval = lookup(tree, node->right, data);
}
else
{
/* Node found - or hidden..! */
if (!(node->hidden) )
{
/* Pass back the data from the tree... */
*data = node->data;
retval = 0;
}
else
{
/* Return that the data was not found! */
return -1;
}
}
return retval;
}