本文整理汇总了C++中BinaryTree::getRoot方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree::getRoot方法的具体用法?C++ BinaryTree::getRoot怎么用?C++ BinaryTree::getRoot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryTree
的用法示例。
在下文中一共展示了BinaryTree::getRoot方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: timeInorder
void timeInorder(int size, int iter, bool linear, bool recur)
{
BinaryTree<int> bt;
// different tree structure depending on function parameters
if (linear) bt = generateLinkedList(size);
else bt = generateCompleteTree(size);
// time for iter iterations
clock_t start = clock();
// different method called depending on test type
if (recur) {
// do iter iterations
for (int i = 0; i <iter; ++i) {
deque<BinaryTree<int>::NodePtr> d;
bt.recursiveInorder(d,bt.getRoot());
}
}
else {
// do iter iterations
for (int i = 0; i <iter; ++i) {
deque<BinaryTree<int>::NodePtr> d;
bt.inorder(d,bt.getRoot());
}
}
clock_t end = clock();
double avg = double(end - start) / double(CLOCKS_PER_SEC) / iter;
// print out test params/results
string test_type = "InO ";
if (linear) test_type += "Lin ";
else test_type += "Bal ";
if (recur) test_type += "Re ";
else test_type += "It ";
cout << test_type << size << ' ' << iter << ' ' << setprecision(10) << avg << endl;
}
示例4: memInorder
void memInorder(int size, bool linear, bool recur)
{
BinaryTree<int> bt;
// different method called depending on test type
if (linear) bt = generateLinkedList(size);
else bt = generateCompleteTree(size-1);
// loops are for easier reading of massif graph
for(int i = 0; i < pow(2,10); ++i) {
int j = 1;
}
for(int i = 0; i < 5; ++i) {
if (recur) bt.dequelessRecInorder(bt.getRoot());
else bt.dequelessInorder(bt.getRoot());
}
}
示例5: main
int main() {
BinaryTree bt;
bt.addValue(5);
bt.addValue(10);
bt.addValue(8);
bt.addValue(9);
bt.addValue(7);
bt.addValue(11);
bt.addValue(3);
bt.addValue(2);
bt.addValue(4);
bt.addValue(20);
bt.addValue(25);
bt.addValue(30);
//bt.noRecursivePreOrderTraversal();
//std::cout<<""<<std::endl;
bt.preOrderTraversal(bt.getRoot());
std::cout<<""<<std::endl;
/*bt.posOrderTraversal(bt.getRoot());
std::cout<<""<<std::endl;
bt.inOrderTraversal(bt.getRoot());
std::cout<<""<<std::endl;
std::cout << "Max Value: " << bt.findMax() << std::endl;
std::cout << "Min Value: " << bt.findMin() << std::endl;
std::cout << "Height: " << bt.findHeight(bt.getRoot()) << std::endl;
Node * n = bt.findLowestCommonAncestor(40, 51);
if(n)
std::cout << "Common Ancestor: " << n->getValue() << std::endl;
else
std::cout << "nullptr" << std::endl;
delete(n);*/
bt.preOrderTraversal(bt.getRoot());
bt.heapfyVectorValue();
std::cout<<""<<std::endl;
std::cout<<""<<std::endl;
bt.preOrderTraversal(bt.heapfyVectorNode());
return 0;
}
示例6: main
int main(int argc, char const *argv[])
{
BinaryTree b;
for (int i = 1; i < 10; ++i)
{
b.insert(i);
}
ReverseEveryLevel(b.getRoot());
return 0;
}
示例7: main
int main(int argc, char* argv[]) {
if(argc < 2){
cout << "Incorrect arguments\n";
}
BinaryTree<int> tree = generateCompleteTree(atoi(argv[1]));
// test preorder
std::deque<BinaryTree<int>::NodePtr> traversal;
tree.preorder(traversal, tree.getRoot());
}
示例8: main
int main(int argc, char const *argv[])
{
BinaryTree b;
for (int i = 0; i < 10; ++i)
{
b.insert(i);
}
if(SearchElement(b.getRoot(),5))
cout<<"FOUND!!!"<<endl;
else
cout<<"NOT FOUND!!!"<<endl;
return 0;
}
示例9: 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;
}
示例10: main
int main()
{
BinaryTree<int> empty;
BinaryTree<int> complete = generateCompleteTree(30);
BinaryTree<int> list = generateLinkedList(100);
// empty tree is empty
assert(empty.isEmpty());
// size of complete tree
assert(complete.getSize() == 31);
// size of list
assert(list.getSize() == 100);
// depth of complete tree
assert(complete.getDepth() == 4);
// depth of list
assert(list.getDepth() == 99);
typedef BinaryTree<int>::NodeDeque traversal_type;
// root is at the front of a preorder traversal
traversal_type pre;
complete.preorder(pre, complete.getRoot());
assert(pre.front() == complete.getRoot());
// root is at the end of a postorder traversal
traversal_type post;
complete.postorder(post, complete.getRoot());
assert(post.back() == complete.getRoot());
// root is in the middle of an inorder traversal
traversal_type in;
complete.inorder(in, complete.getRoot());
assert(in.at(in.size()/2) == complete.getRoot());
// traversals should contain all nodes of the tree
assert(pre.size() == complete.getSize());
assert(post.size() == complete.getSize());
assert(in.size() == complete.getSize());
typedef BinaryTree<int>::NodePtr iterator;
const int query_value = 22;
// 22 should be in the list and complete tree
iterator complete_query = complete.simpleSearch(query_value);
iterator list_query = list.simpleSearch(query_value);
iterator empty_query = empty.simpleSearch(query_value);
assert(complete_query != NULL);
assert(list_query != NULL);
assert(empty_query == NULL);
// test copy ctor and assignment operator
BinaryTree<int> copied(complete);
BinaryTree<int> assigned;
assigned = complete;
// should have same size
assert(copied.getSize() == complete.getSize());
assert(assigned.getSize() == complete.getSize());
// should still have 22
iterator copy_query = copied.simpleSearch(query_value);
iterator assign_query = assigned.simpleSearch(query_value);
assert(copy_query != NULL);
assert(assign_query != NULL);
std::cout << "Passed!" << std::endl;
return 0;
}