Facebook
From Mature Hummingbird, 5 Years ago, written in Plain Text.
This paste is a reply to Untitled from Unique Gibbon - view diff
Embed
Download Paste or View Raw
Hits: 326
  1. #include <iostream>
  2.  
  3. typedef struct REF2 {
  4.         int value;
  5.         REF2 * left = NULL;
  6.         REF2 * right = NULL;
  7. }ref2;
  8.  
  9. void dodaj(ref2 *&t, int value) {
  10.         if (t == NULL) {
  11.                 ref2* nowy = new ref2{ value };
  12.                 t = nowy;
  13.         }
  14.         else if (value < t->value) {
  15.                 dodaj(t->left, value);
  16.         }
  17.         else {
  18.                 dodaj(t->right, value);
  19.         }
  20. }
  21. std::ostream& operator << (std::ostream& stream, ref2* &bst) {
  22.         if (bst->left != NULL) {
  23.                 stream << bst->left;
  24.         }
  25.         stream << bst->value << " ";
  26.         if (bst->right != NULL) {
  27.                 stream << bst->right;
  28.         }
  29.         return stream;
  30. }
  31.  
  32. int main() {
  33.         ref2* tree = new ref2{ 5 };
  34.         dodaj(tree, 10);
  35.         dodaj(tree, 1);
  36.         dodaj(tree, 2);
  37.         std::cout << tree;
  38.         delete tree;
  39.         return 0;
  40. }