Advertisement
Guest User

BSTDrzewo

a guest
May 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. // BST.cpp : Defines the entry point for the console application.
  2.  
  3. // Drzewo BST
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14. struct node{
  15.     node *left;
  16.     node *right;
  17.     int key;
  18. };
  19.  
  20. void loadFile(node *& root);
  21.  
  22.  
  23. void printBT(string sp, string sn, node * root);
  24. node * minBST(node * root);
  25. node * maxBST(node * root);
  26. node * findBST(node * root, int value);
  27. void inorder(node * root);
  28. void saveFile(node *root);
  29. void addNode(node *& root, int value);
  30.  
  31. int _tmain(int argc, _TCHAR* argv[])
  32. {
  33.    
  34.     node *root=new node;
  35.     root=NULL;
  36.     node *temp=new node;
  37.     temp=NULL;
  38.     int option, value;
  39.     do{
  40.         cout << "1. Add NOde" << endl;
  41.         cout << "2. Load File" <<endl;
  42.         cout << "3. Store File" <<endl;
  43.         cout << "4. Minimum" <<endl;
  44.         cout << "5. Maximum" <<endl;
  45.         cout << "6. Find value" <<endl;
  46.         cout << "7. InOrder "<<endl;
  47.         cout << "10. Print Tree" <<endl;
  48.         cout <<"\nPodaj opcje: ";
  49.         cin >>option;
  50.  
  51.  
  52.         switch (option){
  53.             case 1:
  54.                 cout <<"Podaj liczbe: "<<endl;
  55.                 cin >>value;
  56.                 addNode(root,value);
  57.                 break;
  58.             case 2:
  59.                 loadFile(root);
  60.                 break;
  61.             case 3:
  62.                 saveFile(root);
  63.                 break;
  64.             case 4:
  65.                 cout <<"Minimalny element to"<< minBST(root)->key<<endl;
  66.                 break;
  67.             case 5:
  68.                 cout <<"Maksymalny element to"<< maxBST(root)->key<<endl;
  69.                 break;
  70.             case 6:
  71.                 cout <<"Podaj liczbe: "<<endl;
  72.                 cin >>value;
  73.                 temp= findBST(root,value);
  74.                 if( temp!=NULL){
  75.                 cout <<"Znaleziony element  "<<temp->key<<" pod adresem "<<temp<<endl;
  76.                 }else cout <<"Nie ma takiego elementu";
  77.                 break;
  78.             case 7:
  79.                 inorder(root);
  80.                 break;
  81.             //case 10:
  82.             //  printBT(" "," " , root);
  83.             //  break;
  84.  
  85.         }
  86.  
  87.         cout<<"\n\n";
  88.  
  89.     }while(option!=0);
  90.  
  91.  
  92.     return 0;
  93. }
  94.  
  95. void addNode(node *& root, int value){
  96.     if(root==NULL){
  97.         root=new node;
  98.         root->left=NULL;
  99.         root->right=NULL;
  100.         root->key=value;
  101.     }
  102.     else{
  103.         if(value<root->key) addNode(root->left,value);
  104.         if (value>=root->key)addNode(root->right,value);
  105.     }
  106. }
  107.  
  108. void loadFile(node *&root) {
  109.     int idx;
  110.     fstream plik;
  111.     plik.open("BSTdata.txt", ios::in);
  112.     if (plik){
  113.         while(!plik.eof()){
  114.             plik >>idx;
  115.             addNode(root,idx);
  116.         }
  117.         cout <<"Plik wczytany"<<endl;
  118.     }
  119.     plik.close();
  120. }
  121.  
  122. void saveFile(node *root){
  123.     ofstream plik;
  124.     plik.open("result.txt",ios::out|ios::app);
  125.  
  126.     if(root->left)
  127.         saveFile(root->left);
  128.  
  129.     plik << root->key <<" ";
  130.    
  131.     if(root->right)
  132.         saveFile(root->right);
  133.  
  134.     plik.close();
  135. }
  136.  
  137. node * minBST(node * root){
  138.   while(root->left){
  139.       root = root->left;
  140.   }
  141.   return root;
  142. }
  143.  
  144. node * maxBST(node * root){
  145.   while(root->right){
  146.       root = root->right;
  147.   }
  148.   return root;
  149. }
  150.  
  151. node * findBST(node * root, int value)
  152. {
  153.   while (root) {
  154.         if (value == root->key)
  155.             return root;
  156.         else if (value < root->key)
  157.             root = root->left;
  158.         else if (value > root->key)
  159.             root = root->right;
  160.     }
  161.     return NULL;
  162.   return root;
  163. }
  164.  
  165. void printBT(string sp, string sn, node * root)
  166. {
  167.   string s;
  168.   string cr,cl,cp;
  169.   cr = cl = cp = "  ";
  170.   cr[0] = 218; cr[1] = 196;
  171.   cl[0] = 192; cl[1] = 196;
  172.   cp[0] = 179;
  173.  
  174.   if(root)
  175.   {
  176.     s = sp;
  177.     if(sn == cr) s[s.length() - 2] = ' ';
  178.     printBT(s + cp, cr, root->right);
  179.  
  180.     s = s.substr(0,sp.length()-2);
  181.     cout << s << sn << root->key << endl;
  182.  
  183.     s = sp;
  184.     if(sn == cl) s[s.length() - 2] = ' ';
  185.     printBT(s + cp, cl, root->left);
  186.   }
  187. }
  188.  
  189. void inorder(node * root) {
  190.     if (root->left)
  191.         inorder(root->left);
  192.     cout << root->key << " ";
  193.     if (root->right)
  194.         inorder(root->right);
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement