Пример #1
0
int main()
{  
   BinarySearchTree t;
   t.insert("D");
   t.insert("B");
   t.insert("A");
   t.insert("C");
   t.insert("F");
   t.insert("E");
   t.insert("I");
   t.insert("G");
   t.insert("H");
   t.insert("J");
   t.erase("A"); // Removing leaf
   t.erase("B"); // Removing element with one child
   t.erase("F"); // Removing element with two children
   t.erase("D"); // Removing root
   t.print();
   cout << t.count("E") << "\n";
   cout << t.count("F") << "\n";
   return 0;
}
Пример #2
0
int main(int argc, char *argv[]) {
  BinarySearchTree<int> BST;
  assert(BST.insert(4) == true);
  assert(BST.insert(5) == true);
  assert(BST.insert(2) == true);
  assert(BST.insert(3) == true);
  assert(BST.insert(1) == true);
  assert(BST.erase(0) == false);
  assert(BST.erase(2) == true);
  assert(BST.erase(2) == false);
  assert(BST.insert(4) == false);
  // should output 4
  assert(BST.getRootVal() == 4);
  cout << BST.getRootVal() << endl;
  assert(BST.erase(4) == true);
  // should output 5
  assert(BST.getRootVal() == 5);
  cout << BST.getRootVal() << endl;
  assert(BST.erase(5) == true);
  // should output 3
  assert(BST.getRootVal() == 3);
  cout << BST.getRootVal() << endl;
  return 0;
}
Пример #3
0
void basic_test()
{
    BinarySearchTree<int> tree;
    for (int i = 0; i < 15; i += 3) tree.insert(i);

    assert(tree.size() == 5);
    assert(tree.search(3));
    assert(tree.search(12));
    assert(!tree.search(10));
    assert(!tree.erase(8));
    assert(tree.search(9));
    assert(tree.erase(9));
    assert(!tree.erase(9));
    assert(tree.size() == 4);
    assert(!tree.insert(3));
    assert(tree.insert(13));
    assert(tree.size() == 5);
    assert(tree.erase(13));
    assert(tree.erase(12));
    assert(tree.erase(6));
    assert(tree.erase(3));
    assert(tree.erase(0));
    assert(tree.empty());
}
Пример #4
0
int main(int argc, const char * argv[]) {
    // insert code here...
    
    
    cout << (z) << "\n";
    
    set<int> a;
    a.insert(1);
    a.insert(2);
    a.insert(3);
    a.insert(5);
    
    set<int> b;
    b.insert(3);
    b.insert(4);
    b.insert(5);
    
    set<int> c;
    
    
//    c = set_Union(a, b);
//    c = intersection(a, b);
    
    //set<int> d = sieveofEratosthenes(10);
    
    //exercise1();
  //  exercise2();
    
    BinarySearchTree t;
    
    t.insert("D");
    t.insert("B");
    t.insert("A");
    t.insert("C");
    t.insert("F");
    t.insert("E");
    t.insert("I");
    t.insert("G");
    t.insert("H");
    t.insert("J");
    t.erase("A"); // Removing leaf
    t.erase("B"); // Removing element with one child
    t.erase("F"); // Removing element with two children
    t.erase("D"); // Removing root
    t.print();
    cout << t.count("E") << "\n";
    cout << t.count("F") << "\n";
    
    cout << "\nHeap Tasks\n";
    Heap tasks;
    tasks.push(2);
    tasks.push(3);
    tasks.push(2);
    tasks.push(1);
    tasks.push(4);
    tasks.push(9);
    tasks.push(1);
    tasks.push(5);
    while (tasks.size() > 0)
    {
        int task = tasks.top();
        tasks.pop();
        cout << task << "\n";
    }
    
    std::cout << "Hello, World!\n";
    return 0;
}
Пример #5
0
int main()
{
	//need this to get a memory leak dump at end of code execution
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	// Create binary search tree, give it values {2,3,5,11,13,17}
	BinarySearchTree bst;
	bst.insert(3); bst.insert(2); bst.insert(11);
	bst.insert(13); bst.insert(5); bst.insert(17);


	// Prints to the console: 2,3,5,11,13,17,
	for (auto x : bst) std::cout << x << ",";
	std::cout << std::endl;

	bst.erase(6);  // {2,3,5,11,13,17}, no effect
	bst.erase(11);  // {2,3,5,13,17}

	auto start = std::begin(bst);
	auto stop = std::end(bst);

	int value = 17;

	bool keep_looking = true;

	// find a 17 in the list
	while (start != stop && keep_looking){
		if (*start++ == value){
			keep_looking = false;
		}
	}
	--start;

	//// Prints "17 is in the list."
	if (start != stop)
		std::cout << *start << " is in the list." << std::endl;

	//Construct a deep copy of bst.

	//std::cout << "0.\n";
	//bst.print();
	//std::cout << "1.\n";
	BinarySearchTree deep_copy(bst);
	//std::cout << "2.\n";



	deep_copy.insert(11); deep_copy.insert(7);


	// Prints to the console: 2,3,5,7,11, 13, 17,
	for (auto x : deep_copy) std::cout << x << ",";
	std::cout << std::endl;


	//std::cout << "\n\n\n\n\n";


	//bst.print();
	//deep_copy.print();
	
	return 0;
}
Пример #6
0
int main()
{  
   int input;
   BinarySearchTree bin;
   string binarycode;
   int size = 0;


   cout << "Please input a set of distinct nonnegative numbers for a Tree (Enter -1 when you are finished):" << endl;
   while (cin >> input)
		if (input == -1)
			break;
		else
		bin.insert(input);

   int largest = bin.largest();
   cout << "\nThe maximum of the entries is: " << largest;
   int smallest = bin.smallest();
   cout << "\nThe minimum of the entries is: " << smallest;
   
   size = bin.size();
   cout << "\nThe size of the List is: " << size;
   
   cout << endl;

   input = 1;
   while (input != -1)
   {
	   cout << "Select a value for insertion (Enter -1 when finished): ";
	   cin >> input;
	   if (input != -1)
		   bin.insert(input);
	   else
		   break;
   }

   largest = bin.largest();
   cout << "\nThe maximum of the entries is: " << largest;
   smallest = bin.smallest();
   cout << "\nThe minimum of the entries is: " << smallest;
   
   size = bin.size();
   cout << "\nThe size of the List is: " << size << endl;
   

   binarycode = bin.BinaryCode(1);
   cout << "The binary code for 1 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(2);
   cout << "The binary code for 2 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(3);
   cout << "The binary code for 3 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(4);
   cout << "The binary code for 4 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(5);
   cout << "The binary code for 5 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(6);
   cout << "The binary code for 6 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(7);
   cout << "The binary code for 7 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(8);
   cout << "The binary code for 8 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(9);
   cout << "The binary code for 9 is: " << binarycode << endl;

   cout << endl;

   input = 1;
   while (input != -1)
   {
	   cout << "Select a value to erase (enter -1 when finished): ";
	   cin >> input;
	   if (input != -1)
		   bin.erase(input);
	   else
		   break;
   }

   cout << endl;
   binarycode = bin.BinaryCode(1);
   cout << "The binary code for 1 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(2);
   cout << "The binary code for 2 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(3);
   cout << "The binary code for 3 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(4);
   cout << "The binary code for 4 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(5);
   cout << "The binary code for 5 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(6);
   cout << "The binary code for 6 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(7);
   cout << "The binary code for 7 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(8);
   cout << "The binary code for 8 is: " << binarycode << endl;
   binarycode = bin.BinaryCode(9);
   cout << "The binary code for 9 is: " << binarycode << endl;
   
   system("pause");
   return 0;
}