fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Node
  4. {
  5. public:
  6. Node *left,
  7. *right;
  8. int value;
  9. public:
  10. Node(void) {
  11. left = NULL;
  12. right = NULL;
  13. value = -1;
  14. }
  15. Node* getLeft() {return left;}
  16. Node* getRight() {return right;}
  17. int getValue() {return value;}
  18.  
  19. bool insert(Node** root, int val) {
  20. if (!*root) {
  21. *root = new Node();
  22. (*root)->value = val;
  23. (*root)->left = NULL; (*root)->right = NULL;
  24. }
  25. else {
  26. if (val <= (*root)->value)
  27. insert(&((*root)->left), val);
  28. else
  29. insert(&((*root)->right), val);
  30. }
  31. return true;
  32. }
  33.  
  34. int inOrder(Node *root, int x) {
  35. if (root && x > 0) {
  36. if (root->left)
  37. x--;
  38. x = inOrder(root->left, x);
  39. cout << root->value << " ";
  40. if (root->right)
  41. x--;
  42. x = inOrder(root->right, x);
  43. }
  44. return x;
  45. }
  46. ~Node(void);
  47. };
  48.  
  49. int main() {
  50. Node* root = NULL;
  51. root->insert(&root, 100);
  52. root->insert(&root, 50);
  53. root->insert(&root, 150);
  54. root->insert(&root, 25);
  55. root->insert(&root, 75);
  56. root->insert(&root, 175);
  57. root->insert(&root, 125);
  58. root->insert(&root, 110);
  59. cout << "In-Order: ";
  60. root->inOrder(root, 5);
  61. cout << endl;
  62. return 0;
  63. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
In-Order: 25 50 75 100 150