Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct BST {
  9.   string valuecal;
  10.   string valueczesc;
  11.   struct BST *left;
  12.   struct BST *right;
  13. } BST;
  14.  
  15. BST *root = NULL;
  16.  
  17.  
  18. int komparatorstringow(string jedencal,string jedenczesc,string dwacal,string dwaczesc){
  19.     if(jedencal.length()>dwacal.length()) return 1;
  20.     else if (dwacal.length()>jedencal.length()) return 2;
  21.     else if (jedencal.length()==dwacal.length()){
  22.         if(jedencal>dwacal) return 1;
  23.         else if(dwacal>jedencal) return 2;
  24.         else if(jedencal==dwacal){
  25.             if(jedenczesc>dwaczesc) return 1;
  26.             else if(dwaczesc>jedenczesc) return 2;
  27.             else return 0;
  28.         }
  29.     }
  30. }
  31.  
  32. void insert(string valcal,string valczesc){
  33.  
  34.     BST* v = new BST;
  35.     BST* rodzic;
  36.     v->left = NULL;
  37.     v->right = NULL;
  38.     v->valuecal = valcal;
  39.     v->valueczesc = valczesc;
  40.  
  41. if(root==NULL) root=v;
  42.   else
  43.   {
  44.     //Note: ALL insertions are as leaf nodes
  45.     BST* obecny;
  46.     obecny = root;
  47.     // Find the Node's parent
  48.  
  49.     while(obecny)
  50.     {
  51.         rodzic = obecny;
  52.         komparatorstringow(v->valuecal,v->valueczesc,obecny->valuecal,obecny->valueczesc);{
  53.         if(1) obecny = obecny->right;
  54.         else if(2) obecny = obecny->left;
  55.         else return;
  56.             }
  57.         }
  58.     }
  59.     komparatorstringow(v->valuecal,v->valueczesc,rodzic->valuecal,rodzic->valueczesc);{
  60.     if(1)rodzic->left = v;
  61.     else if(2)rodzic->right = v;
  62.     else return;
  63.     }
  64.   }
  65.  
  66. int napisyzplikuW(string temp){
  67.     int x=temp.find(',');
  68.     string a;
  69.     string b;
  70.     for(int i=0;i<x;i++){
  71.         a=temp[i];
  72.     }
  73.     for(int i=x+1; i<temp.length();i++){
  74.         b=temp[i];
  75.     }
  76.     insert(a,b);
  77. }
  78. void search(string dcal,string dczesc){
  79. bool found=false;
  80.     if(root==NULL)
  81.     {
  82.         cout<<" Drzewo jest puste! "<<endl;
  83.         return;
  84.     }
  85.     BST* obecny;
  86.     BST* rodzic;
  87.     obecny = root;
  88.     while(obecny != NULL)
  89.     {
  90.          if(obecny->valuecal == dcal && obecny->valueczesc == dczesc)
  91.          {
  92.             found = true;
  93.             break;
  94.          }
  95.          else
  96.          {
  97.              rodzic = obecny;
  98.              if(dcal > obecny->valuecal)
  99.              obecny = obecny->right;
  100.              else obecny = obecny->left;
  101.          }
  102.     }
  103.     if(!found)
  104.          {
  105.         cout<<" Nie znaleziono elementu! "<<endl;
  106.         system("pause");
  107.         }
  108.     else
  109.     {
  110.         cout<<" Znaleziono podany element!"<<endl;
  111.  
  112.     }
  113. }
  114. int napisyzplikuS(string temp){
  115.     int x=temp.find(',');
  116.     string a;
  117.     string b;
  118.     for(int i=0;i<x;i++){
  119.         a=temp[i];
  120.     }
  121.     for(int i=x+1; i<temp.length();i++){
  122.         b=temp[i];
  123.     }
  124.     search(a,b);
  125. }
  126.  
  127. void searchilecal(BST *v, string dcal){
  128.     int x=0;
  129.         if(v==NULL)
  130.         return ;
  131.     else if(v!=NULL)
  132.     {
  133.         if(v->left) searchilecal(v->left,dcal);
  134.         if(v->right) searchilecal(v->right,dcal);
  135.     }
  136.     cout<<x;
  137. }
  138.  
  139.  
  140. void remove(string dcal,string dczesc)
  141. {
  142.     {
  143.     bool found=false;
  144.     if(root==NULL)
  145.     {
  146.         cout<<" Drzewo jest puste, nie ma co usuwac! "<<endl;
  147.         return;
  148.     }
  149.     BST* obecny;
  150.     BST* rodzic;
  151.     obecny = root;
  152.     while(obecny != NULL)
  153.     {
  154.          if(obecny->valuecal == dcal && obecny->valueczesc == dczesc)
  155.          {
  156.             found = true;
  157.             break;
  158.          }
  159.          else
  160.          {
  161.              rodzic = obecny;
  162.              if(dcal >obecny->valuecal)
  163.              obecny = obecny->right;
  164.              else if(dcal<obecny->valuecal)
  165.              obecny = obecny->left;
  166.              else if(dcal==obecny->valuecal)
  167.                 if(dczesc>obecny->valueczesc)
  168.                 obecny=obecny->right;
  169.                 else
  170.                 obecny = obecny->left;
  171.          }
  172.     }
  173.     if(!found)
  174.          {
  175.         cout<<" Nie znaleziono elementu! "<<endl;
  176.         return;
  177.         }
  178.     else
  179.     {
  180.         cout<<" Znaleziono podany element, usunieto!"<<endl;
  181.     }
  182.  
  183.          // 3 cases :
  184.     // 1. We're removing a leaf node
  185.     // 2. We're removing a node with a single child
  186.     // 3. we're removing a node with 2 children
  187.  
  188.     // Node with single child
  189.     if((obecny->left == NULL && obecny->right != NULL)|| (obecny->left != NULL&& obecny->right == NULL))
  190.     {
  191.        if(obecny->left == NULL && obecny->right != NULL)
  192.        {
  193.            if(rodzic->left == obecny)
  194.            {
  195.              rodzic->left = obecny->right;
  196.              delete obecny;
  197.            }
  198.            else
  199.            {
  200.              rodzic->right = obecny->right;
  201.              delete obecny;
  202.            }
  203.        }
  204.        else // left child present, no right child
  205.        {
  206.           if(rodzic->left == obecny)
  207.            {
  208.              rodzic->left = obecny->left;
  209.              delete obecny;
  210.            }
  211.            else
  212.            {
  213.              rodzic->right = obecny->left;
  214.              delete obecny;
  215.            }
  216.        }
  217.      return;
  218.     }
  219.  
  220.          //We're looking at a leaf node
  221.          if( obecny->left == NULL && obecny->right == NULL)
  222.     {
  223.         if(rodzic->left == obecny) rodzic->left = NULL;
  224.         else rodzic->right = NULL;
  225.                  delete obecny;
  226.                  return;
  227.     }
  228.  
  229.  
  230.     //Node with 2 children
  231.     // replace node with smallest value in right subtree
  232.     if (obecny->left != NULL && obecny->right != NULL)
  233.     {
  234.         BST* temp;
  235.         temp = obecny->right;
  236.         if((temp->left == NULL) && (temp->right == NULL))
  237.         {
  238.             obecny = temp;
  239.             delete temp;
  240.             obecny->right = NULL;
  241.         }
  242.         else // right child has children
  243.         {
  244.             //if the node's right child has a left child
  245.             // Move all the way down left to locate smallest element
  246.  
  247.             if((obecny->right)->left != NULL)
  248.             {
  249.                 BST *ltemp;
  250.                 BST *rtemp;
  251.                 ltemp = obecny->right;
  252.                 rtemp = (obecny->right)->left;
  253.                 while(ltemp->left != NULL)
  254.                 {
  255.                    rtemp = ltemp;
  256.                    ltemp = ltemp->left;
  257.                 }
  258.                 obecny->valuecal = ltemp->valuecal;
  259.                 obecny->valueczesc = ltemp->valueczesc;
  260.                 delete ltemp;
  261.                 rtemp->left = NULL;
  262.            }
  263.            else
  264.            {
  265.                BST* temp2;
  266.                temp2 = obecny->right;
  267.                obecny->valuecal = temp2->valuecal;
  268.                obecny->valueczesc = temp2->valueczesc;
  269.                obecny->right = temp2->right;
  270.                delete temp2;
  271.            }
  272.  
  273.         }
  274.          return;
  275.     }
  276. }
  277. }
  278. int napisyzplikuU(string temp){
  279.     int x=temp.find(',');
  280.     string a;
  281.     string b;
  282.     for(int i=0;i<x;i++){
  283.         a=temp[i];
  284.     }
  285.     for(int i=x+1; i<temp.length();i++){
  286.         b=temp[i];
  287.     }
  288.     remove(a,b);
  289. }
  290. void inorder(BST* v)
  291. {
  292.     if(v==NULL)
  293.         return ;
  294.     else if(v!=NULL)
  295.     {
  296.         if(v->left) inorder(v->left);
  297.         cout<<" "<<v->valuecal<<","<<v->valueczesc<<" ";
  298.         if(v->right) inorder(v->right);
  299.     }
  300.     else return;
  301. }
  302.  
  303. void wypiszpodanypoziom(struct BST *v, int level)
  304. {
  305.     if (v == NULL) return;
  306.     if (level == 1) cout << v->valuecal <<","<<v->valueczesc<<  " ";
  307.     else if (level > 1)
  308.     {
  309.         wypiszpodanypoziom(v->left, level - 1);
  310.         wypiszpodanypoziom(v->right, level - 1);
  311.     }
  312. }
  313. int wysokosc(struct BST* v)
  314. {
  315.     if (v == NULL) return 0;
  316.     else
  317.     {
  318.         int lwysokosc = wysokosc(v->left);
  319.         int rwysokosc = wysokosc(v->right);
  320.  
  321.         if (lwysokosc > rwysokosc)
  322.             return(lwysokosc + 1);
  323.         else return(rwysokosc + 1);
  324.     }
  325. }
  326. void printLevelOrder(struct BST* v)
  327. {
  328.     int h = wysokosc(v);;
  329.     int i;
  330.     for (i = 1; i <= h; i++)
  331.     {
  332.         for (int j = h; j > i; j--) cout << " ";
  333.         wypiszpodanypoziom(v, i);
  334.         cout << endl << endl;
  335.     }
  336. }
  337.  
  338. int main(){
  339.  
  340.     /*int l_polecen;
  341.     char polecenie;
  342.     string liczba;
  343.     fstream file;
  344.     file.open("in.txt", ios::in);
  345.  
  346.     file>>l_polecen;
  347.  
  348.     for(int i=0; i<l_polecen;i++){
  349.         file>>polecenie;
  350.         if(polecenie=='W'){
  351.             file>>liczba;
  352.             cout<<polecenie<<" "<<liczba<<endl;
  353.             napisyzplikuW(liczba);
  354.         }
  355.         else if(polecenie=='U'){
  356.             file>>liczba;
  357.             cout<<polecenie<<" "<<liczba<<endl;
  358.             napisyzplikuU(liczba);
  359.         }
  360.         else if (polecenie=='S'){
  361.             file>>liczba;
  362.             cout<<polecenie<<" "<<liczba<<endl;
  363.             napisyzplikuS(liczba);
  364.         }
  365.         /*else if(polecenie=='L'){
  366.             file>>liczba;
  367.             cout'polecenie'<<" "<<liczba<<endl;
  368.             //ile liczb posiada czesc calkowita rowna x
  369.             //file2<<wynik
  370.         }
  371.  
  372.     }*/
  373.     int wybor;
  374.     string temp,temp2;
  375.     while(1)
  376.     {
  377.        cout<<" Drzewo BST "<<endl;
  378.        cout<<" 1. Dodawanie"<<endl;
  379.        cout<<" 2. Usuwanie elementu "<<endl;
  380.        cout<<" 3. Wyswietlanie drzewa "<<endl;
  381.        cout<<" 4. Wyszukiwanie elementu"<<endl;
  382.        cout<<" 5. Wypisz drzewo In-order"<<endl;
  383.        cout<<" 7. Ile wystapien danej calkowitej"<<endl;
  384.        cout<<" 6. Wyjscie "<<endl;
  385.        cout<<" Wprowadz wybor : ";
  386.        cin>>wybor;
  387.        switch(wybor)
  388.        {
  389.            case 1 : system("cls");
  390.                     cout<<" Podaj wartosc przed przecinkiem jaka chcesz dodac do drzewa :"<<endl;
  391.                     cin>>temp;
  392.                     cout<<" Podaj wartosc po przecinku"<<endl;
  393.                     cin>>temp2;
  394.                     insert(temp,temp2);
  395.                     break;
  396.            case 2 : system("cls");
  397.                     cout<<endl;
  398.                     cout<<" Podaj czesc calkowita elementu, ktory chcesz usunac z drzewa: "<<endl;
  399.                     cin>>temp;
  400.                     cout<<" Oraz czesc po przecinku:"<<endl;
  401.                     cin>>temp2;
  402.                     remove(temp,temp2);
  403.                     break;
  404.            case 3 : system("cls");
  405.                     cout<<endl;
  406.                     printLevelOrder(root);
  407.                     cout<<endl<<endl;
  408.                     break;
  409.            case 4 : system("cls");
  410.                     cout<<endl;
  411.                     cout<<" Podaj element, ktorego wystepowanie chcesz sprawdzic "<<endl;
  412.                     cin>>temp;
  413.                     cout<<" Czesc po przecinka element:"<<endl;
  414.                     cin>>temp2;
  415.                     search(temp,temp2);
  416.                     break;
  417.            case 5 : system("cls");
  418.                     cout<<endl;
  419.                     inorder(root);
  420.                     cout<<endl<<endl;
  421.                     break;
  422.            case 6 : system("cls");
  423.                     system("pause");
  424.                     return 0;
  425.                     break;
  426.            case 7 : system("cls");
  427.                     cout<<endl;
  428.                     cout<<" Podaj element calkowity, ktorego liczbe wystapien chcesz policzyc"<<endl;
  429.                     cin>>temp;
  430.                     searchilecal(root,temp);
  431.                     cout<<endl<<endl;
  432.        }
  433.     }
  434.  
  435.  
  436.  
  437. return 0;
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement