Esempio n. 1
0
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")
Esempio n. 2
0
void FindTest()
{
 o.Find(24);
 o.Find(13);
 o.Find(23);
 cout<<"TEST WYSZUKIWANIA ZOSTAL PRZEPROWADZONY\n\n\n";
}
Esempio n. 3
0
void RemoveTest()
{
 o.Remove(1);
 o.Remove(2);
 o.Remove(3); 
 cout<<"TEST USUWANIA ZOSTAL PRZEPROWADZONY\n\n\n";
}
Esempio n. 4
0
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;
		}
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;
}
Esempio n. 6
0
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;
}
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();
        }
    }
}
Esempio n. 8
0
/* --- 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--;
    }
}
Esempio n. 9
0
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;
}
Esempio n. 10
0
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
Esempio n. 11
0
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;
	}
}
Esempio n. 12
0
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");
}
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;
}
// 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;
}
Esempio n. 15
0
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;
}
Esempio n. 16
0
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;
}
Esempio n. 17
0
void InsertTest()
{
  o.Insert(24);
  o.Insert(13);
  o.Insert(1);
  o.Insert(29);
  o.Insert(2);
  o.Insert(3);
  o.Insert(22);
  o.Insert(26);
  o.Insert(25);
  o.Insert(12);
  cout<<"TEST WSTAWIANIA ZOSTAL PRZEPROWADZONY\n\n\n";
}
Esempio n. 18
0
/* --- 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;
}
Esempio n. 19
0
/* --- Function: static int hide(AvlTree tree, AvlTreeNode node, const void *data) --- */
static int hide(AvlTree tree, AvlTreeNode node, const 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 = hide(tree, node->left, data);
    }
  else if (cmpval > 0) 
    {
      /* Move to the right... */
      retval = hide(tree, node->right, data);
    }
  else /* Node found - hidden or not..! */
    {
      if (!(node->hidden))
        {
          /* Mark the node as hidden... */
          node->hidden = 1;
          /* Return success... */
          retval = 0;
        }
      else
        return -1;
    }

  return retval;  
}
Esempio n. 20
0
Pokemon findBest(AvlTree<Pokemon,PokemonsCompareByLevel>& tree) {
    return tree.findMin();
}
Esempio n. 21
0
int main() {
    AvlTree t;
    t.insert(2);
    t.insert(1);
    t.insert(3);
    t.insert(5);
    t.insert(0);
    t.insert(3);
    printf("min %d max %d\n", t.min_key(), t.max_key());
    assert(5 == t.max_key());
    assert(0 == t.min_key());
    assert(t.contains(0));
    t.remove(0);
    assert(!t.contains(0));
    assert(1 == t.min_key());
    assert(t.contains(1));
    t.remove(1);
    assert(!t.contains(1));
    assert(2 == t.min_key());
    return 0;
}
Esempio n. 22
0
void AvlTree::PrinterAvlTree()
{
  AvlTree avlTreeObject;
  avlTreeObject.Print(AVL->root);	
}
Esempio n. 23
0
int main() {
    AvlTree tree = AvlTree();
    tree.Insert(3);
    tree.Insert(4);
    tree.Insert(5);
    tree.Insert(6);
    tree.Insert(2);
    tree.Print();
    tree.Insert(1);
    tree.Print();
    tree.Insert(7);
    tree.Print();
    tree.Delete(4);
    tree.Print();
    tree.Delete(6);
    tree.Print();
    tree.Delete(7);
    tree.Print();
}
Esempio n. 24
0
 int query(int k) {
    return s.countBigger(k - 1);
 }
Esempio n. 25
0
 void insert(int col, int much) {
    if (m[col] > 0)  s = s.erase(m[col]);
    m[col] += much;
    s = s.insert(m[col]);
 }
