Ejemplo n.º 1
0
int main()
{
  int N=0;
  cin>>N;
  char ins[20];
  int a,b;
  BSTree tree;
  for(int i=0;i<N;i++)
  {
    cin>>ins;
    if(ins[0]=='i')
    {
      cin>>a;
      tree.insertData(a);
      //tree.printByInOrderTraversal();
    }
    if(ins[0]=='d')
    {
      if(ins[6]=='\0')
      {
        cin>>a;
        tree.deletion(a);
        //tree.printByInOrderTraversal();
      }
      else
      {
        if(ins[7]=='l')
int main(){
  const int input[] = {8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28};
  // output should be array of {18 63 80 25 32 43 80 93 80 25 93 -1 28 -1 -1}
  const int arraysize = sizeof(input) / sizeof(input[0]);
  print_array(input, arraysize);
 
  // test naive method
  int output[arraysize];
  replace_with_least_greater_number(input, arraysize, output);
  print_array(output, arraysize);

  // test BST method
  int output2[arraysize];
  BSTree tree;
  for(int i= arraysize-1; i>=0; i--){
    Node* least_greater_predecessor = tree.insert(input[i]);
    if (least_greater_predecessor == NULL){
      output2[i] = -1;
    }else{
      output2[i] = least_greater_predecessor->value;
    }
  }
  
  //tree.print_tree();
  print_array(output2, arraysize);
  return 0;
}
int main()
{
	BSTree<int> bstree;
	TreeNode<int>* ptn;

	bstree.insert(3);
	bstree.insert(3);
	bstree.insert(1);
	bstree.insert(7);
	bstree.insert(5);
	bstree.insert(8);
	
	bstree.print(std::cout);
	
	ptn = bstree.find_min(0);
	std::cout << "\nThe minimum element is " << ptn->element << std::endl;
	
	ptn = bstree.find_max(0);
	std::cout << "\nThe maximum element is "  << ptn->element << std::endl;
	
	bstree.delete_element(3);
	bstree.print(std::cout);

	return 0;
}
Ejemplo n.º 4
0
int main()
{
	int num;
	BSTree T;
	cin >> num;
	while( num != -1 )
	{
		T.insert(num);
		cin >> num;
	}
	T.median();
}
Ejemplo n.º 5
0
void Test2()
{
	int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
	BSTree<int, int> bst;
	for (int i = 0; i < (sizeof(a) / sizeof(a[0])); ++i)
	{
		bst.InsertR(a[i], i);
	}
	bst.Inorder();
	cout << bst.FindR(0)->_key << endl;
	cout << bst.RemoveR(3) << endl;
}
Ejemplo n.º 6
0
void Test1()
{
	int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
	BSTree<int, int> bst;
	for (int i = 0; i < (sizeof(a) / sizeof(a[0])); ++i)
	{
		bst.Insert(a[i], i);
	}
	bst.Inorder();
	//cout << bst.Find(10)->_key << endl;
	cout << "------------------------------------------" << endl;
	cout<<bst.Remove(8)<<endl;
	bst.Inorder();
}
Ejemplo n.º 7
0
int main()
{
	BSTree T;
	int n;
	cin >> n;
	while(n!=-1)
	{
		T.insert(n);
		cin >> n;
	}
	cin >> T.max;
	T.pathsum(T.root);
	cout << T.count << endl;
}
Ejemplo n.º 8
0
//template <typename T>
void SetTreePositions(const BSTree &tree, 
                      std::map<const BSTree::BinTreeNode *, 
                      std::pair<int, int> >& NodePositions)
{
  Position = 0;
  NodePositions.clear();
  SetTreePositions(tree.root(), 0, NodePositions);
}
int main() {
  BSTree tree;
  tree.Insert(8);
  tree.Insert(6);
  tree.Insert(10);
  tree.Insert(5);
  tree.Insert(7);
  tree.Insert(9);
  tree.Insert(11);

  tree.BreadthFirstTraversal();
  //tree.Rotate();
  //tree.BreadthFirstTraversal();
  tree.RotateIterative2();
  tree.BreadthFirstTraversal();
}
Ejemplo n.º 10
0
int main()
{
	BSTreeNode * pHead = NULL;
	BSTreeNode * pListIndex = NULL;
	BSTree* pRoot = new BSTree();
	pRoot->addBSTreeNode(10);
	pRoot->addBSTreeNode(4);
	pRoot->addBSTreeNode(12);
	pRoot->addBSTreeNode(14);
	pRoot->addBSTreeNode(15);
	pRoot->addBSTreeNode(6);
	pRoot->addBSTreeNode(8);
	pRoot->addBSTreeNode(16);
	pRoot->showTree(); // 树的形状
	pRoot->ergodicBSTree();
	return 0;
}
Ejemplo n.º 11
0
void initValues(vector <int>& v, BSTree& root, HashTable& ht) {
    int i, randomValue;
    bool hashTableFull;

    for (i = 0; i < size; i++) {
        randomValue = 1 + rand() % 99;
        v.push_back(randomValue);
        root.insert(randomValue);
        hashTableFull = ht.insert(randomValue);
        if (!hashTableFull) cout << "Hash Table is now full." << endl;
    }
}
Ejemplo n.º 12
0
void WithdrawlOrder::Execute(BSTree &accounts)
{
	Account* temp;
	if (accounts.Retrieve(accountID, temp))
	{
		temp->Withdraw(amount, fundNumber);
	}
	else
	{
		cerr << "ERROR: Account " << accountID << " not found. Withdrawl refused." << endl;
	}
}
Ejemplo n.º 13
0
int main() {
    BSTree<int> tree;
    tree.add(15, 15);
    tree.add(8, 8);
    tree.add(6, 6);
    tree.add(7, 7);
    tree.add(12, 12);
    tree.add(11, 11);
    tree.add(9, 9);
    tree.add(20, 20);
    tree.add(18, 18);
    tree.add(25, 25);
    tree.add(23, 23);

    tree.traverse(TraverseOrder::MID_ORDER);
    tree.traverse(TraverseOrder::PRE_ORDER);

    return 0;
}
Ejemplo n.º 14
0
void Test1()
{
	BSTree<int, int> b;
	int a[] = { 1, 3, 6, 7, 2, 8, 0 };
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
	{
		b.InsertR(a[i], i);
	}
	b.InOder();
	cout<<b.IsBlanceTree()<<endl;
	b.RemoveR(7);
	b.RemoveR(6);
	b.RemoveR(8);
	b.RemoveR(0);
	b.RemoveR(3);
	b.RemoveR(2);
	b.RemoveR(1);
	b.InOder();
	return;
}
Ejemplo n.º 15
0
int main(int args,char* argv[]){


	BSTree * myTree = new BSTree;
	myTree->Insert(80);
	myTree->Insert(43);
	myTree->Insert(21);
	myTree->Insert(42);
	myTree->Insert(20);
	myTree->Insert(50);
	myTree->Insert(6);
	myTree->Insert(74);
	myTree->Insert(12);
	myTree->Insert(78);
	myTree->Insert(52);

	myTree->InOrder();
	cout << endl;
	myTree->PostOrder();
	cout << endl;
	myTree->PreOrder();
	cin.get();
	return 0;
}
int main( int argc, char* argv[] )
{
	using namespace ds;
	std::size_t test_case = 2;

	switch (test_case)
	{
	case 0:
		{
			BSTree<Version::V0,int> bstree0;
			TreeNode<Version::V0,int> treeNode0(30);
			bstree0.insert(&treeNode0);
			TreeNode<Version::V0,int> treeNode1(25);
			bstree0.insert(&treeNode1);
			TreeNode<Version::V0,int> treeNode2(28);
			bstree0.insert(&treeNode2);
			TreeNode<Version::V0,int> treeNode3(10);
			bstree0.insert(&treeNode3);
			TreeNode<Version::V0,int> treeNode4(37);
			bstree0.insert(&treeNode4);
			TreeNode<Version::V0,int> treeNode5(43);
			bstree0.insert(&treeNode5);
			TreeNode<Version::V0,int> treeNode6(32);
			bstree0.insert(&treeNode6);

			print_level_order(bstree0.root());
		}
		break;
	case 1:
		{
			//balanced BST
			int arr[] = {30,25,28,10,37,43,32};
			BSTree<Version::V1,int> bstree;
			for (std::size_t i=0; i<7; ++i)
			{
				bstree.insert(arr[i]);
			}
			print_level_order(bstree.root());
		}
		break;
	case 2:
		{
			//unbalanced BST
			int arr[] = {30,25,28,10,37,43,32,31,34};
			BSTree<Version::V1,int> bstree;
			for (std::size_t i=0; i<9; ++i)
			{
				bstree.insert(arr[i]);
			}
			print_level_order(bstree.root());
		}
		break;
	}

	return 0;
}
Ejemplo n.º 17
0
int main()
{
	/*
	BSTree<int,float> tree;
	BinTreeNode<int,float> s[]={ {2,9.9},{1,103.23},{3,23.54},{0,0.0},{0,0.0},{0,0.0},{0,0.0} };
	tree.create1(s);
	tree.showInOrder();
	cout<<tree.search(3)<<endl;

	tree.remove(3);
	tree.showLevelOrder();

	tree.remove(2);
	tree.showLevelOrder();

	tree.remove(1);
	tree.showLevelOrder();
*/
	BSTree<int,float> tree;
	tree.create2();
	tree.showInOrder();
	
	return 0;
}
Ejemplo n.º 18
0
// 测试主函数  
int main()
{
	// 建立搜索树  
	BSTree tree;
	tree.add(10);
	tree.add(8);
	tree.add(123);
	tree.add(11);
	tree.add(900);


	tree.BFS();
	system("pause");
	
}
Ejemplo n.º 19
0
void main()
{
	BSTree oBSTree;

	oBSTree.Insert(100);
	oBSTree.Insert(50);
	oBSTree.Insert(150);
	oBSTree.Insert(25);
	oBSTree.Insert(75);
	oBSTree.Insert(125);
	oBSTree.Insert(175);
	oBSTree.Insert(110);

	FindSum(oBSTree.pRoot, 75);
}
int main()
{
	BSTree<int> t;
	
	t.Insert(5);
	t.Insert(3);
	t.Insert(7);
	t.Insert(1);
	t.Insert(4);
	t.Insert(6);
	t.Insert(8);
	t.Insert(0);
	t.Insert(2);
	t.Insert(9);
	
//	t.InOrder();
//
//	t.Remove(0);
//	t.Remove(1);
//	t.Remove(9);
//	t.Remove(7);
//
//	t.InOrder();
//
//	for (int i = 0; i < 10; i++)
//		t.Remove(i);
//	t.InOrder();

//	t.Find(9);
//	for (int i = 0; i <= 10; i++)
//	{
//		BSTNode<int> *ret = t.Find(i);
//		if (NULL != ret)
//			std::cout << ret->_key << std::endl;
//		else
//			std::cout << i << " not found !" << std::endl;
//	}

//	std::cout << t.Height() << std::endl;
//	std::cout << t.LeafNums() << std::endl;

//	t.LevelOrder();
//	t.PrevOrderNonR();
//	t.InOrderNonR();
	t.PostOrderNonR();

	return 0;
}
Ejemplo n.º 21
0
void insertarBST(BSTree<int> &ABB,string nomArch)
{
	ifstream arch;
	arch.open(nomArch.c_str());
	if(!arch)
	{
		cout<<"Error: NO Abrio el archivo "<<nomArch<<endl;
	}
	else
	{
		int value;
		while(arch>>value)
		{
			ABB.insert(value);
		}
		arch.close();
	}
}
Ejemplo n.º 22
0
int main()
{	
	BSTree tree;   // The binary search tree to be tested
	char command;  // The command entered by the user (I, N, F, D, L, or P)
	int item;      // The item to have the action performed on
	
	cin >> command;
	
	while (cin)
	{
		switch (command)
		{
			case 'I':
				tree.initialize();
				break;
			case 'N':
				cin >> item;
				tree.insert(item);
				break;
			case 'F':
				cin >> item;
				cout << item << " ";
				if (tree.find(item))
					cout << "Found" << endl;
				else
					cout << "Not Found" << endl;
				break;
			case 'D':
				cin >> item;
				tree.del(item);
				break;
			case 'L':
				tree.inorder(cout);
				break;
			case 'P':
				tree.preorder(cout);
				break;
			default:
				cout << "Illegal Command" << endl;
				cin.clear();
				cin.ignore(255, '\n');
				break;
		}
		cin >> command;
	}
}
void main()
{
	BSTree oBSTree;

	oBSTree.Insert(100);
	oBSTree.Insert(50);
	oBSTree.Insert(150);
	oBSTree.Insert(25);
	oBSTree.Insert(75);
	oBSTree.Insert(125);
	oBSTree.Insert(175);
	oBSTree.Insert(110);

	Node* pTmp = CommonAncestor(oBSTree.pRoot, 110, 175);

	cout << pTmp->data << endl;

	pTmp = CommonAncestor_Book(oBSTree.pRoot, 110, 175);

	cout << pTmp->data << endl;
}
Ejemplo n.º 24
0
//template <typename T>
void PrintBST(const BSTree &tree)
{
  std::map<const BSTree::BinTreeNode *, std::pair<int, int> > NodePositions;

  SetTreePositions(tree, NodePositions);
  int height = tree.height();
  int offset = 0;
  int fudge = 5;
  for (int i = 0; i <= height; i++)
  {
    std::vector<std::pair<const BSTree::BinTreeNode *, int> >nodes = GetNodesAtLevel(i, NodePositions);
    // Borland doesn't like the function, wants a function object
    //std::sort(nodes.begin(), nodes.end(), SortNodes<T>);
    std::sort(nodes.begin(), nodes.end(), fSortNodes());
    typename std::vector<std::pair<const BSTree::BinTreeNode *, int> >::iterator iter;
    char buffer[1024 * 2] = {0};
    std::memset(buffer, ' ', 1024 * 2);

    for (iter = nodes.begin(); iter != nodes.end(); ++iter)
    {
      //char data[60] = {0};
      int value = (*iter).first->data;
      //std::sprintf(data, "%g", value);  // <<**>>

      std::stringstream ss;
      if (SHOW_COUNTS)
        ss << value << "[" << (*iter).first->count << "]";
      else
        ss << value;

      offset = (height / 2) + iter->second * fudge;  // fudge factor (change to 4 for big numbers)
      //strncpy(buffer + offset, data, strlen(data));
      strncpy(buffer + offset, ss.str().c_str(), ss.str().length());
    }
    buffer[offset + fudge * 2] = 0; // should handle 4 digits easily
    std::cout << buffer << std::endl << std::endl;
  }
}
Ejemplo n.º 25
0
int main()
{
	BSTree b;
	printf("\nAdding elements...");
	b.add(1);
	b.add(2);
	b.add(3);
	b.add(4);
	b.add(5);
	b.add(6);
	b.add(8);
	b.add(7);
	b.add(9);
	printf("[ok]\n");
	printf("Chenking count_leaf_nodes...");	
	if(b.count_leaf_nodes() != 2)
		goto fail;
	printf("[ok]\n");
	return 0;
fail:
	printf("[failed]\n");
	return -1;
}
Ejemplo n.º 26
0
int main(){
    BSTree<double> bstree;
    bstree.insert(34);
    bstree.insert(56);
    bstree.insert(3);
    bstree.insert(89);
    bstree.insert(12);
    bstree.insert(22);
    bstree.insert(78);

    int target = -1;
    cout << "Data to look for:" ;
    cin >> target;

    Node<double>* result = bstree.binary_search(target);
    if(result){
        cout << "Found result(" << result->get() << ")" << endl;
    }else{
        cout << "Nothing is found" << endl;
    }
}
void BinarySearchTreeTest(int nmbItems)
{
	int i=0;
	int value;

	BSTree *BinarySearchTree = new BSTree();
	
	while (i < nmbItems)
	{
		value = rand()%10;		// Random integer value
		cout << value;
		if (i < (nmbItems-1))
			cout << ", ";
		BinarySearchTree->insert(value);
		i++;
	}

	cout << endl;
	cout << "Entered " << nmbItems << " random values into BSTree Data Structure ..." << endl;

	cout << endl << "Preorder Traversal: ";
	BinarySearchTree->BSTreeTraverse(PREORDER);

	cout << endl << "Inorder Traversal: ";
	BinarySearchTree->BSTreeTraverse(INORDER);

	cout << endl << "Post order Traversal: ";
	BinarySearchTree->BSTreeTraverse(POSTORDER);

	cout << endl;
	cout << "Search for a value in the tree: ";
	cin >> value;

	if (BinarySearchTree->find(value))
		cout << "Found " << value;
	else
		cout << "Could not find " << value;
	cout << endl;
	BinarySearchTree->~BSTree();
}
Ejemplo n.º 28
0
int main(int argc, char *argv[]) {
  BSTree tree;
  tree.convertToLinkedList();
}
Ejemplo n.º 29
0
int main() {
    BSTree bt;
    //12 54 34 76 46 2 18 38 23 11 9 87 33 65
    bt.root = bt.insert(12,bt.root);        //insert node
    bt.insert(54,bt.root);                  //insert other values
    bt.insert(34,bt.root);
    bt.insert(76,bt.root);
    bt.insert(46,bt.root);
    bt.insert(2,bt.root);
    bt.insert(18,bt.root);
    bt.insert(38,bt.root);
    bt.insert(23,bt.root);
    bt.insert(11,bt.root);
    bt.insert(9,bt.root);
    bt.insert(87,bt.root);
    bt.insert(33,bt.root);
    bt.insert(65,bt.root);
    
    cout<<"Tree values before deleting: "<<endl;
    bt.BFS(bt.root);                    //BFS to print values
    cout<<endl<<"Tree values after deleting: "<<endl;
    bt.deleteNode(bt.root, 54);         //function call to delete given node
    bt.BFS(bt.root);                    //BFS to print new tree values
    
    return 0;
}
Ejemplo n.º 30
0
Archivo: lab7.cpp Proyecto: skoved/lab7
int main(){
	srand(time(NULL));
  	cerr << "\n\tTEST #0: Passes all tests from Lab 6" << endl;
	BSTree tree;
	assert(tree.empty());
	int array[] = {5,3,10,4,8,2,1,7,9,6,12,11,13};
	for(unsigned int i = 0; i < sizeof(array)/sizeof(int); i++){
		tree.insert(array[i]);
    	assert(!tree.empty());
	}
	//retest inserting duplicate values
	for(int i = 0; i < 8; i++){
		assert(!tree.insert(array[i]));
	}
	for(int i = 1; i <= 14; i++){
		if(i != 14){
			assert(tree.find(i));
		}else
			assert(!tree.find(i));
	}
	BSTree tree3;
	assert(!tree3.find(1));
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #1: Cannot remove node that is not in the tree" << endl;
	assert(!tree.remove(25));
	vector<int> test1;
	tree.sortedArray(test1);
	printList(test1);
	assert(test1.size() == 13);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #2: Remove a leaf node" << endl;
	assert(tree.remove(1));
	vector<int> test2;
	tree.sortedArray(test2);
	printList(test2);
	assert(test2.size() == 12);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #3: Remove single branch using the Short Circuit algorithm" << endl;
	assert(tree.remove(7));
	vector<int> test3;
	tree.sortedArray(test3);
	printList(test3);
	assert(test3.size() == 11);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "Test #4: Remove two branch node promoting a leaf node" << endl;
	assert(tree.remove(10));
	vector<int> test4;
	tree.sortedArray(test4);
	printList(test4);
	assert(test4.size() == 10);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #5: Remove two branch node promoting a 1 branch node using Short Circuit" << endl;
	assert(tree.remove(11));
	vector<int> test5;
	tree.sortedArray(test5);
	printList(test5);
	assert(test5.size() == 9);
	cerr << "\n\t========================PASS========================\n" << endl;

	

	cerr << "\n\tTEST #6: Remove root with two branches" << endl;
	assert(tree.remove(5));
	vector<int> test6;
	tree.sortedArray(test6);
	printList(test6);
	assert(test6.size() == 8);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #7: Remove root with one branch" << endl;
	assert(tree.remove(3));
	assert(tree.remove(4));
	assert(tree.remove(2));
	assert(tree.remove(6));
	vector<int> test7;
	tree.sortedArray(test7);
	printList(test7);
	assert(test7.size() == 4);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #8: Remove root as leaf" << endl;
	assert(tree.remove(9));
	assert(tree.remove(8));
	assert(tree.remove(13));
	assert(tree.remove(12));
	vector<int> test8;
	tree.sortedArray(test8);
	printList(test8);
	assert(test8.size() == 0);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #9: Deep copy for a tree of 100 random values" << endl;
	for(int i = 0; i < 100; i++){
		int value = rand() % 1000;
		tree.insert(value);
	}

	BSTree * copyTree = new BSTree(tree);
	vector<int> test9, test9Copy;
	tree.sortedArray(test9);
	copyTree->sortedArray(test9Copy);
	assert(test9Copy == test9);
	cerr << "\n\t========================PASS========================\n" << endl;

	cerr << "\n\tTEST #10: Test deletion and Destructor" << endl;
	for(int i = 0; i < 1000; i++){
		tree.remove(i);
	}
	assert(tree.empty());
	delete copyTree;
	cerr << "\n\t========================PASS========================\n" << endl;

	return 0;
}