コード例 #1
0
ファイル: rbt.cpp プロジェクト: divyang4481/mipt-hw
/*void PrintNode(const TTree<TFoo>::TNode &node, const TTree<TFoo>::TNode* Nil)
{
        cout << "Node: " << node.key.Value << endl;
        if (node.left != Nil)
                cout << "Left: " << node.left -> key.Value << endl;
        else cout << "Left: Nil" << endl;
        if (node.right != Nil)
                cout << "Right: " << node.right -> key.Value << endl;
        else cout << "Right: Nil" << endl;
        if (node.parent != Nil)
                cout << "Parent: " << node.parent -> key.Value << endl;
        else cout << "Parent: Nil" << endl;
        cout << endl;
}

void PrintTree(const TTree<TFoo>::TNode* node, const TTree<TFoo>::TNode* Nil)
{
        if (node != Nil)
        {
                if (node -> left != 0)
                        PrintTree(node  -> left, Nil);
                PrintNode(*node, Nil);
                if (node -> right != 0)
                        PrintTree(node -> right, Nil);
        }
}
void Test1()
{
        TTree<TFoo> tree;
        for (int i = 0; i < 10; ++i)
        {
                TTree<TFoo>::TNode* node = new TTree<TFoo>::TNode;
                node -> key.Value = rand() % 100;
                tree.Insert(node);
        }
                PrintTree(tree.Root, tree.Nil);
        while (!tree.IsEmpty())
        {
                const TTree<TFoo>::TNode* node = tree.Root;
                PrintNode(*node, tree.Nil);
                tree.Delete(node);
        }
}*/
void Test2()
{
        srand(time(NULL));
        TTree<int> tree;
        for (int i = 0; i < 8; ++i)
                tree.insert(rand() % 100);
        tree.PrintTree();
        TTree<int>::iterator iter = tree.begin();
        ++iter;
        tree.erase(iter);
        tree.PrintTree();
        TTree<int> tree2;
        tree2 = tree;
        tree2.PrintTree();
        tree.clear();
        tree.PrintTree();
        tree.insert(3);
        tree.PrintTree();
        tree.swap(tree2);
        tree.PrintTree();
        tree2.PrintTree();
        iter = tree.find(0);
        if (iter == tree.end())
                cout << "No element" << endl;
        else cout << "Element: " << *iter << endl;
        TTree<TFoo> tree3;
        for (int i = 0; i < 10; ++i)
        {
                TFoo tmp(i);
                tree3.insert(tmp);
        }
}
コード例 #2
0
ファイル: rbt.cpp プロジェクト: divyang4481/mipt-hw
static void Test3() {
    TTree<TFoo> a;
    for (int i = 0; i < 10; ++i)
        a.insert(TFoo(i));

    TTree<TFoo> b = a;
    for (int i = 0; i < 10; ++i)
        a.insert(TFoo(i));

    TTree<TFoo> c;
    a = c;

    for (TTree<TFoo>::iterator iter = a.begin(); iter != a.end(); ++iter) {
        c.insert(*iter);
    }
}
コード例 #3
0
int main(int argc, char **argv){
	
	TTree<int> tree;
	int value, choice, to_be_removed;
	bool toExit = false;

	while(!toExit){
		cout << "\nSelect an action \n1.insert, 2.remove, 3.print, 4.exit " << endl;
		cin >> choice;
		cout << "\noption selected : " << choice << endl;

		switch(choice){

			case 1: 
				cout << "Enter the keys to be inserted.. separated by spaces.. to terminate input, press any alphabet [Enter]" << endl;
				
				while(cin >> value){
					tree.insert(tree.getRoot(), value);
				}	
				cin.clear();
				cin.ignore(1);
				break;
			case 2:
				cout << "Enter the key of the element to be deleted.. " << endl;
				cin >> to_be_removed;
				tree.remove(NULL, tree.getRoot(), to_be_removed);
				break;
			case 3: 
				cout << "Printing the tree in in-order" << endl;
				tree.printTree(tree.getRoot());
				break;
			case 4:
				cout << "Exiting the program" << endl;
				// no break here
			default:
				toExit = true;
				tree.printTree(tree.getRoot());
		}
	}

	return 0;
}