Advertisement
KeiroKamioka

Tree

Mar 5th, 2021
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  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.         node* current = this;
  48.         int total = 0;
  49.  
  50.         while (current != NULL) {
  51.             if (current->right != NULL) {
  52.                 total = total + right->value;
  53.                
  54.             }
  55.             if (current->left != NULL) {
  56.                 total = total + left->value;
  57.              
  58.             }
  59.            
  60.         }
  61.         total = total + current->value;
  62.  
  63.         return total;
  64.  
  65.  
  66.         // Your code ends here
  67.     }
  68.  
  69.     //Prints all the leaves in the tree (i.e. nodes that have no children)
  70.     void print_leaves()
  71.     {
  72.         // Your code starts here
  73.         node* current = this;
  74.         if (!current->left && !current->right) {
  75.             cout << current->value << " ";
  76.         }
  77.         // Your code ends here
  78.     }
  79.  
  80.     //Finds the largest number in the tree
  81.     int max()
  82.     {
  83.         // Your code starts here
  84.         node* current = this;
  85.         int max = 0;
  86.         if (!current->left && !current->right) {
  87.             if (max < current->value) {
  88.                 max = current->value;
  89.             }
  90.         }
  91.         return max;
  92.         // Your code ends here
  93.     }
  94. };
  95. //After
  96. int main() {
  97.     node* root = new node(1, NULL, NULL);
  98.     root->add(5);
  99.     root->add(10);
  100.     root->add(11);
  101.     root->add(100);
  102.     root->add(11);
  103.  
  104.     cout << root->sum();
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement