Home Forums C Programming SOS!!! Reply To: SOS!!!

#3424
Humayan
Participant

I have this binary seach tree ( hope this helps ) :
 

/*
* Název programu:                ADT Binarni strom
* Autor:                        Petr Bobek
* Studijní skupina:                IT-01
* Email autora:                    [email protected]
* Datum vypracování:            26.10.2005
* Stručný popis řeÅ¡ení:            Je uveden dale v programu
*/
#include "iostream"
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#include "process.h"
#include "stdlib.h"
#include "string.h" //standardní knihovny
using namespace std;
typedef char tStr;
struct uzel
{
   int item;
   struct uzel *right;
   struct uzel *left;
};
typedef struct uzel * BST;
typedef struct uzel uzel;
bool Empty ( BST t )
{
   if( t != NULL )
       return false;
   else
    return true;
}
void Initialize ( BST * t )
{
   * t = NULL;
  
}
void DeleteTree ( BST t )
{
   if ( t != NULL )
   {
       DeleteTree ( t-> left );
       DeleteTree ( t->right );
       free ( t );
   }
}
BST Search ( BST t, int x )
{
   if( t == NULL )
   {
       cout << "item was not found!" << endl;
       return NULL;
   }
   if( x < t -> item )
   {
       return Search(  t -> left  , x );
   }
   else if( x > t -> item )
       return Search (  t -> right  , x );
   else
       return t;
  
}
BST Insert ( BST * t, int x )
{
   if( * t == NULL )
   {
       * t = (struct uzel * ) malloc ( sizeof ( struct uzel ) );
       (* t ) -> item = x;
       ( * t ) -> left = ( * t ) -> right = NULL;
       return * t;
   }

   if( ( *t ) -> item > x)
   Insert (  & ( ( * t ) -> left  )  , x );
   else
   Insert (  & ( ( * t ) -> right  ) , x );
     
  
}
BST findmin ( BST t )
{
 
 while ( t -> left )
  t = t -> left;
 return t;
 
}
BST findmax ( BST t )
{
   while ( t -> right )
   t = t -> right;
   return t;
  
}
void removeMin ( BST * t )
{
    while ( ( * t ) -> left )
  ( * t ) = ( * t ) -> left;
 
 BST  temp =  * t;
  * t  =   ( ( * t ) -> right );
 free (  temp );

}
//THIS DOESN'T LOOK RIGHT
BST Delete ( BST * t, int x )
{
   BST pom_bunka;
   if( t == NULL )
   {
       cout << " Cannot delete, bin. tree is empty !"<< endl;
       return NULL;
   }
   if ( x < ( * t ) -> item )
      Delete ( & ( ( * t ) -> left ) , x );
   else if ( x > ( * t ) -> item )
      Delete ( & ( ( * t ) -> right ) , x );   
   else if ( ( * t ) -> right != NULL && ( * t ) -> left  != NULL )
   {
       BST temp = findmin ( ( * t ) -> right );
       ( * t ) -> item = temp -> item;
       removeMin ( & (( * t ) -> right ) );
   }
   else
   {
    BST  temp =   * t ;
    if ( ( * t ) -> left != NULL )
   * t = ( * t ) -> left;
    else
     * t = ( * t ) -> right;
    free ( temp  );
 }
}
void Preorder ( BST t )
{
   if ( t != NULL )
   {
       cout << t -> item << endl;
       Preorder ( t -> left );
       Preorder ( t -> right ) ;
   }
 
  
}
void Inorder ( BST t )
{
   if ( t != NULL )
   {
       Inorder ( t -> left );
       cout << t -> item << endl;
       Inorder ( t -> right );
   }
}
void Postorder ( BST t )
{
   if ( t != NULL )
   {
       Postorder ( t -> left );
       Postorder ( t -> right );
       cout << t -> item << endl;
   }
}
int getData ( void )
{
   
    cout << "Get data: ";
    int data;
    cin >> data;
    return data;
}
/*
void VypisSeznam(BST t){
    tStr data;
    cout << "Obsah seznamu:";
    if (Empty(t)==true){
       cout<<" NULL"<     }
    else{
       data = t->item;
       cout << data << ", ";
    }
}
*/
void Menu ( void )//vypis menu
{
    cout << "nGet number 0-7: nn";
    cout << "0: Inicializen";
    cout << "1: Insertn";
    cout << "2: Searchn";
    cout << "3: Preordern";
    cout << "4: Inordern";
    cout << "5: Postordern";
    cout << "6: Deleten";
    cout << "7: Delete treen";
    cout << "M: Menun";
    cout << "E: Endn";
}
int main()
{
  
   char znak;
   BST strom = 0 ; //root ?
   int data;
   //tStr data;
   do {
       Menu();
    cout << "nnEnter Choice ! >> ";
       cin >> znak;
       switch ( toupper ( znak ) )
       {
              case '0' :  cout << "Inicialize - inicializuje stromn";
                          Initialize ( & strom );
                          //VypisSeznam(strom);
                   break;
              case '1' :  cout << "Insert - vlozi do stromu novy uzeln";
                          cout << "Get data: ";
        cin >> data;
                          Insert( & strom , data );
                          //VypisSeznam(strom);
                   break;
              case '2' :  cout << "Search - vyhleda uzeln";
                          Search(strom, getData());
                          cout<                    break;
              case '3' :  cout << "Preorder - projde strom pruchodem preordern";
                          Preorder(strom);
                   break;
              case '4' :  cout << "Inorder - projde strom pruchodem inordern";
                          Inorder(strom);
                   break;
              case '5' :  cout << "Postorder - projde strom pruchodem postordern";
                          Postorder(strom);
                   break;
              case '6' :  cout << "Delete - smaze ze stromu uzeln";
                          Delete( & strom, getData());
                   break;
              case '7' :  cout << "Delete Tree - smaze cely stromn";
                          DeleteTree(strom);
                   break;
              case 'M' : Menu();
                   break;
              case 'E' :
                   break;
              default : cout << "Zadali jste nespravny znak!!!n";
       }
   } while (toupper(znak) != 'E');
   return 0;
}