Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.22 KB | None | 0 0
  1. #include <iostream>
  2. #include<cctype>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10.     int key; // @AVR
  11.     int desc; //@AVR
  12.     int element;
  13.     node *left;
  14.     node *right;
  15.     int height;
  16. };
  17. typedef struct node *nodeptr;
  18. class bstree
  19. {
  20.     public:
  21.         void insert(int,nodeptr &);
  22.         void del(int, nodeptr &);
  23.         int deletemin(nodeptr &);
  24.         int countmax(int, nodeptr &);  //@ AVR
  25.         void find(int,nodeptr &);
  26.         nodeptr findmin(nodeptr);
  27.         //nodeptr findmax(nodeptr);
  28.         void makeempty(nodeptr &);
  29.         void copy(nodeptr &,nodeptr &);
  30.         nodeptr nodecopy(nodeptr &);
  31.         //void preorder(nodeptr);
  32.         void inorder(nodeptr);
  33.     //void postorder(nodeptr);
  34.         int bsheight(nodeptr);
  35.         nodeptr srl(nodeptr &);
  36.         nodeptr drl(nodeptr &);
  37.         nodeptr srr(nodeptr &);
  38.         nodeptr drr(nodeptr &);
  39.         int max(int,int);
  40.         int nonodes(nodeptr);
  41. };
  42. // Inserting a node
  43. void bstree::insert(int x,nodeptr &p)
  44. {
  45.     if (p == NULL)
  46.     {
  47.         p = new node;
  48.         p->element = x;
  49.         p->left=NULL;
  50.         p->right = NULL;
  51.         p->height=0;
  52.         if (p==NULL)
  53.         {
  54.             cout<<"S-a terminat spatiul\n"<<endl;
  55.         }
  56.     }
  57.     else
  58.     {
  59.         if (x<p->element)
  60.         {
  61.             insert(x,p->left);
  62.             if ((bsheight(p->left) - bsheight(p->right))==2)
  63.             {
  64.                 if (x < p->left->element)
  65.                 {
  66.                     p=srl(p);
  67.                 }
  68.                 else
  69.                 {
  70.                     p = drl(p);
  71.                 }
  72.             }
  73.         }
  74.         else if (x>p->element)
  75.         {
  76.             insert(x,p->right);
  77.             if ((bsheight(p->right) - bsheight(p->left))==2)
  78.             {
  79.                 if (x > p->right->element)
  80.                 {
  81.                     p=srr(p);
  82.                 }
  83.                 else
  84.                 {
  85.                     p = drr(p);
  86.                 }
  87.             }
  88.         }
  89.         else
  90.         {
  91.             cout<<"Elementul exista\n"<<endl;
  92.         }
  93. }
  94. int m,n,d;
  95. m=bsheight(p->left);
  96. n=bsheight(p->right);
  97. d=max(m,n);
  98. p->height = d + 1;
  99. }
  100. // Finding the Smallest
  101. nodeptr bstree::findmin(nodeptr p)
  102. {
  103.     if (p==NULL)
  104.     {
  105.         cout<<"Arborele este gol\n"<<endl;
  106.         return p;
  107.     }
  108.     else
  109.     {
  110.         while(p->left !=NULL)
  111.         {
  112.             p=p->left;
  113.             //return p;
  114.         }
  115.         return p;
  116.     }
  117. }
  118.  
  119. /*
  120. // Finding the Largest node
  121. nodeptr bstree::findmax(nodeptr p)
  122. {
  123.     if (p==NULL)
  124.     {
  125.         cout<<"Arborele este gol\n"<<endl;
  126.         return p;
  127.     }
  128.     else
  129.     {
  130.         while(p->right !=NULL)
  131.         {
  132.             p=p->right;
  133.             //return p;
  134.         }
  135.         return p;
  136.     }
  137. }
  138.  
  139.  
  140. // Finding an element
  141. void bstree::find(int x,nodeptr &p)
  142. {
  143.     if (p==NULL)
  144.     {
  145.         cout<<"Elementul nu a fost gasit!\n"<<endl;
  146.     }
  147.     else
  148.     {
  149.         if (x < p->element)
  150.         {
  151.             find(x,p->left);
  152.         }
  153.         else
  154.         {
  155.             if (x>p->element)
  156.             {
  157.                 find(x,p->right);
  158.             }
  159.             else
  160.             {
  161.                 cout<<"Elementul nu a fost gasit!\n"<<endl;
  162.             }
  163.         }
  164.     }
  165. }
  166. // Copy a tree
  167. void bstree::copy(nodeptr &p,nodeptr &p1)
  168. {
  169.     makeempty(p1);
  170.     p1 = nodecopy(p);
  171. }
  172. // Make a tree empty
  173. void bstree::makeempty(nodeptr &p)
  174. {
  175.     nodeptr d;
  176.     if (p != NULL)
  177.     {
  178.         makeempty(p->left);
  179.         makeempty(p->right);
  180.         d=p;
  181.         free(d);
  182.         p=NULL;
  183.     }
  184. }
  185. // Copy the nodes
  186. nodeptr bstree::nodecopy(nodeptr &p)
  187. {
  188.     nodeptr temp;
  189.     if (p==NULL)
  190.     {
  191.         return p;
  192.     }
  193.     else
  194.     {
  195.         temp = new node;
  196.         temp->element = p->element;
  197.         temp->left = nodecopy(p->left);
  198.         temp->right = nodecopy(p->right);
  199.         return temp;
  200.     }
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. /*
  210.  
  211.  
  212. // Returns count of
  213. //int CountGreater(struct Node* root, int x)
  214.  bstree::countmax(int x,nodeptr &p)                              //x=x ; Node* = nodeptr, root = &p
  215. {
  216.     int res = 0;
  217.  
  218.         nodeptr d;
  219.     // Search for x. While searching, keep
  220.     // updating res if x is greater than
  221.     // current node.
  222.     while (p != NULL) {
  223.  
  224.         int desc = (p->right != NULL) ?
  225.                    p->right->desc : -1;
  226.  
  227.         if (p->key > x) {
  228.             res = res + desc + 1 + 1;
  229.             p = p->left;
  230.         } else if (p->key < x)
  231.             p = p->right;
  232.         else {
  233.             res = res + desc + 1;
  234.             break;
  235.         }
  236.     }
  237.     return res;
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. */
  247.  
  248.  
  249.  
  250.  
  251.  
  252. // Deleting a node
  253. void bstree::del(int x,nodeptr &p)
  254. {
  255.     nodeptr d;
  256.     if (p==NULL)
  257.     {
  258.         cout<<"Elementul nu a fost gasit!\n"<<endl;
  259.     }
  260.     else if ( x < p->element)
  261.     {
  262.         del(x,p->left);
  263.     }
  264.     else if (x > p->element)
  265.     {
  266.         del(x,p->right);
  267.     }
  268.     else if ((p->left == NULL) && (p->right == NULL))
  269.     {
  270.         d=p;
  271.         free(d);
  272.         p=NULL;
  273.         cout<<"Elementul a fost sters\n"<<endl;
  274.     }
  275.     else if (p->left == NULL)
  276.     {
  277.         d=p;
  278.         free(d);
  279.         p=p->right;
  280.         cout<<"Elementul a fost sters\n"<<endl;
  281.     }
  282.     else if (p->right == NULL)
  283.     {
  284.         d=p;
  285.         p=p->left;
  286.         free(d);
  287.         cout<<"Elementul a fost sters\n"<<endl;
  288.     }
  289.     else
  290.     {
  291.         p->element = deletemin(p->right);
  292.     }
  293. }
  294.  
  295. int bstree::deletemin(nodeptr &p)
  296. {
  297.     int c;
  298.     cout<<"interior stergemin\n"<<endl;
  299.     if (p->left == NULL)
  300.     {
  301.         c=p->element;
  302.         p=p->right;
  303.         return c;
  304.     }
  305.     else
  306.     {
  307.         c=deletemin(p->left);
  308.         return c;
  309.     }
  310. }
  311.  
  312. /*
  313.  
  314. void bstree::preorder(nodeptr p)
  315. {
  316.     if (p!=NULL)
  317.     {
  318.         cout<<p->element<<"\t";
  319.         preorder(p->left);
  320.         preorder(p->right);
  321.     }
  322. }
  323.  
  324. */
  325.  
  326.  
  327.  
  328. // Inorder Printing
  329. void bstree::inorder(nodeptr p)
  330. {
  331.     if (p!=NULL)
  332.     {
  333.         inorder(p->left);
  334.         cout<<p->element<<"\t";
  335.         inorder(p->right);
  336.     }
  337. }
  338.  
  339.  
  340.  
  341.  
  342. /*
  343. // PostOrder Printing
  344. void bstree::postorder(nodeptr p)
  345. {
  346.     if (p!=NULL)
  347.     {
  348.         postorder(p->left);
  349.         postorder(p->right);
  350.         cout<<p->element<<"\t";
  351.     }
  352. }
  353.  
  354. */
  355.  
  356. int bstree::max(int value1, int value2)
  357. {
  358.     return ((value1 > value2) ? value1 : value2);
  359. }
  360. int bstree::bsheight(nodeptr p)
  361. {
  362.     int t;
  363.     if (p == NULL)
  364.     {
  365.         return -1;
  366.     }
  367.     else
  368.     {
  369.         t = p->height;
  370.         return t;
  371.     }
  372. }
  373.  
  374.  
  375. nodeptr bstree:: srl(nodeptr &p1)
  376. {
  377.     nodeptr p2;
  378.     p2 = p1->left;
  379.     p1->left = p2->right;
  380.     p2->right = p1;
  381.     p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  382.     p2->height = max(bsheight(p2->left),p1->height) + 1;
  383.     return p2;
  384. }
  385. nodeptr bstree:: srr(nodeptr &p1)
  386. {
  387.     nodeptr p2;
  388.     p2 = p1->right;
  389.     p1->right = p2->left;
  390.     p2->left = p1;
  391.     p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  392.     p2->height = max(p1->height,bsheight(p2->right)) + 1;
  393.     return p2;
  394. }
  395. nodeptr bstree:: drl(nodeptr &p1)
  396. {
  397.     p1->left=srr(p1->left);
  398.     return srl(p1);
  399. }
  400. nodeptr bstree::drr(nodeptr &p1)
  401. {
  402.     p1->right = srl(p1->right);
  403.     return srr(p1);
  404. }
  405.  
  406. int bstree::nonodes(nodeptr p)
  407. {
  408.     int count=0;
  409.     if (p!=NULL)
  410.     {
  411.         nonodes(p->left);
  412.         nonodes(p->right);
  413.         count++;
  414.     }
  415.     return count;
  416. }
  417.  
  418. int main()
  419. {
  420.     //clrscr();
  421.     nodeptr root,nr,root1,min,max;//,flag;
  422.     int a,choice,findele,delele;
  423.     char ch='y';
  424.     bstree bst;
  425.  
  426.     //system("clear");
  427.     root = NULL;
  428.     root1=NULL;
  429.     cout<<"\n\t\t\t\t     ARBORE AVL"<<endl;
  430.     cout<<"\t\t\t\t:::::::::::::::::::\n"<<endl;
  431.  
  432.     do
  433.     {
  434.         cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::"<<endl;
  435.         cout<<"\t\t::::Apasa 1 pentru a insera nod:::::::::::::::::"<<endl;
  436.         cout<<"\t\t::::Apasa 2 pentru valoare min::::::::::::::::::"<<endl;
  437.         //cout<<"\t\t::::Apasa 3 pentru valoare max::::::::::::::::::"<<endl;
  438.         //cout<<"\t\t::::Apasa 4 pentru a cauta::::::::::::::::::::::"<<endl;
  439.         cout<<"\t\t::::Apasa 5 pentru a sterge:::::::::::::::::::::"<<endl;
  440.         //cout<<"\t\t::::Apasa 6 pentru afisare Preorder:::::::::::::"<<endl;
  441.         cout<<"\t\t::::Apasa 7 pentru afisare ordonata:::::::::::::"<<endl;
  442.         //cout<<"\t\t::::Apasa 8 pentru afisare Postorder::::::::::::"<<endl;
  443.         //cout<<"\t\t::::Apasa 9 pentru inaltimea arborelui::::::::::"<<endl;
  444.         //cout<<"\t\t::::Apasa 10 pentru nr nod mai mari:::::::::::::"<<endl;
  445.         cout<<"\t\t::::Apasa 0 pentru a iesi:::::::::::::::::::::::"<<endl;
  446.         cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::\n"<<endl;
  447.  
  448.         cout<<"\nIntrodu alegerea: ";
  449.         cin>>choice;
  450.  
  451.         switch(choice)
  452.         {
  453.             case 1:
  454.                 cout<<"\n\t\tINSERARE NOD"<<endl;
  455.                 cout<<"\t\t:::::::::::::\n"<<endl;
  456.                 cout<<"Introdu nod nou: ";
  457.                 cin>>a;
  458.                 bst.insert(a,root);
  459.                 cout<<"\nNodul a fost introdus cu succes!\n"<<endl;
  460.                 break;
  461.             case 2:
  462.                 if (root !=NULL)
  463.                 {
  464.                     min=bst.findmin(root);
  465.                     cout<<"\nNodul minim este: "<<min->element<<endl;
  466.                 }
  467.                 break;
  468.  
  469.                 /*
  470.             case 3:
  471.                 if (root !=NULL)
  472.                 {
  473.                     max=bst.findmax(root);
  474.                     cout<<"\nNodul maxim este: "<<max->element<<endl;
  475.                 }
  476.                 break;
  477.         */
  478.  
  479.         /*
  480.             case 4:
  481.                 cout<<"\nIntrodu nodul cautat: ";
  482.                 cin>>findele;
  483.                 if (root != NULL)
  484.                 {
  485.                     bst.find(findele,root);
  486.                 }
  487.                 break;
  488.                 */
  489.  
  490.             case 5:
  491.                 cout<<"\nIntrodu nodul pentru stergere: ";
  492.                 cin>>delele;
  493.                 bst.del(delele,root);
  494.                 bst.inorder(root);
  495.                 cout<<endl;
  496.                 break;
  497.     /*
  498.  
  499.             case 6:
  500.                 cout<<"\n\t\tPRE-ORDER TRAVERSAL"<<endl;
  501.                 bst.preorder(root);
  502.                 cout<<endl;
  503.         */      break;
  504.             case 7:
  505.                 cout<<"\n\t\tAFISARE IN ORDINE"<<endl;
  506.                 bst.inorder(root);
  507.                 cout<<endl;
  508.                 break;
  509.  
  510.             /*
  511.             case 8:
  512.                 cout<<"\n\t\tPOST ORDER TRAVERSAL"<<endl;
  513.                 bst.postorder(root);
  514.                 cout<<endl;
  515.                 break;
  516.             case 9:
  517.                 cout<<"\n\t\tINALTIMEA\n"<<endl;
  518.                 cout<<"Inaltimea arborelui este: "<<bst.bsheight(root)<<endl;
  519.  
  520.                 break;
  521.  
  522.                 */
  523.  
  524.         /*
  525.             case 10:
  526.                 if (root !=NULL) {
  527.                 cout<<"\n\t\tNODURI MAI MARI\n"<<endl;
  528.                  nr=bst.countmax(root);
  529.                 cout<<"Nr de noduri mai mari este: "<<max->element<<endl; // @ AVR
  530.                 }
  531.                 break;
  532.             case 0:
  533.                 cout<<"\n\tO zi buna!\n"<<endl;
  534.                 break;
  535.             default:
  536.                 cout<<"Input gresit!\n"<<endl;
  537.                 break;
  538.  
  539.             */
  540.         }
  541.         system("pause");
  542.         system("cls");
  543.     }while(choice != 0);
  544.     return 0;
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement