Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "bst.h"
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. ostream& operator << (ostream& stream, BstNode *BST)
  8. {
  9.     if (BST)
  10.     {
  11.         cout << BST->left.get();
  12.         stream << to_string(BST->value) << " ";
  13.         cout << BST->right.get();
  14.     }
  15.     return stream;
  16. }
  17. unique_ptr<BstNode> NowyWezel(int value)
  18. {
  19.     unique_ptr<BstNode>wezel(new BstNode(value));
  20.     return wezel;
  21. }
  22. void addToTree(unique_ptr<BstNode>&bst, int value)
  23. {
  24.     if (bst == nullptr)
  25.     {
  26.         bst = NowyWezel(value);
  27.     }
  28.     else if(value >= bst->value)
  29.     {
  30.         addToTree(bst->right,value);
  31.     }
  32.     else if (value < bst->value)
  33.     {
  34.         addToTree(bst->left, value);
  35.     }
  36. }
  37.  
  38. unique_ptr<BstNode> &operator <<(unique_ptr<BstNode>&bst, int value)
  39. {
  40.     if (bst == nullptr)
  41.     {
  42.         bst = NowyWezel(value);
  43.     }
  44.     else if (value >= bst->value)
  45.     {
  46.         addToTree(bst->right, value);
  47.     }
  48.     else if (value < bst->value)
  49.     {
  50.         addToTree(bst->left, value);
  51.     }
  52.     return bst;
  53. }
  54. int minimum(BstNode*root)
  55. {  
  56.    
  57.     if (root)
  58.     {
  59.         while (root->left)
  60.         {
  61.             root = root->left.get();
  62.         }
  63.         cout << root->value << endl;
  64.         return(root->value);
  65.     }
  66.     cout << "Drzewo jest puste!" << endl;
  67.     return 0;
  68. }
  69. int maksimum(BstNode*root)
  70. {
  71.     if (root)
  72.     {
  73.         while (root->right)
  74.         {
  75.             root = root->right.get();
  76.         }
  77.         cout << root->value << endl;
  78.         return (root->value);
  79.     }
  80.     cout << "Drzewo jest puste!" << endl;
  81.     return 0;
  82. }
  83. bool contains(BstNode*root, int value)
  84. {
  85.     if (root == NULL) return false;
  86.     else if (root->value == value) return true;
  87.     else if (value <= root->value) return contains(root->left.get(), value);
  88.     else return contains(root->right.get(), value);
  89. }
  90. unsigned int size(BstNode* root)
  91. {
  92.     int zmienna = 1;
  93.     if (root == NULL) return 0;
  94.     else
  95.     {
  96.         zmienna= zmienna+size(root->left.get());
  97.         zmienna= zmienna+size(root->right.get());
  98.     }
  99.     return zmienna;;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement