本文整理汇总了C++中BinaryTree类的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree类的具体用法?C++ BinaryTree怎么用?C++ BinaryTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BinaryTree类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
//const char* src[] = {"1", "2", "3", "4", "5", "6", "7"};
const char* src[] = {"1", "2", "3", "#", "#", "6", "7", "#", "#", "#", "#"};
BinaryTree bt;
TreeNode* root = bt.levelOrderDeserialize(src, 7);
bt.preorderTraversal(root);
cout << endl;
bt.levelOrderTraversal(root);
cout << endl;
cout << bt.levelOrderSerialize(root) << endl;
/// test zigzag
cout << "zigzag with 2 stacks: " << endl;
bt.zigzagOrderTraversal(root);
cout << "zigzag with 2 stacks and recursion:" << endl;
bt.zigzagOrderTraversal2(root);
cout << endl;
cout << "zigzag with recursive: " << endl;
bt.zigzagOrderTraversal3(root);
cout << endl;
/// height
cout << "height=" << bt.height(root) << endl;
/// test number of nodes
cout << "number of nodes(Recursive):" << bt.numberOfNodes(root) << endl;
cout << "number of nodes(Stack):" << bt.numberOfNodesWithStack(root) << endl;
// test number of leaf nodes
cout << "number of leaf nodes(Recursive):" << bt.numberOfLeafNodes(root) << endl;
cout << "number of leaf nodes(Stack):" << bt.numberOfLeafNodesWithStack(root) << endl;
bt.release(root);
return 0;
}
示例2: main
int main(int argc,char ** argv)
{
BinaryTree bt;
for(int i=0; i < 100; ++i) bt.insert(rand()%1000);
bt.walkMid();
cout <<"max" << bt.max()->weight << endl;
/*
BinaryTree::node *p = bt.min();
while(p != NULL)
{
cout << p->weight << endl;
p = bt.nextNode(p);
}*/
BinaryTree bb;
bb.insert(4);
bb.insert(3);
bb.insert(8);
bb.insert(1);
bb.insert(2);
bb.insert(6);
bb.insert(7);
bb.insert(10);
bb.insert(11);
bb.walkMid();
bb.remove(bb.search(7));
bb.remove(bb.search(10));
bb.remove(bb.search(11));
bb.remove(bb.search(4));
bb.walkMid();
return 0;
}
示例3: main
int main (void) {
string input;
Vertex* root;
Vertex* temp;
int count = 1;
BinaryTree* g = new BinaryTree();
// adding root element
root = g->addVertex(count++);
bool ok = true;
while (cin >> input) {
if (input != "()") {
if (ok) {
input = input.substr(1, input.size() - 2);
size_t found = input.find (",");
string value = input.substr(0, (int)found);
string pos = input.substr((int)found+1);
int v = 0;
reverse (value.begin(), value.end());
for (int i = 0; i < value.size(); i++)
v += (value[i] - 48) * pow(10, i);
if (pos.size() == 0) {
if (root->value == -1)
root->value = v;
else {
ok = false;
continue;
}
} else {
temp = root;
for (int i = 0; i < pos.size(); i++) {
if (pos[i] == 'L') {
if (temp->left == NULL) {
temp->left = g->addVertex(count);
temp = temp->left;
++count;
} else {
temp = temp->left;
}
} else if (pos[i] == 'R') {
if (temp->right == NULL) {
temp->right = g->addVertex(count);
temp = temp->right;
++count;
} else {
temp = temp->right;
}
}
}
if (temp->value == -1)
temp->value = v;
else
ok = false;
}
continue;
}
} else {
if (!ok) {
cout << "not complete" << endl;
} else {
for (int i = 0; i < g->allVertexes.size(); i++) {
if (g->allVertexes[i]->value == -1) {
ok = false;
break;
}
}
if (!ok) {
cout << "not complete" << endl;
} else {
queue<Vertex*> q;
q.push(root);
bool first = true;
while (!q.empty()) {
temp = q.front();
q.pop();
if (!first) cout << " ";
else first = false;
cout << temp->value;
if (temp->left != NULL) q.push(temp->left);
if (temp->right != NULL) q.push(temp->right);
}
cout << endl;
}
}
ok = true;
delete g;
//.........这里部分代码省略.........
示例4: main
main(int, char **)
{
String cmd;
BinaryTree<String> bt;
while (cmd != 'q')
{
cout << endl;
cout << "quit - q" << endl;
cout << "insert - i" << endl;
cout << "remove - r" << endl;
cout << "clear - c" << endl;
cout << "show - s" << endl;
cout << "copy - C" << endl;
cout << "pre-order - p" << endl;
cout << "post-order - o" << endl;
cout << "in-order - n" << endl;
cout << "level-order - l" << endl;
cout << "choose one: ";
cin >> cmd;
switch (cmd[0])
{
case 'q':
break;
case 'p':
{
cout << endl;
cout << "pre order ... ";
if (bt.preOrder(work) != OK)
cout << "pre order failed !!!" << endl;
cout << endl;
cout << "pre order iterator ... ";
BinaryTreeIterator_PreOrder<String> iter(bt);
for ( ; ! iter.done(); iter++)
{
cout << iter() << " ";
}
cout << endl;
}
break;
case 'o':
{
cout << endl;
cout << "post order ... ";
if (bt.postOrder(work) != OK)
cout << "post order failed !!!" << endl;
cout << endl;
cout << "post order iterator ... ";
BinaryTreeIterator_PostOrder<String> iter(bt);
for ( ; ! iter.done(); iter++)
{
cout << iter() << " ";
}
cout << endl;
}
break;
case 'n':
{
cout << endl;
cout << "in order ... ";
if (bt.inOrder(work) != OK)
cout << "in order failed !!!" << endl;
cout << endl;
cout << "in order iterator ... ";
BinaryTreeIterator_InOrder<String> iter(bt);
for ( ; ! iter.done(); iter++)
{
cout << iter() << " ";
}
cout << endl;
}
break;
case 'l':
{
cout << endl;
cout << "level order ... ";
if (bt.levelOrder(work) != OK)
cout << "level order failed !!!" << endl;
cout << endl;
cout << "level order iterator ... ";
BinaryTreeIterator_LevelOrder<String> iter(bt);
for ( ; ! iter.done(); iter++)
{
cout << iter() << " ";
}
cout << endl;
}
break;
case 'i':
cout << endl;
cout << "character to insert: ";
cin >> cmd;
bt.insert(cmd);
break;
case 'r':
cout << endl;
cout << "character to remove: ";
cin >> cmd;
//.........这里部分代码省略.........
示例5: testBinaryTree_get_lower_nearest
void testBinaryTree_get_lower_nearest()
{
BinaryTree<int> tree;
tree.add(3, 3);
tree.add(5, 5);
tree.add(1, 1);
tree.add(9, 9);
tree.add(7, 7);
EXPECT_EQ(0, tree.get_lower_nearest(-1));
EXPECT_EQ(1, tree.get_lower_nearest(1));
EXPECT_EQ(1, tree.get_lower_nearest(2));
EXPECT_EQ(3, tree.get_lower_nearest(3));
EXPECT_EQ(3, tree.get_lower_nearest(4));
EXPECT_EQ(5, tree.get_lower_nearest(5));
EXPECT_EQ(5, tree.get_lower_nearest(6));
EXPECT_EQ(7, tree.get_lower_nearest(7));
EXPECT_EQ(7, tree.get_lower_nearest(8));
EXPECT_EQ(9, tree.get_lower_nearest(9));
EXPECT_EQ(9, tree.get_lower_nearest(10));
}
示例6: main
int main() {
BinaryTree<int> btree;
vector<int> tree;
btree.Add(0);
btree.Add(4);
btree.Add(5);
btree.Add(-2);
btree.Add(-1);
btree.Add(8);
tree = btree.Walk();
cout << "[ ";
for (auto x = tree.begin(); x != tree.end(); ++x) {
cout << (*x) << " ";
}
cout << "]\n";
BinaryTree<string> btreestr;
vector<string> treestr;
btreestr.Add("YoYo");
btreestr.Add("Hi");
btreestr.Add("Howdy");
btreestr.Add("Sup");
btreestr.Add("Hello");
btreestr.Add("Wassup");
treestr = btreestr.Walk();
cout << "[ ";
for (auto x = treestr.begin(); x != treestr.end(); ++x) {
cout << (*x) << " ";
}
cout << "]\n";
BinaryTree<double> btreedbl;
vector<double> treedbl;
btreedbl.Add(0.001);
btreedbl.Add(-0.001);
btreedbl.Add(0.0123);
btreedbl.Add(123123.00);
btreedbl.Add(123121.001);
btreedbl.Add(0.021);
treedbl = btreedbl.Walk();
cout << "[ ";
for (auto x = treedbl.begin(); x != treedbl.end(); ++x) {
cout << (*x) << " ";
}
cout << "]\n";
{ BinaryTree<int> nbtree; }
return 0;
}
示例7: 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;
}
示例8: BT_depth_first
void BT_depth_first()
{
BinaryTree<int> T;
T.insert(5);
T.insert(104);
T.insert(2);
T.insert(3);
T.insert(10);
T.insert(130);
T.insert(60);
T.insert(9);
T.insert(78);
T.insert(11);
T.insert(8);
cout << "BinaryTree inorder traverse" << endl;
T.inorder();
/* You see the depth-first traverse result
* is ordered
*/
cout << "BinaryTree depth-first traverse" << endl;
T.depth_first();
}
示例9: test1
void test1()
{
int pre[]={1,2,4,8,9,5,10,3,6,7};
int in[]={8,4,9,2,10,5,1,6,3,7};
int post[]={8,9,4,10,5,2,6,7,3,1};
BinaryTree<int> btree;
//btree.CreateWithPre(pre,in,sizeof(pre)/sizeof(pre[0]));
btree.CreateWithPost(post,in,sizeof(post)/sizeof(post[0]));
btree.PreOrder();
btree.InOrder();
btree.PostOrder();
btree.PreOrder_NR();
btree.InOrder_NR();
btree.PostOrder_NR();
btree.LevelOrder();
cout<<btree.Depth()<<endl;
btree.PrintEdge1();
}
示例10: EGthanX
std::vector<int> EGthanX(const BinaryTree<int> & BST, const int x) {
std::vector<int> ret;
EGthanX_impl(BST.GetRoot(), x, ret);
return ret;
}
示例11: getFunctions
// reads the null function set and adds the functions in there to our binary tree
// this helps with creating the tree faster than reading through all the file and adding missing functions
void getFunctions()
{
char * functionBuffer;
// check if get calls saw a null function and passed on the call to getFunctions
if(!transfer)
{
// in case start of file, read till the first call which is typically the null function
while (!feof(optStream))
{
fgets(buffer, max_buffer, optStream);
if(strstr(buffer, "Call") != NULL)
{
break;
}
}
// make sure we are at the null function, if not return
if(strstr(buffer, "null function") == NULL)
{
transfer = true;
return;
}
}
int count = callGraph->numberOfFunctions;
list<string> functions;
// read through the null function and populate the function list
while (!feof(optStream))
{
fgets(buffer, max_buffer, optStream);
// parse the buffer line into the function being called
strtok(buffer, "'");
functionBuffer = strtok(NULL, "'");
// check if we have reached the end of the null function
if (functionBuffer == NULL)
{
break;
}
// add the function to the binary tree and update the counts if it is a unique add
string function(functionBuffer);
if(binaryTree.insert(new BinaryNode(function, count)))
{
functions.push_back(function);
count++;
}
}
callGraph->numberOfFunctions = count; // update the call graph object
fg.resize(count); // update size of the function graph
// update the necessary lists
list<string>::iterator it = functions.begin();
for(int i = 0; i < count; i++)
{
names.push_back(*it);
callCount.push_back(0);
it++;
}
}
示例12: getCalls
// generate the call graphs for each function
void getCalls()
{
int currentFunction = -1;
char * functionBuffer;
// read through stream creating the call graph for each function
while(!feof(optStream))
{
// check if there was a transfer in the buffer from the getFunctions function
if(!transfer)
{
fgets(buffer, max_buffer, optStream);
}
transfer = false;
// if we reach an empty line we are at the next function
if(strlen(buffer) < 5)
{
currentFunction = -1;
}
else if(currentFunction == -1)
{
// check if we have the null function. If we do, call get functions to create a function list
if(strstr(buffer, "null function"))
{
getFunctions();
fgets(buffer, max_buffer, optStream);
}
// tokenize the function being called
strtok(buffer, "'");
functionBuffer = strtok(NULL, "'");
// find the function's id and use to populate the right functiongraph
if(functionBuffer != NULL)
{
// find the node in the binary tree
BinaryNode *function = binaryTree.Find(string(functionBuffer));
// if the function does not exist in the binary tree add it in
if(function == NULL)
{
function = new BinaryNode(string(functionBuffer), callGraph->numberOfFunctions);
binaryTree.insert(function);
callGraph->numberOfFunctions++;
names.push_back(string(functionBuffer));
fg.resize(callGraph->numberOfFunctions);
callCount.push_back(0);
}
currentFunction = function->ID;
}
}
else
{
strtok(buffer, "'");
functionBuffer = strtok(NULL, "'");
// if we have a function add it to the function graph
if( functionBuffer != NULL)
{
BinaryNode *function = binaryTree.Find(string(functionBuffer));
// if the function is not in the binary tree, add it
if(function == NULL)
{
function = new BinaryNode(string(functionBuffer), callGraph->numberOfFunctions);
binaryTree.insert(function);
callGraph->numberOfFunctions++;
names.push_back(string(functionBuffer));
fg.resize(callGraph->numberOfFunctions);
callCount.push_back(0);
}
pair<set<int>::iterator, bool> result = fg[currentFunction].insert(function->ID);
// increase the call count of the function it was added to the function list
// if the function was not aded to the list, it is being called multiple times
if( result.second)
{
callCount[function->ID]++;
}
}
}
}
}