当前位置: 首页>>代码示例>>C++>>正文


C++ BinaryTree::inOrderTraversal方法代码示例

本文整理汇总了C++中BinaryTree::inOrderTraversal方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree::inOrderTraversal方法的具体用法?C++ BinaryTree::inOrderTraversal怎么用?C++ BinaryTree::inOrderTraversal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryTree的用法示例。


在下文中一共展示了BinaryTree::inOrderTraversal方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: tryTree

void NodeController:: tryTree()
{
    BinaryTree<int> sampleTree;
    sampleTree.insert(7);
    sampleTree.insert(5);
    sampleTree.insert(213);
    sampleTree.insert(-129);
    sampleTree.insert(5);
    sampleTree.insert(123124);
    sampleTree.insert(0);
    
    cout<< "The in order traversal\n";
    sampleTree.inOrderTraversal(sampleTree.getRoot());
    cout << endl << endl;

    cout<< "The pre order traversal\n";
    sampleTree.preOrderTraversal(sampleTree.getRoot());
    cout << endl << endl;
    
    cout<< "The post order traversal\n";
    sampleTree.postOrderTraversal(sampleTree.getRoot());
    cout << endl << endl;
    
    cout << "Size of tree = " << sampleTree.getSize() << endl;
}
开发者ID:jpowell03,项目名称:SummerNodeProject,代码行数:25,代码来源:NodeController.cpp

示例2: tryTree

void NodeController :: tryTree()
{
    BinaryTree<int> sampleTree;
    sampleTree.insert(7);
    sampleTree.insert(5);
    cout << "The tree is this big: "<< sampleTree.getSize() << endl;
    sampleTree.insert(213);
    sampleTree.insert(-123);
    cout << "The tree is this big: "<< sampleTree.getSize() << endl;
    sampleTree.insert(5);
    cout << "The tree is this big: "<< sampleTree.getSize() << endl;
    sampleTree.insert(1231234);
    sampleTree.insert(0);
    cout << "The tree is this big: "<< sampleTree.getSize() << endl;
    
    cout << "The in order traversal:" << endl;
    sampleTree.inOrderTraversal(sampleTree.getRoot());
    cout << endl;
    
    cout << "The pre order traversal:" << endl;
    sampleTree.preOrderTraversal(sampleTree.getRoot());
    cout << endl;
    
    cout << "The post order traversal:" << endl;
    sampleTree.postOrderTraversal(sampleTree.getRoot());
    cout << endl;
}
开发者ID:dbeecroft,项目名称:SummerNodeProject,代码行数:27,代码来源:NodeController.cpp

示例3: main

int main() 
{
    // Grab current cout flags, used for when the stream is altered and the user wants
    //   to reset it to the default values: 
    ios::fmtflags f( cout.flags() );

    // Test 1:
    {
      SinglyLinkedList<char>* list = new SinglyLinkedList<char>();

      char var = 'A';

      for (int i = 1; i <= 10; i++) 
        {
        list->pushBack(var);
        var++;
      }

      cout << endl << "Test 1 - Char list:" << endl << "\t";
      list->print();
        cout << endl;
    }

    // Reset cout stream flags:
    cout.flags(f);

    // Test 2:
    {
        DoublyLinkedList<myType>* list = new DoublyLinkedList<myType>();

        myType var = 1;

        for (int i = 1; i <= 10; i++) 
        {
            list->pushBack(var);
            var++;
        }

        cout << endl << "Test 2 - Int list:" << endl << "\t";
        list->print();
        cout << endl;
    }

    // Reset cout stream flags:
    cout.flags(f);

    // Test 3:
    {
        BinaryTree<int>* bt = new BinaryTree<int>();

        // Insert a bunch of stuff:
        bt->insert(11);
        bt->insert(6);
        bt->insert(8);
        bt->insert(19);
        bt->insert(4);
        bt->insert(10);
        bt->insert(5);
        bt->insert(17);
        bt->insert(43);
        bt->insert(49);
        bt->insert(31);

        // Print tree preOrderTraversal:
        cout << endl << "Binary Tree PreOrderTravesal:" << endl << "\t";
        bt->preOrderTraversal();
        cout << endl;

        // Print tree inOrderTraversal:
        cout << endl << "Binary Tree InOrderTravesal:" << endl << "\t";
        bt->inOrderTraversal();
        cout << endl;

        // Print tree postOrderTraversal:
        cout << endl << "Binary Tree PostOrderTravesal:" << endl << "\t";
        bt->postOrderTraversal();
        cout << endl;
    }

    cout << endl;

    return 0;
}
开发者ID:calebwherry,项目名称:Cpp-Libraries,代码行数:83,代码来源:data-structure-tests.cpp

示例4: main

int main() {
	//read file
	string file;
	cout << "Enter a file: ";
	cin >> file;
	ifstream is(file.c_str(), ifstream::in);
	
	//print contents of file
	cout << "Given input: " << endl;
	int data;
	BinaryTree tree = *(new BinaryTree());
	while(!is.eof()) {
		is >> data;
		if(is.good()) {
			cout << data << endl;
			tree.insert(data);
		}
	}
	cout << endl << "Tree: " << endl << tree << endl;
	
	//print tree
	int* cost = new int(0);
	if(tree.size() < 16){
		cout << "Pre-order traversal: " << endl;
        tree.preOrderTraversal(tree.getRoot());
        cout << endl << "In-order traversal: " << endl;
        tree.inOrderTraversal(tree.getRoot());
        cout << endl << "Post-order traversal: " << endl;
        tree.postOrderTraversal(tree.getRoot());
        cout << endl;
    }else{
        cout << "Enter a output file location (which ends in .txt)" << endl;
        cin >> file;
		ofstream os(file.c_str());
		os << tree;
		cout << "Tree output to file: " << file << endl;
	}
	cout << "Number of nodes: " << tree.size() << endl;
	cout << "Average search cost: " << tree.avgcost(tree.getRoot(), cost) << endl << endl;
	
	//Pick an element to remove, remove it
	cout << "Please enter an element to remove: ";
	cin >> data;
	tree.remove(data, tree.getRoot());
    cout << endl;
	
	//print new tree
	*cost = 0;
	if(tree.size() < 16){
		cout << "In-order traversal: " << endl;
        tree.preOrderTraversal(tree.getRoot());
        cout << endl << "In-order traversal: " << endl;
        tree.inOrderTraversal(tree.getRoot());
        cout << endl << "Post-order traversal: " << endl;
        tree.postOrderTraversal(tree.getRoot());
        cout << endl;
    }else{
		cout << "Enter a output file location (which ends in .txt)" << endl;
        cin >> file;
        ofstream os(file.c_str());
		os << tree;
		cout << "Tree output to file" << file << endl;
	}
	cout << "Number of nodes: " << tree.size() << endl;
	cout << "Average search cost: " << tree.avgcost(tree.getRoot(), cost) << endl;
	
	delete cost;
}
开发者ID:sandanzuki,项目名称:sophomore-code-dump,代码行数:68,代码来源:main.cpp


注:本文中的BinaryTree::inOrderTraversal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。