예제 #1
0
파일: main.cpp 프로젝트: joheec/JUMBO15
void test_duplicate_count() {
	BinarySearchTree tree;
	tree.insert(4);
	tree.insert(4);
	tree.insert(4);
	tree.insert(4);
}
예제 #2
0
int main(int argc, char** argv) {
	BinarySearchTree bst;
	bst.insert(9);
	bst.insert(7);
	bst.insert(11);
	bst.insert(4);
	bst.insert(5);
	bst.insert(10);
	bst.insert(15);
	
	// pre order traverse
	std::cout<<"pre order:"<<std::endl;
	bst.preOrder();

	// post order traverse
	std::cout<<"post order:"<<std::endl;
	bst.postOrder();

	std::cout<<"post order non recursive:"<<std::endl;
	bst.postOrderNonRecursively1();

	//system("pause");

	return 0;
}
예제 #3
0
파일: main.cpp 프로젝트: ssvlab/esbmc-gpu
int
main ()
{
  BinarySearchTree t (NULL);
  int NUMS = 400;
  int GAP = 37;
  int i;

  try
  {
    long ssn1 = 10243;
    Customer *c = new Customer (long (ssn1));
    t.insert (c);
    printf ("Inserted %d\n", ssn1);

    long ssn2 = 10244;
    c = new Customer (long (ssn2));
    t.insert (c);
    printf ("Inserted %d\n", ssn2);

    long ssn3 = 10244;
    c = new Customer (long (ssn3));
    t.insert (c);
    printf ("Inserted %d\n", ssn3);

  }
  catch (InsertException x)
  {
    printf ("Could not create Customer:%d\n", (x.a)->ssn);
  }


  t.makeEmpty ();
  return 0;
}
예제 #4
0
int main()
{
   BinarySearchTree<int> BST;
   BST.insert(6);
   BST.insert(5);
   BST.insert(7);
   BST.inorder();
}
예제 #5
0
파일: main.cpp 프로젝트: CCJY/coliru
int main()
{
    BinarySearchTree<int> bst;
    bst.insert(13);
    bst.insert(3);
    bst.insert(5);
    bst.insert(31);
    bst.print();
    return 0;
}
예제 #6
0
int main(int argc, char const *argv[])
{
	BinarySearchTree bst;
	bst.insert(new TreeNode(10));
	bst.insert(new TreeNode(1));
	bst.insert(new TreeNode(-1));
	bst.insert(new TreeNode(1));
	bst.insert(new TreeNode(6));
	bst.inOrderDisplay();
	return 0;
}
예제 #7
0
int main(){
    BinarySearchTree<int> b;
    b.insert(30);
    b.insert(1);
    b.insert(24);
    b.insert(7);

    b.search(24);

    BinarySearchTree<char> c;
    c.insert('k');
    c.search('v');

    return 0;
}
예제 #8
0
 void insert(int value) {
     if (value < data) {
         if (left == NULL) {
             left = new BinarySearchTree(value);
         } else {
             left->insert(value);
         }
     } else if (data < value) {
         if (right == NULL) {
             right = new BinarySearchTree(value);
         } else {
             right->insert(value);
         }
     }
 }
int main()
{
   
   // int ch,tmp,tmp1;
    int N;
	cin>>N;
	for(int y=0;y<N;y++){
		 BinarySearchTree b;
	int kam;cin>>kam;
	for (int i = 0; i < kam; i++)
	{int tmp;
		cin>>tmp;
		b.insert(tmp);
	}
	cout<<"Case "<<y<<":"<<endl;
	cout<<"Pre.:";
                    b.print_preorder();cout<<endl;
					  cout<<"In..:";
                    
                    b.print_inorder();cout<<endl;
					 cout<<"Post:";
                    b.print_postorder();cout<<endl;
					cout<<endl;
					
	}
	
    return 0;}
예제 #10
0
int bstSort(std::vector<int> numVec)
{
  BinarySearchTree* bst = new BinarySearchTree();

  for(std::vector<int>::iterator it = numVec.begin(); it != numVec.end(); ++it)
    bst->insert(new Node(*it));

  int size;
  int index;
  
  numVec.clear();


  bst->inorder(numVec);
  size = numVec.size();
  index = (size - 1)/2;

  bst->empty();
  delete bst;
  if(size % 2 == 0)
    {
      int mean = (numVec.at(index) + numVec.at(index + 1)/2);
      return mean;
    }

  int mean = numVec.at(index);
  return mean;
}
 void insert(const T key) {
   if(key == value) return; // no duplicate keys
   if(key < value) {
     if(left == NULL) {
       left = new BinarySearchTree<T>(key);
     } else {
       left->insert(key);
     }
   } else {
     if(right == NULL) {
       right = new BinarySearchTree<T>(key);
     } else {
       right->insert(key);
     }
   }
 }
예제 #12
0
int main()
{
	int a = 0;
	BinarySearchTree T;
	ifstream in("input.txt");
	while(!in.eof())
	{
		in >> a;
		T.insert(a);
	}
	in.close();
	int l = height(T.root); // высота дерева
	int L = 1;
	for (int i = 0; i < (l - 1); i++)
	{
		L *= 2;
	}
	init_graphics();
	printTree(T.root, 40, 2, L*3);
	set_cursor_pos(1, 1);
	cout << "Input: ";
	cin >> a;
	clearTree(T.root, 40, 2, L * 3);
	T.remove(a);
	printTree(T.root, 40, 2, L*3);
	_getch();
	return 0;
}
int main(int argc, char **argv)
{
  int _data[] = {30, 20, 40, 10, 25, 35, 45};
  vector<int> data(_data, _data + sizeof(_data) / sizeof(int));
  BinarySearchTree<int> tree;
  for (vector<int>::iterator it = data.begin(); it != data.end(); it++) {
    tree.insert(*it);
  }
  TreeItem<int> *p = tree.minimum(tree.root);
  cout << "The minimum data is " << p->_value << endl;
  p = tree.maximum(tree.root);
  cout << "The maximum data is " << p->_value << endl;
  cout << "Successor " << tree.successor(tree.root)->_value << endl;

  tree.travel();
  cout << endl;

  BinarySearchTree<int> tree2;
  for (int i = 0; i < 100; i++) {
    tree2.insert(i);
  }
  tree2.travel();
  cout << endl;
  return 0;
}
예제 #14
0
void Menu::Undo(BinarySearchTree<DataRecord>& g)
{
	if (DataStack.isEmpty())
		//Nothing to undo
		cout << "nothing to undo";
	else
	{
		//add to Hash Table and BST
		DataRecord Temp;
		DataRecord topStack;
		DataStack.peek(topStack);
		DataStack.pop();
		if (g.getEntry(topStack, Temp))
		{
			//data already exists
			cout << "already exists";
			return;
		}
		else
		{
			g.insert(topStack);
			//successful
			cout << "undo was successful";
			return;
		}
	}
}
예제 #15
0
파일: 4.6.cpp 프로젝트: snowyoneill/CTCI
int main()
{
	BinarySearchTree<int> bSTree;

	bSTree.insert(6);
	bSTree.insert(5);
	bSTree.insert(2);
	bSTree.insert(1);
	bSTree.insert(4);
	bSTree.insert(3);

	bSTree.print_in_order();
	print_in_order_with_levels(bSTree.root, 0);

	Node<int> *res = nextNodeInOrder<int>(bSTree.root, 3);
	if(res)
		printf("Found: %d\n", res->val);
	else
		printf("Found: null\n");

	//---------------------------------------------------

	std::cout << std::endl;

	NodeWithParent<int> nP(1);

	nP.left = new NodeWithParent<int>(2);
	nP.right = new NodeWithParent<int>(3);
	nP.left->parent = &nP;
	nP.right->parent = &nP;

	nP.left->left = new NodeWithParent<int>(4);
	nP.left->right = new NodeWithParent<int>(5);
	nP.left->left->parent = nP.left;
	nP.left->right->parent = nP.right;

	print_in_order_with_levels(&nP, 0);

	NodeWithParent<int> *res2 = inorderSucc<int>(nP.left);
	if(res2)
		printf("Found: %d\n", res2->val);
	else
		printf("Found: null\n");

	return 0;
}
예제 #16
0
파일: Main.cpp 프로젝트: Amorming/C--
int _tmain(int argc, _TCHAR* argv[])
{
  cout << "Hello Binary Search Tree" << endl;
	/* BinarySearchTree bt (); is NO good */
	{
		BinarySearchTree<int> bst;
		bst.insert(3); 
		bst.insert(20);
		bst.insert(15);
		bst.insert(77);
		bst.insert(9);
		cout << "Debugging..." << endl;
	}
	//_CrtSetBreakAlloc(222);
	
	cout << "Debugging..." << endl;
	_CrtDumpMemoryLeaks();
	return 0;
}
예제 #17
0
//--------------------------------------------------------------------------------------------------
void firstBST_Test01()
{
	BinarySearchTree tree;
	tree.insert(6);
	tree.insert(2);
	tree.insert(8);
	tree.insert(4);
	tree.insert(5);
	tree.insert(1);
	tree.insert(3);
	cout << "Number of levels in this tree is -> \t" << tree.levels() << endl;
	cout << "maximum Possible Nodes At Height -> \t" << tree.levels() << "\t is ->";
	cout << tree.maximumPossibleNodesAtHeight(tree.levels()) << endl;
	tree.preorderTreeTraversalWithRecursion();

	// cout<< "Deleted node -> " << tree.deleteNode(50);
	cout << endl << endl;
	tree.print();
}
예제 #18
0
int main(){
	BinarySearchTree b;
	b.insert("huhu");
	b.exists("huhu", NULL);
	
//	printf("%i\n", "a"[0]);
//	printf("%i\n", "z"[0]);
//	printf("%i\n", "A"[0]);
//	printf("%i\n", "Z"[0]);
}
예제 #19
0
int main() {
	int values[14] = {4,2,11,15,9,1,-6,5,3,15,2,5,13,14};
	BinarySearchTree bst;
	for (int i=0;i<14;i++) {
		cout << "Inserting " << values[i] << " into the tree.\n";
		bst.insert(values[i]);
	}
	cout << "Original tree " <<
			"(asterisks denote a count of more than 1):\n";
	print_tree_details(bst);

	// make a copy with copy constructor
	BinarySearchTree bst_copy_constructor = bst;
	//bst_copy_constructor.print_tree();

	// make a copy with assignment overload
	BinarySearchTree bst_copy_1;
	bst_copy_1 = bst;

	cout << "Removing 9 from original tree:\n";
	bst.remove(9); // remove a node with no children
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 1 from original tree:\n";
	bst.remove(1); // remove a node with one child
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 11 from original tree:\n";
	bst.remove(11); // remove a node with one child
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 5 from original tree " <<
			"(should still have one 5):\n";
	bst.remove(5); // remove a node with one child
	print_tree_details(bst);

	// check if the tree contains values
	bst = bst_copy_1;
	for (int i=-10;i<20;i++) {
		cout << "Tree " << (bst.contains(i) ?
				"contains " :
				"does not contain ")
				<< "the value " << i << "\n";
	}

	cout << "\nFinished!\n";
	return 0;
}
예제 #20
0
int main() {
  std::vector<int> inpSequence = { 10, 2, 3, 65, 3, 34, 5, 76, 3, 3, 2, 4, 6, 78, 8, 34, 3, 67 };
  BinarySearchTree bst;
  for (int i : inpSequence) {
    bst.insert(i);
    bst.printPreOrder();
  }
  for (int i : inpSequence) {
    cout << bst.contains(i) << "\n";
  }
  cout << bst.contains(1000) << "\n";
  return 0;
}
예제 #21
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;
}
예제 #22
0
int main(int argc, const char * argv[]) {
    // insert code here...
    BinaryTree bt(5);
    
    bt.insert(bt.root, 7);
    bt.insert(bt.root, 9);
    bt.insert(bt.root, 11); //Should display error
    
    bt.preOrder(bt.root);
    determineBigO();
    
    bt.inOrder(bt.root);
    determineBigO();
    
    bt.postOrder(bt.root);
    determineBigO();
    
    BinarySearchTree bst;
    bst.insert(5);
    bst.insert(2);
    bst.insert(8);
}
예제 #23
0
int main() {
  BinarySearchTree<char> bb;
  bb.insert('o');
  bb.insert('d');
  bb.insert('c');
  bb.insert('f');
  bb.insert('s');
  bb.insert('p');
  bb.insert('r');
  bb.insert('z');
  
  bb.traversePreorder();
  bb.traverseInorder();
  bb.traversePostorder();
  std::cout << "lowest common ancestor of 'z' and 'p' is " 
            << bb.lowestcommonAncestor('z', 'p')->getElement() 
            << "\n";
  BinarySearchTree<char> cc = bb.createMirror();
  std::cout << "Mirror created ...\n";
  std::cout << "Mirror :------------------------------->\n";
  cc.traversePreorder();
  return 0;
}
int main()
{
    BinarySearchTree bst;
    int n;
    cin >> n;
    while(n){
        bst.insert(n);
        cin >> n;
    }
    bst.print();
    cin >> n;
    cout << bst.find(n);
    return 0;
}
예제 #25
0
파일: 4-1.cpp 프로젝트: marsanatta/infinite
int main() {
  int N;
  cin >> N;
  BinarySearchTree t;
  srand(time(NULL));
  for (int i = 0; i < N; i++) {
    int randNum = 1 + rand() % 100;
    t.insert(randNum);
  }
  t.dfsRecursive(PREORDER, t.getRoot());
  cout << "is balanced: " << isBalanced(t) << endl;

  return 0;
}
예제 #26
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());
}
예제 #27
0
파일: Update_BST.cpp 프로젝트: lc2879/Ccode
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;
}
예제 #28
0
파일: main.cpp 프로젝트: joheec/JUMBO15
void test_remove() {
	BinarySearchTree * tree = new BinarySearchTree;
	//remove empty tree
	cout << tree->remove(2);
	test_insert(tree);
	tree->print_tree();
	tree->remove(10);
	tree->print_tree();


	//remove 2 children
	tree->remove(8);
	tree->print_tree();
	//remove 1 child
	tree->remove(2);
	tree->print_tree();
	//remove no children
	tree->remove(3);
	tree->print_tree();
	//remove no parent
	tree->remove(5);
	tree->print_tree();
	tree->remove(5);
	tree->print_tree();
	//remove doesn't exist
	tree->remove(8);
	tree->print_tree();
	//remove only right child
	tree->insert(10);
	tree->insert(11);
	tree->insert(12);
	tree->print_tree();
	tree->remove(10);
	tree->print_tree();

}
예제 #29
0
int main()
{
    BinarySearchTree b;
    int ch,tmp,tmp1;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Pre-Order Traversal "<<endl;
       cout<<" 4. Post-Order Traversal "<<endl;
       cout<<" 5. Removal "<<endl;
       cout<<" 6. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>tmp;
                    b.insert(tmp);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_inorder();
                    break;
           case 3 : cout<<endl;
                    cout<<" Pre-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_preorder();
                    break;
           case 4 : cout<<endl;
                    cout<<" Post-Order Traversal "<<endl;
                    cout<<" --------------------"<<endl;
                    b.print_postorder();
                    break;
           case 5 : cout<<" Enter data to be deleted : ";
                    cin>>tmp1;
                    b.remove(tmp1);
                    break;
           case 6 : system("pause");
                    return 0;
                    break;
       }
    }
}
예제 #30
0
    // Test program
int main( )
{
    BinarySearchTree<int> t;
    int NUMS = 400000;
    const int GAP  =   3711;
    int i;

    cout << "Checking... (no more output means success)" << endl;

    for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )
        t.insert( i );

    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;
    }

    BinarySearchTree<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 << "Finished testing" << endl;

    return 0;
}