Giter Site home page Giter Site logo

rwtreenode's Introduction

RWTreeNode

Description

RWTreeNode is An easy-to-use tree node structure in Objective-C.

Usage

Basic usage

Following is code to create a tree with RWTreeNode

    RWTreeNodeObject<NSString*>* root = [[RWTreeNodeObject alloc] initWithValue:@"0"];
    RWTreeNodeObject<NSString*>* n1 = [[RWTreeNodeObject alloc] initWithValue:@"1"];
    RWTreeNodeObject<NSString*>* n2 = [[RWTreeNodeObject alloc] initWithValue:@"2"];
    RWTreeNodeObject<NSString*>* n3 = [[RWTreeNodeObject alloc] initWithValue:@"3"];
    RWTreeNodeObject<NSString*>* n4 = [[RWTreeNodeObject alloc] initWithValue:@"4"];
    RWTreeNodeObject<NSString*>* n5 = [[RWTreeNodeObject alloc] initWithValue:@"5"];
    RWTreeNodeObject<NSString*>* n6 = [[RWTreeNodeObject alloc] initWithValue:@"6"];
    RWTreeNodeObject<NSString*>* n7 = [[RWTreeNodeObject alloc] initWithValue:@"7"];
    RWTreeNodeObject<NSString*>* n8 = [[RWTreeNodeObject alloc] initWithValue:@"8"];
    RWTreeNodeObject<NSString*>* n9 = [[RWTreeNodeObject alloc] initWithValue:@"9"];
    [root addChild:n1];
    [root addChild:n2];
    [root addChild:n3];
    [n3 addChild:n4];
    [n4 addChild:n5];
    [n5 addChild:n6];
    [n5 addChild:n7];
    [n7 addChild:n8];
    [n7 addChild:n9];

You can view tree hierachy in lldb:

(lldb)po [root dump]

result will be revealed in console like:

  <RWTreeNodeObject:0x7fb8fa900d70 - value:0>
    <RWTreeNodeObject:0x7fb8fa900df0 - value:1>
    <RWTreeNodeObject:0x7fb8fa900e20 - value:2>
    <RWTreeNodeObject:0x7fb8fa900e50 - value:3>
        <RWTreeNodeObject:0x7fb8fa900e80 - value:4>
            <RWTreeNodeObject:0x7fb8fa900eb0 - value:5>
                <RWTreeNodeObject:0x7fb8fa900ee0 - value:6>
                <RWTreeNodeObject:0x7fb8fa9003a0 - value:7>
                    <RWTreeNodeObject:0x7fb8fa9003d0 - value:8>
                    <RWTreeNodeObject:0x7fb8fa900400 - value:9>

Traveral

  • DFS
  [root dfsWithBlock:^(id<RWTreeNode> currentNode, NSIndexPath *indexPath, BOOL *stop) {
    NSLog([currentNode description]);
  }];
  • BFS
  [root bfsWithBlock:^(id<RWTreeNode> currentNode, NSIndexPath *indexPath, BOOL *stop) {
    NSLog([currentNode description]);
  }];
  • Back Trace
  [n6 backTraceToRootWithBlock:^(id<RWTreeNode> currentNode, BOOL *stop) {
    NSLog([currentNode description]);
  }];

Indexing

We can operate tree node via index path or get index path of a node in tree. For instance, indexPath of n9 :

(lldb) po [n9 indexPath]
<NSIndexPath: 0x7fb8faa0d560> {length = 5, path = 2 - 0 - 0 - 1 - 1}

Some other interface for tree node operation based on index:

- (id<RWTreeNode>) childAtIndexPath:(NSIndexPath*) indexPath;
- (void) removeChildAtIndexPath:(NSIndexPath*) indexPath;
- (void) insertChild:(id<RWTreeNode>)child atIndexPath:(NSIndexPath*) indexPath;

Issues

rwtreenode's People

Contributors

deput avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.