//int main( void )
int tree_traversal_main()
{
	Tree* tree = new Tree();
	tree->AddNode( 30 );
	tree->AddNode( 10 );
	tree->AddNode( 20 );
	tree->AddNode( 40 );
	tree->AddNode( 50 );

	cout << "In order traversal" << endl;
	tree->InOrder( tree->Root( ) );
	cout << endl << endl;

	cout << "Pre order traversal" << endl;
	tree->PreOrder( tree->Root( ) );
	cout << endl << endl;

	cout << "Pre order traversal - no recursion" << endl;
	tree->PreOrder_NoRecursion( tree->Root( ) );
	cout << endl << endl;

	cout << "Post order traversal" << endl;
	tree->PostOrder( tree->Root( ) );
	cout << endl << endl;
	
	cout << "Tree Height : " << tree->Height( tree->Root() ) << endl;
	cout << endl << endl;

	delete tree;
	return 0;
}
Example #2
0
int main(int argc, char *argv[]){
    Tree<int> Apple;

    Apple.Insert(8);
	Apple.Insert(10);
	Apple.Insert(4);
	Apple.Insert(2);
	Apple.Insert(6);
	Apple.Insert(5);
	
	// Apple.Remove(5);
	// Apple.Remove(4);

    
    cout << "PRE-ORDER:" << endl;
    Apple.TraversePreOrder(Apple.Root());
	cout << endl << endl;
	
	cout << "POS-ORDER:" << endl;
	Apple.TraversePosOrder(Apple.Root());
	cout << endl << endl;
		
	cout << "IN-ORDER:" << endl;
	Apple.TraverseInOrder(Apple.Root());
	cout << endl << endl;
	
	cout << "LEVEL-ORDER:" << endl;
	Apple.TraverseLevelOrder();
	cout << endl << endl;
	
    return 0;
}
Example #3
0
int main(int argc, const char * argv[])
{
    //测试树
    cout<<"开始测试树\n";
    Tree<char> t;
    t.InsertChild('A');
    for (int  i = 0; i < 3; i ++) {
        t.Root();
        t.InsertChild('B'+i);
    }
    for (int i = 0; i < 2; i++) {
        t.Root();
        t.FirstChild();
        t.InsertChild('E'+i);
    }
    
    t.DisplayTree();
    
    return 0;
}
Example #4
0
File: tree.cpp Project: joyjitc/lab
// Test main program
int main() {
   Tree* tree = new Tree();
   char data;
   int choice;
   while(1)
   {
      cout<<"1> Add Node\n2> Pre order traversal\n3> In order traversal\n4>Post order traversal\n5> Exit\nCHOOSE WISELY::";
      cin>>choice;
      switch(choice)
      {
        case 1:
        cout<<"\nEnter data:";
        cin>>data;
        tree->addNode(data);
        break;
        case 2:
        cout << "Pre order traversal" << endl;
        tree->Preorder(tree->Root());
        cout << endl;
        break;
        case 3:
        cout << "In order traversal" << endl;
        tree->Inorder(tree->Root());
        cout << endl;
        break;
        case 4:
        cout << "Post order traversal" << endl;
        tree->Postorder(tree->Root());
        cout << endl;
        break;
        case 5:
        exit(0);  



      }
   }
   delete tree;
   return 0;
} 
std::string 




int main( void )
{
	Tree* tree = new Tree();
	tree->AddNode( 30 );
	tree->AddNode( 10 );
	tree->AddNode( 20 );
	tree->AddNode( 40 );
	tree->AddNode( 50 );

	cout << "In order traversal" << endl;
	tree->InOrder( tree->Root( ) );
	cout << endl << endl;

	cout << "Pre order traversal" << endl;
	tree->PreOrder( tree->Root( ) );
	cout << endl << endl;

	cout << "Pre order traversal - no recursion" << endl;
	tree->PreOrder_NoRecursion( tree->Root( ) );
	cout << endl << endl;

	cout << "Post order traversal" << endl;
	tree->PostOrder( tree->Root( ) );
	cout << endl << endl;
	
	cout << "Tree Height : " << tree->Height( tree->Root() ) << endl;
	cout << endl << endl;

    cout << Round( 123.456, 2 ) << endl;


	delete tree;

	return 0;
}
Example #6
0
// Test main program
int main() {
	int a=0,dat;
	char ch;
   Tree* tree = new Tree();
   cout<<"Enter y/Y to enter new node, any other key to stop adding node:  ";
   cin>>ch;
   do{
   cout<<"Enter node: ";
   cin>>dat;
   tree->addNode(dat);
   cout<<"Wanna continue adding? ";
   cin>>ch;
   }while(ch=='y' || ch=='Y');
   
   while(1){
   cout<<"Enter your choice:"<<endl<<"1. InOrder 2. PreOrder 3.PostOrder 4. Exit"<<endl;
   cin>>a;
   switch(a)
   {
   
   case 1:cout << "In order traversal" << endl;
   tree->inOrder(tree->Root());
   cout << endl;break;

   case 2:cout << "Pre order traversal" << endl;
   tree->preOrder(tree->Root());
   cout << endl;break;

   case 3:cout << "Post order traversal" << endl;
   tree->postOrder(tree->Root());
   cout << endl;break;
   
   case 4:exit(0);
   }
}
   delete tree;
   return 0;
}
int main()
{
	Tree* tree = new Tree();
	tree->addNode(30);
	tree->addNode(10);
	tree->addNode(20);
	tree->addNode(40);
	tree->addNode(50);

	cout << "In order traversal" << endl;
	tree->inOrder(tree->Root());
	cout << endl;

	cout << "Pre order traversal" << endl;
	tree->preOrder(tree->Root());
	cout << endl;

	cout << "Post order traversal" << endl;
	tree->postOrder(tree->Root());
	cout << endl;

	delete tree;
}
Example #8
0
int _tmain(int argc, _TCHAR* argv[])
{
    Tree<LinkNode<int>> linkTree(new LinkNode<int>());
    Tree<MAryNode<int, 3>> mAryTree(new MAryNode<int, 3>());
    Tree<TrickyNode<int>> trickyTree(new TrickyNode<int>());

    Tree<FullNode<int>>* simpleTree = TreeConstructor(L"0-l1,0-r2,1-l3,1-r4,2-l5,2-r6");
    Tree<FullNode<int>>* deepTree = TreeConstructor(L"0-l1,0-r2,1-l3,2-r4,3-l5,4-r6,5-l7,6-l8,6-r9");
    Tree<FullNode<int>>* sparseTree = TreeConstructor(L"0-l1,0-r2,1-l3,1-r4,2-l5,2-r6,3-r7,4-l8,5-l9,6-r10");

    wprintf(L"PreOrder:\n");
    PrintPreOrderRecursive(simpleTree->Root()); wprintf(L"\n");
    PrintPreOrderIterativeStack(simpleTree->Root()); wprintf(L"\n");
    PrintPreOrderParentPointer(simpleTree->Root()); wprintf(L"\n");
    PrintPreOrderRecursive(deepTree->Root()); wprintf(L"\n");
    PrintPreOrderIterativeStack(deepTree->Root()); wprintf(L"\n");
    PrintPreOrderParentPointer(deepTree->Root()); wprintf(L"\n");

    wprintf(L"InOrder:\n");
    PrintInOrderRecursive(simpleTree->Root()); wprintf(L"\n");
    PrintInOrderIterativeStack(simpleTree->Root()); wprintf(L"\n");
    PrintInOrderParentPointer(simpleTree->Root()); wprintf(L"\n");
    PrintInOrderParentPointer_Reduced(simpleTree->Root()); wprintf(L"\n");
    PrintInOrderRecursive(deepTree->Root()); wprintf(L"\n");
    PrintInOrderIterativeStack(deepTree->Root()); wprintf(L"\n");
    PrintInOrderParentPointer(deepTree->Root()); wprintf(L"\n");
    PrintInOrderParentPointer_Reduced(deepTree->Root()); wprintf(L"\n");

    wprintf(L"PostOrder:\n");
    PrintPostOrderRecursive(simpleTree->Root()); wprintf(L"\n");
    PrintPostOrderIterativeStack(simpleTree->Root()); wprintf(L"\n");
    PrintPostOrderParentPointer(simpleTree->Root()); wprintf(L"\n");
    PrintPostOrderRecursive(deepTree->Root()); wprintf(L"\n");
    PrintPostOrderIterativeStack(deepTree->Root()); wprintf(L"\n");
    PrintPostOrderParentPointer(deepTree->Root()); wprintf(L"\n");
    PrintPostOrderRecursive(sparseTree->Root()); wprintf(L"\n");
    PrintPostOrderIterativeStack(sparseTree->Root()); wprintf(L"\n");
    PrintPostOrderParentPointer(sparseTree->Root()); wprintf(L"\n");

    wprintf(L"BFS:\n");
    PrintBFSIterativeQueue(simpleTree->Root()); wprintf(L"\n");
    PrintBFSLogLinear(simpleTree->Root()); wprintf(L"\n");
    PrintBFSIterativeQueue(deepTree->Root()); wprintf(L"\n");
    PrintBFSLogLinear(deepTree->Root()); wprintf(L"\n");
    PrintBFSIterativeQueue(sparseTree->Root()); wprintf(L"\n");
    PrintBFSLogLinear(sparseTree->Root()); wprintf(L"\n");

    wprintf(L"Zig Zag:\n");
    PrintZigZagTree(simpleTree->Root()); wprintf(L"\n");
    PrintZigZagTree(deepTree->Root()); wprintf(L"\n");
    PrintZigZagTree(sparseTree->Root()); wprintf(L"\n");

    getchar();

    return 0;
}
Example #9
0
int main(int argc, char *argv[])
{
    Tree<int, int> tree;
    unsigned int num, key, key2, root;
    bool error;


    srand(time(NULL));

    build_tree(&tree, 0, 20);

    key = rand_int(0, 20);
    key2 = rand_int(0, 20);
    num = tree.SearchByKey(key2, &error);

    print_valid(&tree);

    cout << endl << "[Remove by key " << key << "]" << endl;

    if (tree.RemoveByKey(key))
    {
        cout << "ERROR removing by key " << key << endl;
    }

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    cout << endl << "[Remove ( key " << key2 << ") by data " << num << "]"
         << endl;

    if (tree.RemoveByData(num))
    {
        cout << "ERROR removing by data " << num << endl;
    }

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    root = tree.Root();
    cout << endl << "[Remove root " << root << " by key " << root << "]" << endl;

    if (tree.RemoveByKey(root))
    {
        cout << "ERROR removing by key " << root << endl;
    }

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    cout << endl << "[Insert]" << endl;
    build_tree(&tree, 20, 23);

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    root = tree.Root();
    cout << endl << "[Remove root " << root << " by key " << root << "]" << endl;

    if (tree.RemoveByKey(root))
    {
        cout << "ERROR removing by key " << root << endl;
    }

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    cout << endl << "[Clear tree]" << endl;
    tree.Clear();

    print_valid(&tree);

    ////////////////////////////////////////////////////////////////

    cout << endl << "[Build new tree]" << endl;
    build_tree(&tree, 0, 5);

    print_valid(&tree);

    return 0;
}