Advertisement
KeiroKamioka

Nazo

Mar 7th, 2021
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 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.        
  48.         //node* current = this;
  49.        
  50.         int total = 0;
  51.         int leftSum = 0;
  52.         int rightSum = 0;
  53.  
  54.         if (this->left != nullptr) {
  55.             total = total + left->sum();
  56.         }
  57.         if (this->right != nullptr) {
  58.             total = total + right->sum();
  59.         }
  60.  
  61.         return total + this->value;
  62.  
  63.         //if (this == NULL) {
  64.         //    return 0;
  65.         //}
  66.         //if (this->left == nullptr && this->right == nullptr) {
  67.         //    return this->value;
  68.         //}
  69.         //else {
  70.         //    if (this->left != nullptr) {
  71.         //        right->sum();
  72.         //    }
  73.         //    else {
  74.         //        left->sum();
  75.         //    }
  76.         //}
  77.        
  78.         //if (this == NULL) {
  79.         //    return 0;
  80.        // }
  81.  
  82.         //if (left != nullptr) {
  83.         //    int leftSum;
  84.         //    leftSum = left->value;
  85.         //}
  86.  
  87.         //if (right != nullptr) {
  88.         //    rightSum = right->value;
  89.         //}
  90.         //total = this->value + leftSum + rightSum;
  91.  
  92.         //return total;
  93.  
  94.  
  95.         //if (left != nullptr) left->sum();
  96.         //total = this->value + total;
  97.         //if (right != nullptr) right->sum();
  98.  
  99.         //return total;
  100.  
  101.         // Your code ends here
  102.        
  103.  
  104.  
  105.         //if (left != nullptr) {
  106.         //    current->left;
  107.         //    leftSum = leftSum + left->value;
  108.         //    left->sum();
  109.         //}
  110.         //if (right != nullptr) {
  111.         //    current->right;
  112.         //    rightSum = rightSum + right->value;
  113.         //    right->sum();
  114.         //}
  115.  
  116.         //return total + leftSum + rightSum + current->value;
  117.  
  118.  
  119.         //int leftSum = left->sum();
  120.         //int rightSum = right->sum();
  121.  
  122.         //if (left == nullptr && right == nullptr) {
  123.         //    return total + current->value;
  124.         //}
  125.         //if (left == nullptr) {
  126.         //current->right;
  127.         //}
  128.         //if (right == nullptr ) {
  129.         //    current->left;
  130.         //}
  131.         //return total + right->value + left->value + current->value;
  132.  
  133.  
  134.         //if (left != nullptr) left->sum();
  135.         //else {}
  136.         //total = total + leftSum;
  137.         //current->left;
  138.  
  139.         //if (right != nullptr )right->sum();
  140.         //else {}
  141.         //total = total + rightSum;
  142.         //current->right;
  143.  
  144.         //return total + current->value;
  145.  
  146.    
  147.  
  148.    
  149.  
  150.  
  151.         // Your code ends here
  152.     }
  153.  
  154.     //Prints all the leaves in the tree (i.e. nodes that have no children)
  155.     void print_leaves()
  156.     {
  157.         // Your code starts here
  158.         node* current = this;
  159.         if (!current->left && !current->right) {
  160.             cout << current->value << " ";
  161.         }
  162.         // Your code ends here
  163.     }
  164.  
  165.     //Finds the largest number in the tree
  166.     int max()
  167.     {
  168.         // Your code starts here
  169.         node* current = this;
  170.         int max = 0;
  171.         if (!current->left && !current->right) {
  172.             if (max < current->value) {
  173.                 max = current->value;
  174.             }
  175.         }
  176.         return max;
  177.         // Your code ends here
  178.     }
  179. };
  180. //After
  181. int main() {
  182.     node* root = new node(1, NULL, NULL);
  183.     root->add(5);
  184.     root->add(10);
  185.     root->add(11);
  186.     root->add(100);
  187.     root->add(11);
  188.  
  189.     cout << root->sum();
  190.  
  191.    
  192.  
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement