Reputation: 13
I'm doing an in order tree traversal of a custom coded BST. Here's the code
-(void) inOrderTraversalToString : (NSString*) accumulateString : (Node*) ptrNode {
if(ptrNode == nil) {
return;
} else {
[inOrderTraversalToString : accumulateString : ptrNode.left];
accumulateString += [[NSNumber : ptrNode.datum] stringValue];
[inOrderTraversalToString : (NSString*) accumulateString, ptrNode.right];
}
}
I'm getting all kinds of awful error messages when trying to compile this. I know it's probably just a typo somewhere, but some peer review would be nice. I've been churning at this for days.
Thanks. -- Alex
Upvotes: 1
Views: 979
Reputation: 16
The syntax in your code looks wrong in several places. To use multiple parameters you need to extend your function name to have a parameter description for each parameter. You might want to change it to something like this:
-(void) inOrderTraversalToString:(NSString*)accumulateString FromNode:(Node*)ptrNode
In the function body you're making function calls incorrectly. A function call should look like:
[object/class param_desc:(Param1Type)param1 param_desc:(Param2Type)param2 ...];
Call with the class name first if you are calling a static class method, otherwise use the object name that you are operating on. So in your code the first recursive call might look like this:
[YourObjectOrClass inOrderTraversalToString:accumulateString FromNode:ptrNode.left];
You may want to brush up on the basics of Objective C. Check out
Upvotes: 0
Reputation: 5313
Your code is far from legal Objective-C. The + operator is not defined for NSString (and overloading is not allowed). NSString isn't going to work with your recursion anyway because it is immutable. You'll have to use NSMutableString. You are not calling or declaring methods correctly, either.
Here is an attempt at a correct version:
- (void)inOrderTraversalToString:(NSMutableString *)accumulateString withNode:(Node *)node
{
if(ptrNode == nil) return;
[self inOrderTraversalToString:accumulatedString withNode:ptrNode.left];
[accumulatedString appendString:[ptrNode.datum stringValue]];
[self inOrderTraversalToString:accumulatedString withNode:ptrNode.right];
}
This code assumes that left
, right
, and datum
really are properties of your Node
class, and that accumulatedString
has been initialized to something reasonable before calling this method.
Upvotes: 1
Reputation: 17317
You need to use self
when calling the methods. So something like:
[self inOrderTraversalToString : accumulateString : ptrNode.left];
Upvotes: 0
Reputation: 11970
your method signature seems wrong, it should be like this:
param_description:(param_type) parameter param2_description:(param2_type) parameter2
try something like:
- (void) inOrderTraversalToString:(NSString*) accumulateString node:(Node*) ptrNode
then there is the way you call your methods; in general the syntax looks like this:
[object method];
[object method2:arg1 argname2:arg2];
I suggest you read some basic introduction to objective-C because you are struggling with the basics.
Upvotes: 0