mkrobot  [ Profile ]

Sort by: Date / Title /

  1. 1 month ago
    1. #include<iostream>
    2. using namespace std;
    3.  
    4. struct element{
    5.         int label;
    6.         bool koristen;
    7. };
    8.  
    9. struct BinaryTree{
    10.         element elementi[ 2000 ];
    11. };
    12.  
    13. void InitB( int x, BinaryTree* stablo ){
    14.         for( int i = 0; i < 2000; i++ )
    15.                         elementi[ i ].koristen = false;
    16.         stablo->elementi[ 1 ].koristen = true;
    17.         stablo->elementi[ 1 ].label = x;
    18. }
    19.  
    20. int RootB( BinaryTree* stablo ){
    21.         if( stablo->elementi[ 1 ].koristen )
    22.                 return 1;                                   
    23.         return 0;                                          
    24. }
    25.  
    26. int LabelB( int n, BinaryTree* stablo ){
    27.         return stablo->elementi[ n ].label;
    28. }
    29.  
    30. void ChangeLabelB( int x, int n, BinaryTree* stablo ){
    31.         stablo->elementi[ n ].label = x;
    32. }
    33.  
    34. void CreateLeftB( int x, int n, BinaryTree* stablo ){
    35.         if( stablo->elementi[ 2*n ].koristen )
    36.                 cout << "ERROR!" << endl;
    37.         else{
    38.                 stablo->elementi[ 2*n ].koristen = true;
    39.                 stablo->elementi[ 2*n ].label = x;
    40.         }
    41. }
    42.  
    43. void CreateRightB( int x, int n, BinaryTree* stablo ){
    44.         if( stablo->elementi[ 2*n+1 ].koristen )
    45.                 cout << "ERROR!" << endl;
    46.         else{
    47.                 stablo->elementi[ 2*n+1 ].koristen = true;
    48.                 stablo->elementi[ 2*n+1 ].label = x;
    49.         }
    50. }
    51.  
    52.  
    53.  
    54. int LeftChildB( int n, BinaryTree* stablo ){
    55.         if( stablo->elementi[ 2*n ].koristen ) /
    56.                 return (2*n );
    57.         else
    58.         return 0;
    59. }
    60.  
    61.  
    62. int RightChildB( int n, BinaryTree* stablo ){
    63.         if( stablo->elementi[ 2*n+1 ].koristen )
    64.                 return (2*n+1);
    65.         else
    66.         return 0;
    67. }
    68.  
    69.  
    70.  
    71. void DeleteB( int n, BinaryTree* stablo ){
    72.         if( n ){
    73.                 DeleteB( LeftChildB( n, stablo ), stablo );
    74.                 DeleteB( RightChildB( n, stablo ), stablo );
    75.                 stablo->elementi[ n ].koristen = false;
    76.         }
    77. }
    78.  
    79. bool ExistsLeftChildB( int n, BinaryTree* stablo ){
    80.         if(LeftChildB( n, stablo ) != 0)
    81.         return true;
    82.         else
    83.                 return false;
    84. }
    85.  
    86. bool ExistsRightChildB( int n, BinaryTree* stablo ){
    87.         if(RightChildB( n, stablo ) != 0)
    88.         return true;
    89.         else
    90.                 return false;
    91. }
    92.  
    93. int ParentB( int n, BinaryTree* stablo ){
    94.         if( n == 1 )
    95.                 return 0;
    96.         else
    97.                 return n/2;
    98. }
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2034"></script>
  2. 1 month ago
    1. #include<iostream>
    2. #include<cstdlib>
    3. using namespace std;
    4.  
    5. struct element{
    6.         int label;
    7.         element *lijevo,*desno;
    8. };
    9.  
    10. struct BinaryTree{
    11.         element *korijen;
    12. };
    13.  
    14. struct stack{
    15.         element * elementi[ 10000 ];
    16.         int top;
    17. };
    18.  
    19. void InitS( stack& S ){
    20.         S.top = 9999;
    21. }
    22.  
    23. bool IsEmptyS( stack& S ){
    24.         return S.top == 9999;
    25. }
    26.  
    27. element * TopS( stack& S ){
    28.         if( !IsEmptyS( S ) )
    29.                 return S.elementi[ S.top+1 ];
    30.         else
    31.                 return 0;
    32. }
    33.  
    34. void PushS( element * x, stack& S ){
    35.    if( S.top >= 0 )
    36.       S.elementi[ S.top-- ] = x;
    37.    else
    38.       exit( 1 );
    39. }
    40.  
    41. void PopS( stack& S ){
    42.    if( !IsEmptyS( S ) )
    43.       S.top++;
    44.    else
    45.       exit( 1 );
    46. }
    47.  
    48. void InitB( int x, BinaryTree* stablo ){
    49.         stablo->korijen = new element;
    50.         stablo->korijen->lijevo = stablo->korijen->desno = 0;
    51.         stablo->korijen->label = x;
    52. }
    53.  
    54. element * RootB( BinaryTree* stablo ){
    55.         if( stablo )
    56.                 return stablo->korijen;
    57.         return 0;
    58. }
    59.  
    60. int LabelB( element * n, BinaryTree* stablo ){
    61.         return n->label;
    62. }
    63.  
    64. void ChangeLabelB( int x, element * n, BinaryTree* stablo ){
    65.         n->label = x;
    66. }
    67.  
    68. element * LeftChildB( element * n, BinaryTree* stablo ){
    69.         if( n->lijevo )
    70.                 return n->lijevo;
    71.         return 0;
    72. }
    73.  
    74. element * RightChildB( element * n, BinaryTree* stablo ){
    75.         if( n->desno != 0 )
    76.                 return n->desno;
    77.         return 0;
    78. }
    79.  
    80. void CreateLeftB( int x, element * n, BinaryTree* stablo ){
    81.         if( n->lijevo != 0)
    82.                 cout << "Error!" << endl;
    83.         else{
    84.                 element * novi = new element; 
    85.                 novi->label = x;         
    86.                 novi->lijevo = novi->desno = 0;
    87.                 n->lijevo = novi;
    88.         }
    89. }
    90.  
    91. void CreateRightB( int x, element * n, BinaryTree* stablo ){
    92.         if( n->desno != 0)
    93.                 cout << "Error!" << endl;
    94.         else{
    95.                 element * novi = new element;
    96.                 novi->label = x;
    97.                 novi->lijevo = novi->desno = 0;
    98.                 n->desno = novi;
    99.         }
    100. }
    101.  
    102. element * ParentB( element * n, BinaryTree* stablo ){
    103.         element * tekuci;
    104.         bool nadjen=false;
    105.         stack S;
    106.         InitS(S);
    107.         PushS( stablo->korijen,S);
    108.         while((!IsEmptyS(S)) && (!nadjen)) {
    109.                 tekuci=TopS(S)
    110.                 PopS( S );                 
    111.                 if ((tekuci->lijevo!=n) && (tekuci->desno!=n)){
    112.                         if (tekuci->lijevo!=0) PushS(tekuci->lijevo,S);
    113.                         if (tekuci->desno!=0) PushS(tekuci->desno,S);
    114.                 }
    115.                 else
    116.                         nadjen = true;
    117.         }
    118.         if( nadjen )
    119.                 return tekuci;
    120.         else
    121.                 return 0;
    122. }
    123.  
    124. void DeleteElement( element * n, BinaryTree* stablo ){
    125.         if( n->lijevo ) DeleteElement( n->lijevo, stablo );
    126.         if( n->desno ) DeleteElement( n->desno, stablo );
    127.         delete n;
    128. }
    129.  
    130. void DeleteB( element * n, BinaryTree* stablo ){
    131.         element * l = n;
    132.         if( n->lijevo ) DeleteElement( n->lijevo, stablo );
    133.         if( n->desno ) DeleteElement( n->desno, stablo );
    134.         l = ParentB( n, stablo );
    135.         if( l != 0 ){
    136.                 if( l->lijevo == n )
    137.                         l->lijevo = 0;
    138.                 else
    139.                         l->desno = 0;
    140.         delete n;
    141.         }
    142.         else{
    143.                 delete stablo->korijen;
    144.                 stablo->korijen = 0;
    145.         }
    146. }
    147.  
    148. bool ExistsLeftChildB( element * n, BinaryTree* stablo ){
    149.         if( LeftChildB( n, stablo ) != 0 )
    150.                 return true;
    151.         else
    152.                 return false;
    153. }
    154.  
    155. bool ExistsRightChildB( element * n, BinaryTree* stablo ){
    156.         if( RightChildB( n, stablo ) != 0 )
    157.                 return true;
    158.         else
    159.                 return false;
    160. }
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2033"></script>

First / Previous / Next / Last / Page 1 of 1 (2 posteets)