Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:41:20+00:00 2026-06-11T02:41:20+00:00

I haven’t implemented binary for a long time, and recently got a requirement to

  • 0

I haven’t implemented binary for a long time, and recently got a requirement to do that (to demonstrate some code), and I started using:

@interface NSNode : NSObject

@property (strong, nonatomic) NSNode *leftChild;
@property (strong, nonatomic) NSNode *rightChild;

but later on, it seems that whenever I need to do something to both nodes, I need to write it twice — once for the left child and once for the right child, unless if I make what needs to be done into an extra method, and pass the proper arguments carefully — there will be a lot of extra methods just to accomplish this.

If it had been done by using an NSMutableArray

typedef enum {
    NSNodeLeft = 0,
    NSNodeRight
} NSNodeLeftOrRightSide;

@interface NSNode : NSObject

@property (strong, nonatomic) NSMutableArray *children;

then I can always just use a loop to do something to both nodes:

for (NSNode *node in self.children) {
    // do something
}

Or if an index is needed to tell whether it is left or right child:

[self.children enumerateObjectsUsingBlock:
    ^(NSNode *node, NSUInteger nodeIndex, BOOL *stop) {
        // do something
    }
];

and the nodeIndex can be used to determined whether it is left or right child.

And what’s more, this class can be easily extend to a tree with N-children. Is this actually a better practice? Is there any disadvantage except for a very small performance for using array? (I chose NSMutableArray instead of NSArray because if we ever need to make this N-children, we don’t need to change it to NSMutableArray all over the place).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:41:22+00:00Added an answer on June 11, 2026 at 2:41 am

    First, you should not use the name NSNode. Apple reserves NS prefix for its own use.

    Second, just give your Node class its own enumerate message.

    @interface Node : NSObject
    
    - (void)enumerateChildrenUsingBlock:(void (^)(Node *child, NSUInteger childIndex, BOOL *stop))block;
    
    // I prefer this shorter name style in my own classes:
    - (void)forEachChild:(void (^)(Node *child, NSUInteger childIndex, BOOL *stop))block;
    

    Implementing it is trivial:

    @implementation Node
    
    - (void)enumerateChildrenUsingBlock:(void (^)(Node *child, NSUInteger childIndex, BOOL *stop))block {
        BOOL stop = NO;
        block(self.leftChild, 0, &stop);
        if (!stop) {
            block(self.rightChild, 1, &stop);
        }
    }
    

    If you implement the NSFastEnumeration protocol, you can also write a for/in loop like this:

    for (Node *child in parentNode) {
        // do something with child
    }
    

    You can implement NSFastEnumeration like this:

    @interface Node : NSObject <NSFastEnumeration>
    ...
    
    @implementation Node
    
    - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)stackbufCount {
        // The compiler initializes state->state to 0 at the start of the for...in loop.
        // I use it to track which children have been returned.
        // 0 = no children returned yet
        // 1 = left children returned
        // 2 = both children returned
    
        state->itemsPtr = stackbuf;
        state->mutationsPtr = (__bridge unsigned long *)self;
    
        NSUInteger count = 0; // number of children I'm returning on this call
        if (state->state < 1 && count < stackbufCount) {
            stackbuf[count++] = self.leftChild;
            ++state->state;
        }
        if (state->state < 2 && count < stackbufCount) {
            stackbuf[count++] = self.rightChild;
            ++state->state;
        }
    
        return count;
    }
    

    Take a look at this article for more about fast enumeration: http://www.mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't played with CSS for too long a time and am without references
Haven't done ASP.NET development since VS 2003, so I'd like to save some time
I haven't found any solution, some people ask almost the same thing, but that
I haven't used the STL much before, but I started to on this huffman
I haven't been able to get this working and all of the sample code
I've got a string that has curly quotes in it. I'd like to replace
Haven't touch javascript for 3 years. Just got a javascript project and wanted to
Haven't seen anything, but I have seen that you can create albums in the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Haven't touched Ruby for ages so this really puzzled me. Started up irb. Then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.