KeiroKamioka

Tree!

Mar 3rd, 2021
870
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class node
  5. {
  6.     int value;
  7.     node* left;
  8.     node* right;
  9.  
  10. public:
  11.     node(int value, node* left, node* right)
  12.     {
  13.         this->value = value;
  14.         this->left = left;
  15.         this->right = right;
  16.     }
  17.  
  18.     void add(int value)
  19.     {
  20.         if (value < this->value)
  21.         {
  22.             if (left == nullptr)
  23.                 left = new node(value, nullptr, nullptr);
  24.             else
  25.                 left->add(value);
  26.         }
  27.         else
  28.         {
  29.             if (right == nullptr)
  30.                 right = new node(value, nullptr, nullptr);
  31.             else
  32.                 right->add(value);
  33.         }
  34.     }
  35.  
  36.     void print()
  37.     {
  38.         if (left != nullptr) left->print();
  39.         cout << value << " ";
  40.         if (right != nullptr) right->print();
  41.     }
  42.  
  43.     //Return the sum of all the numbers in the tree
  44.     int sum()
  45.     {
  46.         // Your code starts here
  47.  
  48.         // Your code ends here
  49.     }
  50.  
  51.     //Prints all the leaves in the tree (i.e. nodes that have no children)
  52.     void print_leaves()
  53.     {
  54.         // Your code starts here
  55.  
  56.         // Your code ends here
  57.     }
  58.  
  59.     //Finds the largest number in the tree
  60.     int max()
  61.     {
  62.         // Your code starts here
  63.  
  64.         // Your code ends here
  65.     }
  66. };
  67. //After
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×