Thread: Here's a problem, any one helps me?

  1. #16
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    The insert and find function, copied from my textbook:

    Code:
    SearchTree
    Insert( ElementType X, SearchTree T )
    {
      /* 1*/      if( T == NULL )
        {
          /* Create and return a one-node tree */
          /* 2*/          T = malloc( sizeof( struct TreeNode ) );
          /* 3*/          if( T == NULL )
    	/* 4*/              FatalError( "Out of space!!!" );
          else
    	{
    	  /* 5*/              T->Element = X;
    	  /* 6*/              T->Left = T->Right = NULL;
    	}
        }
      else
        /* 7*/      if( X < T->Element )
          /* 8*/          T->Left = Insert( X, T->Left );
        else
          /* 9*/      if( X > T->Element )
    	/*10*/          T->Right = Insert( X, T->Right );
      /* Else X is in the tree already; we'll do nothing */
      
      /*11*/      return T;  /* Do not forget this line!! */
    }
    /* END */
    
    
    Position
    Find( ElementType X, SearchTree T )
    {
      if( T == NULL )
        return NULL;
      if( X < T->Element )
        return Find( X, T->Left );
      else
        if( X > T->Element )
          return Find( X, T->Right );
        else
          return T;
    }

  2. #17
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    It seems the probelm happened in this two lines
    Code:
    Insert(num, T);    
    P = Find(num, T);
    because the printf statment after the two is not working.

  3. #18
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    O....no
    I chaged the statment into T = Insert(num, T);
    but still not works, this goddam thing almost make me mad.

  4. #19
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    Now I am sure that the problem is in the Insert function, but why?
    I just copy it from my textbook. O...god, if I cannot find it, I will die.
    Code:
    SearchTree
    Insert( ElementType X, SearchTree T )
    {
      /* 1*/      if( T == NULL )
        {
          /* Create and return a one-node tree */
          /* 2*/          T = malloc( sizeof( struct TreeNode ) );
          /* 3*/          if( T == NULL )
    	/* 4*/              FatalError( "Out of space!!!" );
          else
    	{
    	  /* 5*/              T->Element = X;
    	  /* 6*/              T->Left = T->Right = NULL;
    	}
        }
      else
        /* 7*/      if( X < T->Element )
          /* 8*/          T->Left = Insert( X, T->Left );
        else
          /* 9*/      if( X > T->Element )
    	/*10*/          T->Right = Insert( X, T->Right );
      /* Else X is in the tree already; we'll do nothing */
      
      /*11*/      return T;  /* Do not forget this line!! */
    }
    /* END */

  5. #20
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    yes, you r right, I found it in my textbook as soon as you post it on!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM