Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "bst.h"
- #include <iostream>
- #include <string>
- using namespace std;
- ostream& operator << (ostream& stream, BstNode *BST)
- {
- if (BST)
- {
- cout << BST->left.get();
- stream << to_string(BST->value) << " ";
- cout << BST->right.get();
- }
- return stream;
- }
- unique_ptr<BstNode> NowyWezel(int value)
- {
- unique_ptr<BstNode>wezel(new BstNode(value));
- return wezel;
- }
- void addToTree(unique_ptr<BstNode>&bst, int value)
- {
- if (bst == nullptr)
- {
- bst = NowyWezel(value);
- }
- else if(value >= bst->value)
- {
- addToTree(bst->right,value);
- }
- else if (value < bst->value)
- {
- addToTree(bst->left, value);
- }
- }
- unique_ptr<BstNode> &operator <<(unique_ptr<BstNode>&bst, int value)
- {
- if (bst == nullptr)
- {
- bst = NowyWezel(value);
- }
- else if (value >= bst->value)
- {
- addToTree(bst->right, value);
- }
- else if (value < bst->value)
- {
- addToTree(bst->left, value);
- }
- return bst;
- }
- int minimum(BstNode*root)
- {
- if (root)
- {
- while (root->left)
- {
- root = root->left.get();
- }
- cout << root->value << endl;
- return(root->value);
- }
- cout << "Drzewo jest puste!" << endl;
- return 0;
- }
- int maksimum(BstNode*root)
- {
- if (root)
- {
- while (root->right)
- {
- root = root->right.get();
- }
- cout << root->value << endl;
- return (root->value);
- }
- cout << "Drzewo jest puste!" << endl;
- return 0;
- }
- bool contains(BstNode*root, int value)
- {
- if (root == NULL) return false;
- else if (root->value == value) return true;
- else if (value <= root->value) return contains(root->left.get(), value);
- else return contains(root->right.get(), value);
- }
- unsigned int size(BstNode* root)
- {
- int zmienna = 1;
- if (root == NULL) return 0;
- else
- {
- zmienna= zmienna+size(root->left.get());
- zmienna= zmienna+size(root->right.get());
- }
- return zmienna;;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement