KebabJoy

Binary Tree

Jan 13th, 2022 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Node{
  5.     Node *lhs;
  6.     Node *rhs;
  7.     int val;
  8.  
  9.     Node(int v) : val(v){
  10.         rhs = nullptr;
  11.         lhs = nullptr;
  12.     }
  13. };
  14.  
  15. class BinaryTree{
  16. public:
  17.     BinaryTree();
  18.     BinaryTree(std::vector<bool> v);
  19.     void push(int val);
  20.     bool empty();
  21.     int top() const;
  22.     size_t Size() const;
  23.     void print(Node* p);
  24.     void printFromHead();
  25.     BinaryTree& operator=(const BinaryTree& rhs){
  26.         Node* tmp = rhs.head;
  27.         return copy(tmp);
  28.     }
  29.  
  30. private:
  31.     size_t size;
  32.     Node *head;
  33.     BinaryTree& copy(Node* rhs);
  34. };
  35.  
  36. BinaryTree& BinaryTree::copy(Node* rhs){
  37.     if(rhs == nullptr){
  38.         return *this;
  39.     }
  40.  
  41.     copy(rhs->rhs);
  42.     copy(rhs->lhs);
  43.     push(rhs->val);
  44. }
  45.  
  46. BinaryTree::BinaryTree() {
  47.     head = nullptr;
  48.     size = 0;
  49. }
  50.  
  51. BinaryTree::BinaryTree(std::vector<bool> v){
  52.     for (int i = 0; i < v.size(); ++i) {
  53.         push(v[i]);
  54.     }
  55. }
  56.  
  57. void BinaryTree::print(Node* p){
  58.     std::cout << p->val << '\n';
  59.     if(p->lhs != nullptr){
  60.         print(p->lhs);
  61.     }
  62.     if(p->rhs != nullptr){
  63.         print(p->rhs);
  64.     }
  65. }
  66.  
  67. void BinaryTree::printFromHead() {
  68.     print(head);
  69. }
  70.  
  71. void BinaryTree::push(int val){
  72.     Node *tmp = new Node(val);
  73.     if(head != nullptr){
  74.         Node *last = head;
  75.         while(last->lhs != nullptr || last->rhs != nullptr){
  76.             if(last->rhs != nullptr){
  77.                 last = last->rhs;
  78.             }else if(last->lhs != nullptr){
  79.                 last = last->lhs;
  80.             }else{
  81.                 break;
  82.             }
  83.         }
  84.  
  85.         if(last->val >= val){
  86.             last->lhs = tmp;
  87.         }else{
  88.             last->rhs = tmp;
  89.         }
  90.     }else{
  91.         head = new Node(val);
  92.     }
  93.     size++;
  94. }
  95.  
  96. bool BinaryTree::empty(){
  97.     return head == nullptr;
  98. }
  99.  
  100. size_t BinaryTree::Size() const{
  101.     return size;
  102. }
  103.  
  104. int BinaryTree::top() const{
  105.     return  head->val;
  106. }
  107.  
  108. int main() {
  109.     std::cout << "First tree:\n";
  110.     BinaryTree *bt = new BinaryTree();
  111.     bt->push(12);
  112.     bt->push(10);
  113.     bt->push(111);
  114.     bt->push(150);
  115.     std::cout << bt->empty() << '\n';
  116.     std::cout << bt->Size() << '\n';
  117.     bt->printFromHead();
  118.     std::cout << "Second, bitmask tree:\n";
  119.  
  120.     std::vector<bool> v = {1,0,1,0};
  121.     BinaryTree bt2 = BinaryTree(v);
  122.     bt2.printFromHead();
  123.  
  124.     std::cout << "Third tree\n";
  125.     BinaryTree bt3 = bt2;
  126.     bt3.printFromHead();
  127. }
Add Comment
Please, Sign In to add comment