BST check function..whats wrong?

This is a discussion on BST check function..whats wrong? within the C Programming forums, part of the General Programming Boards category; Code: /*Check if the variable root is a BST*/ 113 int is_BST(tnode_t *node){ 114 return( (node == NULL) || ( ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    BST check function..whats wrong?

    Code:
    /*Check if the variable root is a BST*/
    113 int is_BST(tnode_t *node){
    114     return( (node == NULL) || (
    115                             (node->data > node->left->data) &&
    116                             (node->data < node->right->data) &&
    117                             (is_BST(node->left)) &&
    118                             (is_BST(node->right))
    119                             )
    120         );
    121 }
    Im looking to return true if it is a BST, flase otherwise, im getting an "error: syntax error before '}' token"

    aNy help greaty appreciated, im sure its just something simple, i just cant spot it..

    EDIT::
    Well i managed to get rid of the error, forgot to put a function declaration in..but now im getting segmentation faults when i run it.
    Last edited by roy.42; 08-18-2007 at 03:28 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Might want to check if node->left and node->right are NULL or not, although that isn't your problem currently.

    I don't see the syntax error, and you weren't kind enough to tell me what line it was on, so I'm sorry.

    Edit: See my first statement to fix the seg faults.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    oh sorry, it was on 121...
    Oh, yeah, where abouts should i be checking them? Will i still be able to keep it in the one return statement do you thnk?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yeah, I suppose you could, but I wouldn't recommend making giant return statements.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Thanks for your help thus far, definately getting somewhere..
    Code:
    114 /*Check if the variable root is a BST*/
    115 int is_BST(tnode_t *node){
    116     if (node == NULL){
    117         return 1;
    118     }
    119     else if( node->left!=NULL && node->right !=NULL){
    120         if ((node->data > node->left->data) &&
    121             (node->data < node->right->data) &&
    122             (is_BST(node->left)) &&
    123             (is_BST(node->right))
    124             ){
    125                     return 1;
    126             }
    127             else{
    128                     return 0;
    129             }
    130     }
    131 
    132     else if ( node->left!=NULL && node->right == NULL){
    133         if((node->data>node->left->data) &&
    134             (is_BST(node->left))){
    135                 return 1;
    136             }
    137             else{
    138                 return 0;
    139             }
    140     }
    141 
    142     else if ( node->left==NULL && node->right != NULL){
    143         if((node->data<node->right->data) &&
    144             (is_BST(node->right))){
    145                 return 1;
    146             }
    147             else{
    148                 return 0;
    149             }
    150     }
    151 else return 0;
    152 }
    Ok so i've split it up a bit..it compiles and runs, only problem is, its returning 0 for sometihng i know is a BST..
    Last edited by roy.42; 08-18-2007 at 04:24 AM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your code returns false if both left and right are NULL. Guess that would be a valid tree.
    Kurt

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Hahahaha, thanks.
    Oh well, to anyone else that was looking add these lines
    Code:
    119     else if (node->left == NULL && node->right == NULL){
    120         return 1;
    121     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21