#include <iostream>
using namespace std;
class Node
{
public:
Node *left,
*right;
int value;
public:
Node(void) {
left = NULL;
right = NULL;
value = -1;
}
Node* getLeft() {return left;}
Node* getRight() {return right;}
int getValue() {return value;}
bool insert(Node** root, int val) {
if (!*root) {
*root = new Node();
(*root)->value = val;
(*root)->left = NULL; (*root)->right = NULL;
}
else {
if (val <= (*root)->value)
insert(&((*root)->left), val);
else
insert(&((*root)->right), val);
}
return true;
}
int inOrder(Node *root, int x) {
if (root && x > 0) {
if (root->left)
x--;
x = inOrder(root->left, x);
cout << root->value << " ";
if (root->right)
x--;
x = inOrder(root->right, x);
}
return x;
}
~Node(void);
};
int main() {
Node* root = NULL;
root->insert(&root, 100);
root->insert(&root, 50);
root->insert(&root, 150);
root->insert(&root, 25);
root->insert(&root, 75);
root->insert(&root, 175);
root->insert(&root, 125);
root->insert(&root, 110);
cout << "In-Order: ";
root->inOrder(root, 5);
cout << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjbGFzcyBOb2RlCnsKcHVibGljOgogICAgTm9kZSAqbGVmdCwKCQkqcmlnaHQ7CglpbnQgdmFsdWU7CnB1YmxpYzoKCU5vZGUodm9pZCkgewoJbGVmdCA9IE5VTEw7CglyaWdodCA9IE5VTEw7Cgl2YWx1ZSA9IC0xOwoJfQoJTm9kZSogZ2V0TGVmdCgpIHtyZXR1cm4gbGVmdDt9CglOb2RlKiBnZXRSaWdodCgpIHtyZXR1cm4gcmlnaHQ7fQoJaW50IGdldFZhbHVlKCkge3JldHVybiB2YWx1ZTt9CgoJYm9vbCBpbnNlcnQoTm9kZSoqIHJvb3QsIGludCB2YWwpIHsKCWlmICghKnJvb3QpIHsKCQkqcm9vdCA9IG5ldyBOb2RlKCk7CgkJKCpyb290KS0+dmFsdWUgPSB2YWw7CgkJKCpyb290KS0+bGVmdCA9IE5VTEw7ICgqcm9vdCktPnJpZ2h0ID0gTlVMTDsKCX0KCWVsc2Ug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