Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     Node* parent;
  8.     Node* left;
  9.     Node* right;
  10.     int value;
  11. };
  12.  
  13. class BST
  14. {
  15.     Node* root;
  16.  
  17. public:
  18.     BST() { root = nullptr; };
  19.     ~BST() { delete root; };
  20.     void push(int a);
  21.     void place(int a, Node* n);
  22.     //void pop();
  23.     //void print();
  24.     void search();
  25.     void preorder(Node* n);
  26.     void inorder(Node* n);
  27.     void postorder(Node* n);
  28.     void check(Node* n);
  29. };
  30.  
  31. int main()
  32. {
  33.     BST b;
  34.  
  35.     b.push(5);
  36.     b.push(9);
  37.     b.push(2);
  38.     b.push(4);
  39.     b.push(1);
  40.     b.push(7);
  41.    
  42.     b.search();
  43.     return 0;
  44. }
  45.  
  46.  
  47.  
  48. void BST::push(int a)
  49. {
  50.     Node* new_node = new Node;
  51.     new_node->value = a;
  52.  
  53.     if (!root)
  54.     {
  55.         new_node->parent = nullptr;
  56.         root = new_node;
  57.         new_node->left = nullptr;
  58.         new_node->right = nullptr;
  59.     }
  60.     else
  61.     {
  62.         place(a, root);
  63.     }
  64. }
  65.  
  66. void BST::place(int a, Node* n)
  67. {
  68.     if (a < n->value)
  69.     {
  70.         if (n->left)
  71.             place(a, n->left);
  72.         else
  73.         {
  74.             Node* new_node = new Node;
  75.             new_node->value = a;
  76.             n->left = new_node;
  77.             new_node->left = nullptr;
  78.             new_node->right = nullptr;
  79.         }
  80.     }
  81.     else
  82.     {
  83.         if (n->right)
  84.             place(a, n->right);
  85.         else
  86.         {
  87.             Node* new_node = new Node;
  88.             new_node->value = a;
  89.             n->right = new_node;
  90.             new_node->left = nullptr;
  91.             new_node->right = nullptr;
  92.         }
  93.     }
  94. }
  95.  
  96. void BST::search()
  97. {
  98.     //cout << "pre-order: " << endl;
  99.     //preorder(root);
  100.     cout << "in-order: " << endl;
  101.     inorder(root);
  102.     //cout << "post-order: " << endl;
  103.     //postorder(root);
  104.  
  105.     //cout << "check: " << endl;
  106.     //check(root);
  107.  
  108. }
  109.  
  110. void BST::preorder(Node* n)
  111. {
  112.     cout << n->value << endl;
  113.     if (n->left)
  114.         preorder(n->left);
  115.     if (n->right)
  116.         preorder(n->right);
  117. }
  118.  
  119. void BST::inorder(Node * n)
  120. {
  121.     if (n->left)
  122.         preorder(n->left);
  123.     cout << n->value << endl;
  124.     if (n->right)
  125.         preorder(n->right);
  126. }
  127.  
  128. void BST::postorder(Node * n)
  129. {
  130.     if (n->left)
  131.         preorder(n->left);
  132.     if (n->right)
  133.         preorder(n->right);
  134.     cout << n->value << endl;
  135. }
  136.  
  137. void BST::check(Node* n)
  138. {
  139.     cout << "parent: " << n->parent << endl;
  140.     cout << "value: " << n->value << endl;
  141.     cout << "node: " << n << endl;
  142.     cout << "l: " << n->left << endl;
  143.     cout << "r: " << n->right << endl;
  144.     cout << "---------------------\n\n";
  145.     if (n->left)
  146.         check(n->left);
  147.     if (n->right)
  148.         check(n->right);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement