error in binary tree

B

Bill Cunningham

I have found somecode that designs a binary search tree. Unfortunately
the compiler will not compile this saying there is a parse error before
struct on line 30. I am at a loss. I have copied this code changing only a
couple of things and I have stayed consistant.

#include <stdio.h>
#include <stdlib.h>

struct node {
struct node *right;
struct node *left;
int count;
};

static int find(struct node *node, int target)
{
if (node == NULL) {
return (1);
} else {
if (target == node->count) {
return (0);
} else {
if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);
}
}
}

struct node *newNode(int count)
{
struct node *node = new(struct node);

Something is supposed to be wrong with the above line.

node->right = NULL;
node->left = NULL;
node->count = count;
return (node);
}

struct node *insert(struct node *node, int count)
{
if (node == NULL) {
return (newNode(count));
} else {
if (count <= node->count)
node->left = insert(node->left, count);
else
node->right = insert(node->right, count);
return (node);
}
}
 
P

Phil Carmody

Bill Cunningham said:
I have found somecode that designs a binary search tree. Unfortunately
the compiler will not compile this saying there is a parse error before
struct on line 30. I am at a loss. I have copied this code changing only a
couple of things and I have stayed consistant.

#include <stdio.h>
#include <stdlib.h>

struct node {
struct node *right;
struct node *left;
int count;
};

static int find(struct node *node, int target)
{
if (node == NULL) {
return (1);
} else {
if (target == node->count) {
return (0);
} else {
if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);
}
}
}

The inconistency in the use of brackets (of various types) in that is
pretty awful.
struct node *newNode(int count)
{
struct node *node = new(struct node);

Something is supposed to be wrong with the above line.

Yup, it's not C. Unless you've #defined new to be something
appropriate. You, or the source of that code, apparently have
not yet realised that C and C++ are different languages.
Please learn that simple fact.

Phil
 
S

Spiros Bousbouras

I have found somecode that designs a binary search tree. Unfortunately
the compiler will not compile this saying there is a parse error before
struct on line 30. I am at a loss. I have copied this code changing only a
couple of things and I have stayed consistant.

#include <stdio.h>
#include <stdlib.h>

struct node {
struct node *right;
struct node *left;
int count;

};

static int find(struct node *node, int target)
{
if (node == NULL) {
return (1);
} else {
if (target == node->count) {
return (0);
} else {
if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);
}
}

}

struct node *newNode(int count)
{
struct node *node = new(struct node);

Something is supposed to be wrong with the above line.

What's wrong is that C does not have new. Perhaps this is C++ code?
By the way, if you want to insert comments in the middle of code
please put it inside comments like /* Your comments */ so that
people can cut and paste it and attempt to compile it without
having to erase your comments first.
node->right = NULL;
node->left = NULL;
node->count = count;
return (node);
}

Here's a correct version for function newNode. It compiles but I
haven't tested it.

struct node *newNode(int count) {
struct node *node = malloc(sizeof(struct node)) ;

if (node) {
node->right = NULL;
node->left = NULL;
node->count = count;
}
return node ;
}
struct node *insert(struct node *node, int count)
{
if (node == NULL) {
return (newNode(count));
} else {
if (count <= node->count)
node->left = insert(node->left, count);
else
node->right = insert(node->right, count);
return (node);
}
}

--
"Calm down. Yvonne people are staring at you," said Penelope
worriedly.
"Do you feel ok?" she asked questioningly.

Atlanta Nights
 
K

Keith Thompson

Kenneth Brody said:
Bill said:
I have found somecode that designs a binary search
tree. Unfortunately the compiler will not compile this saying there
is a parse error before struct on line 30. I am at a loss. I have
copied this code changing only a couple of things and I have stayed
consistant. [...]
struct node *node = new(struct node);

Something is supposed to be wrong with the above line.
[...]

What's wrong is you're apparently missing a #define for "new", which
given the context of its usage, might possibly perhaps look something
sort of like this, maybe:

#define new(type) malloc(sizeof(type))

It's far more likely that it's a C++ "new" operator (though the
parentheses are unusual; perhaps that was Bill's failed attempt to
avoid a syntax error message).

Bill: Just use malloc.
 
S

Spiros Bousbouras

http://cslibrary.stanford.edu/110/BinaryTrees.html

This is where the code came from. I have changed data to count though.

The site doesn't seem to be making the distinction between
C and C++. In particular the line in question says

struct node* node = new(struct node); // "new" is like "malloc"

The comment is perhaps meant as a hint to those who know
C but not C++. Bill, if you're concentrating on C I recommend
that you avoid sites which claim to have programmes for "C/C++".
 
B

Bill Cunningham

The site doesn't seem to be making the distinction between
C and C++. In particular the line in question says

struct node* node = new(struct node); // "new" is like "malloc"

The comment is perhaps meant as a hint to those who know
C but not C++. Bill, if you're concentrating on C I recommend
that you avoid sites which claim to have programmes for "C/C++".

That new did look kind of fishy but I don't see where in my limited
knowledge of C++ where the new came from or how it would work in c++ either.
As far as the braces go I didn't see where they were wrong.

Bill
 
S

Spiros Bousbouras

The inconistency in the use of brackets (of various types) in that is
pretty awful.
I used indent with the -kr -nut switches.

indent won't correct your typing mistakes or the choice of the wrong
language.

I didn't see any typing mistakes. The insertion of English text
(the phrase "Something is supposed to be wrong with the above
line.") in the middle of the code was a mistake but not a typing
mistake. It doesn't look as if either this or the existence of new
altered the output of indent. Beyond that I don't see why people
complain so much about the appearance of the code. The only 2 grating
things I saw were parentheses around the expression following return
and lines like
return (find(node->left, target)
);
but if indent tries to make sure that each line will not have more
than a certain number of characters then it's unavoidable that some
lines will be broken in two.
 
S

Spiros Bousbouras

As far as the braces go I didn't see where they were wrong.

They weren't wrong. Perhaps some people object to things such as

if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);

where you have braces around the if body but not the else body. It
would be more consistent to have it like

if (target < node->count)
return (find(node->right, target)
);
else
return (find(node->left, target)
);

or

if (target < node->count) {
return (find(node->right, target)
);
} else {
return (find(node->left, target)
);
}

Personally I would write it as

if (target < node->count) return find(node->right , target) ;
else return find(node->left , target ;
 
S

Spiros Bousbouras

Personally I would write it as

if (target < node->count) return find(node->right , target) ;
else return find(node->left , target ;

Last line should be
else return find(node->left , target) ;
 
O

osmium

Bill Cunningham said:
That new did look kind of fishy but I don't see where in my limited
knowledge of C++ where the new came from or how it would work in c++
either.

You posted your question to a C group, indicating that you intended a C
program. It should be of no interest to you at all what C++ has for
reserved words or whatever. C is not Fortran. It is not Cobol. It is not
C++. Choose ***A*** language and stick with it, I think C is a better
choice for you than C++. If you couldn't find new in the header files you
included, that was the time to stop and get your bearings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top