int main(){
	AvlTree t;
	t.insert(1);
	t.insert(2);
	t.insert(3);
	t.insert(4);
	t.insert(5);
	preOrder(t.getRoot());
	std::cout << std::endl;
	t.preOrder();
	inOrder(t.getRoot());
	std::cout << std::endl;
	t.inOrder();
	postOrder(t.getRoot());
	std::cout << std::endl;
	t.postOrder();
	std::cout << "Height:" << t.getHeight() << std::endl;
	AvlTree t1(t);
	std::cout << "t1 Height:" << t1.getHeight() << std::endl;
	AvlTree t2;
	t2 = t;
	std::cout << "t2 Height:" << t2.getHeight() << std::endl;
	t.insert(9);
	t.inOrder();
	t1.inOrder();
	t2.inOrder();
	return 0;
}
Esempio n. 27
0
main()
{
      
    AvlTree tree;
    Comparable * found = NULL;
    Comparable value(10);
    vector< Comparable > values;
    int  i;
    int size;
	int count;
	int preSize;
	int inSize;
	long inOrder[100];
	long toPayam2[100];
	long preOrder[100];
    for(i=0;i<10;i++) {
                      Comparable myComp(i);
                      values.push_back( myComp);
                      }
   // for (i = 0 ; i <10;i++){// NUM_ELEMENTS(TestVals) ; i++)  {
      // cout << "+++ inserting key #" << i+1<<endl ;//<< ": " << TestVals[i] << endl;
      for(i=0;i<10;i++){
                        
                         found = tree.Insert(&(values[i]));//&TestVals[i]);
                         
                         if (found) 
                         cout << "\t(already in tree)\n";
       }
       
        long * toPayam= tree.bsf(size);
		tree.postOrder(count,toPayam2);
		tree.preOrder(preSize,preOrder);
		tree.inOrder(inSize,inOrder);
		
		
        cout<<"sldfjsldf"<<endl;
        for(i=0;i<inSize;i++)  cout<<inOrder[i]<<endl;
       //VerifyTree(tree);
   // }/* for */

   // for (i = 0 ; i < NUM_ELEMENTS(TestVals) ; i++)  {
     //  cout << "+++ searching for key #" << i+1 << ": " << TestVals[i] << endl;
       found = tree.Search(1);
       if (! found) {
          cout << "\t(not found in tree)\n";
       }
       else cout<<" foung";
      /* VerifyTree(tree);
    }

    for (i = 0 ; i < NUM_ELEMENTS(DelVals) ; i++)  {
       cout << "+++ deleting key #" << i+1 << ": " << DelVals[i] << endl;
       found = tree.Delete(DelVals[i]);
       if (! found) {
          cout << "\t(not found in tree)\n";
       }
       VerifyTree(tree);
    } 

    cout << endl << "Deallocating tree ..." << endl;
    while (! tree.IsEmpty()) {
       found = tree.Delete(0, MAX_CMP);
       if (! found) {
          cout << "+++ max element not found in tree +++\n";
       } else {
          cout << "+++ deleted max element " << found->Key() << " +++\n";
       }
       delete found;
       VerifyTree(tree);
    }*/
    cout << "DONE!" << endl;
    cin>>i;
    return  0;
}/* main */
Esempio n. 28
0
int main() {
	AvlTree<int> *tree = new AvlTree<int>();
	tree->insert(3);
	tree->printTree();
}
Esempio n. 29
0
int main()
{
	AvlTree<Soundtrack> avlTree;
	vector<Soundtrack> cdVector;  // Holds soundtrack objects created from file input
	string fileName = "Topic F Soundtrack.txt";  // File to open
	bool ableToOpen;  // Test success of opening file

	cout << "Create and populate AVL tree\n\n";
	
	do {
		ableToOpen = openFile(fileName, cdVector, avlTree);
		if (!ableToOpen)
		{
			cout << fileName << " cannot be opened.  Enter another file name -->  ";
			getline(cin, fileName);
		}
	} while (!ableToOpen);

	continueProgram();

	cout << "Get item with key \"FSMBox 03 Disc 8\":\n\n";
	Soundtrack FSM;
	FSM.setLabel("FSM");
	FSM.setCatalogNumber("Box 03 Disc 8");
	if ( !avlTree.get(FSM) )
		cout << "No items found with key \"FSMBox 03 Disc 8\"\n\n";
	cout << "\n\nGet item with key \"FSMBox 07 Disc 8\":\n\n";
	Soundtrack FSM2;
	FSM2.setLabel("FSM");
	FSM2.setCatalogNumber("Box 07 Disc 8");
	if (!avlTree.get(FSM2))
		cout << "No items found with key \"FSMBox 07 Disc 8\"\n";

	continueProgram();

	cout << "Listing of all items in the tree: (There are " << avlTree.getNumberOfNodes() << " items in the tree)\n\n";
	avlTree.inorderTraverse(display);

	continueProgram();

	cout << "\n\nList all soundtracks recorded in the 1950s:\n\n";
	vector<Soundtrack> matchedYear;
	getYear(cdVector, matchedYear);
	for (unsigned int i = 0; i < matchedYear.size(); i++)
	{
		if (avlTree.get(matchedYear[i]))
			cout << matchedYear[i];
	}

	continueProgram();

	cout << "\n\nDelete all items with key \"FSM V8N11\":";
	Soundtrack dltFSM;
	dltFSM.setLabel("FSM");
	dltFSM.setCatalogNumber("V8N11");
	if (avlTree.get(dltFSM))
	{
		avlTree.remove(dltFSM);
		cout << dltFSM << "\nhas been deleted\n\n";
	}
	cout << "Again delete all items with key \"FSM V8N11\":\n";
	if (avlTree.get(dltFSM))
	{
		avlTree.remove(dltFSM);
		cout << dltFSM << "\nhas been deleted\n\n";
	}
	else
		cout << "NO items for \"FSM V8N11\"";

	continueProgram();

	cout << "\nListing of all items in the tree: (There are " << avlTree.getNumberOfNodes() << " items in the tree)\n";
	avlTree.inorderTraverse(display);

	cin.ignore( cin.rdbuf()->in_avail() );
	cout << "\n\nProgram Ending\n\n";
	cout << "Press Enter to end -->  ";
	cin.ignore();
}  // end main
void viewCommonThreeWordPhrases( AvlTree& tree ){
    cout << "Most common three word phrase is: " << tree.getLargestElement() << endl;
    cout << "It appeared: " << tree.getLargest() << " times." << endl;
}