Logo

Programming-Idioms

History of Idiom 17 > diff from v51 to v52

Edit summary for version 52 by OC:
New Obj-C implementation by user [OC]

Version 51

2020-10-02, 21:42:20

Version 52

2020-10-11, 00:22:27

Idiom #17 Create a Tree data structure

The structure must be recursive. A node may have zero or more children. A node has access to children nodes, but not to its parent.

Idiom #17 Create a Tree data structure

The structure must be recursive. A node may have zero or more children. A node has access to children nodes, but not to its parent.

Imports
@import Foundation;
Code
@interface Node:NSObject
@property id value; // id means any object value
@property NSMutableArray *children;
@end

// usage like
Node *n=[Node new];
n.value=@1;
[n.children addObject:otherNode];
[n.children removeLastObject];

// somewhere needed also
@implementation Node
-(instancetype)init {
  if (!(self=[super init])) return nil;
  _children=[NSMutableArray array];
  return self;
}
@end
Comments bubble
Compare also #9