fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node {
  5. public:
  6. int data;
  7. Node *left;
  8. Node *right;
  9. };
  10.  
  11. Node* GetNewNode(int data) {
  12. Node *newNode = new Node();
  13. newNode->data = data;
  14. newNode->left = NULL;
  15. newNode->right = NULL;
  16. return newNode;
  17. }
  18.  
  19. void Insert(Node **root, int data)
  20. {
  21. if (*root == NULL) { // empty tree
  22. *root = GetNewNode(data);
  23. }
  24. else if ((*root)->data < data) {
  25. Insert(&((*root)->left), data);
  26. }
  27. else {
  28. Insert(&((*root)->right), data);
  29. }
  30. }
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34. Node *treeRoot = NULL;
  35.  
  36. Insert(&treeRoot, 15);
  37. Insert(&treeRoot, 23);
  38. Insert(&treeRoot, 10);
  39.  
  40. }
Success #stdin #stdout 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty