View: Implementacija binarnog stabla pomoću polja

  1. 1 month ago by mkrobot
    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 comments about "Implementacija binarnog stabla pomoću polja"

  1. Moj kod se od ostalih razlikuje po imenima varijabli
    mkrobot on January 12, 2010
  2. Razlika je i u korištenju pojedinih varijabli kod fukcije za inicijalizaciju binarnog stabla
    mkrobot on January 12, 2010