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 9226349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:48:31+00:00 2026-06-18T04:48:31+00:00

I am trying to build a tree to store USB device information. I thought

  • 0

I am trying to build a tree to store USB device information. I thought that I would use NSMutableArray and NSMutableDictionary to contain this information. My problem is that I’ve never studied software engineering – I’m learning as I go – and I haven’t the faintest idea about tree theory.

I’m basing my tree on the USB Location ID, which is eight nibbles long. As I understand it, each nibble represents a layer of the tree (if you see what I mean). I’ve written a little bit of test code to see if I can build my tree properly – and, sadly, it seems that I can’t!

#import <Foundation/Foundation.h>

#define MAXCHILDREN 0xf

NSDictionary* AddItemToTree(NSDictionary* nodeEntry, unsigned int value, int depth)
{
    // Convert the value into a set of nibbles
    char *bytes = (char *)&value;
    char byte = bytes[depth];

    NSMutableDictionary* thisEntry = [[[NSMutableDictionary alloc] initWithDictionary:nodeEntry] autorelease];

    if (byte == 0)
    {
        [thisEntry setObject:[NSString stringWithFormat:@"%08x",value] forKey:@"Value"];
        [thisEntry setObject:[NSString stringWithFormat:@"%08x",byte] forKey:@"Byte"];
        [thisEntry setObject:[NSNumber numberWithInt:depth] forKey:@"Depth"];

        return thisEntry;
    }



    if(![[thisEntry allKeys]containsObject:@"ChildEntries"])
    {
        NSMutableArray* childArray = [[NSMutableArray alloc]init];
        NSMutableDictionary* newNode = [[NSMutableDictionary alloc] init];

        [childArray addObject:AddItemToTree(newNode,value,++depth)];

        [thisEntry setObject:[NSNumber numberWithInt:depth] forKey:@"Depth"];
        [thisEntry setObject:[NSString stringWithFormat:@"%08x",value] forKey:@"Value"];
        [thisEntry setObject:[NSString stringWithFormat:@"%08x",byte] forKey:@"Byte"];
        [thisEntry setObject:childArray forKey:@"ChildEntries"];


        [newNode release];
        [childArray release];

    }
    else
    {
        [[thisEntry objectForKey:@"ChildEntries"]addObject:AddItemToTree(thisEntry,value, ++depth)];

    }


    return thisEntry;
}

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


    NSMutableDictionary* treenode=[[NSMutableDictionary alloc]init];

    char bytearray[4] = {0x0F, 0x0F, 0x02, 0x00};
    unsigned int *value = (unsigned int*)bytearray;
    char bytearray2[4] = {0x0F, 0x02, 0x00, 0x00};
    unsigned int *value2 = (unsigned int*)bytearray2;
    char bytearray3[4] = {0x0F, 0x02, 0x00, 0x00};
    unsigned int *value3 = (unsigned int*)bytearray3;


    [treenode setObject:[NSNumber numberWithInt:0] forKey:@"Depth"];
    [treenode setObject:[NSString stringWithFormat:@"%08x",*value] forKey:@"Value"];
    [treenode setObject:AddItemToTree(treenode,*value, 0) forKey:@"ChildEntries"];

//    [[treenode objectForKey:@"ChildEntries"]addObject:AddItemToTree(treenode,*value2, 0)];


    [treenode writeToFile:@"/Users/headbanger/Desktop/test.plist" atomically:YES];

    [pool release];
}

Adding one USB location ID works perfectly. Adding a second (by uncommenting-out the line in main) causes SIGABRT. I’m sure that it’s perfectly simple, and I’ve committed a typical newbie error. However, it’s not obvious to me and any help that you can provide would be more than welcome.

My tree will need to look something like this:

F-
 |--F-
 |   |--2
 |
 |--2

This tree should be true even if an attempt is made to add the third byte array.
If you can answer the question without being USB specific then that would be most helpful, because I’d really like to understand about trees and what I’ve done wrong. That said, if there’s a quick and easy way to get a tree built for me in Objective-C then I’d love to hear it.
So please, experts, can someone tell me what I’m doing wrong? Thank you for your time.

  • 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-18T04:48:32+00:00Added an answer on June 18, 2026 at 4:48 am

    One problem is that you set dictionary as the type of the ChildEntries:

    [treenode setObject:AddItemToTree(treenode,*value, 0) forKey:@"ChildEntries"];
    

    but elsewhere you attempt to use it as a NSMutableArray (mind the addObject: method):

    [[thisEntry objectForKey:@"ChildEntries"]addObject:AddItemToTree(thisEntry,value, ++depth)];
    

    To fix it, in your main you could do

    [treenode setObject:[[NSMutableArray alloc] initWithObjects:AddItemToTree(treenode,*value, 0), nil]forKey:@"Children"];
    

    but even when your recursion progresses towards the 0x00 byte if (byte==0), I think, from mentally inspecting it, that it’s going to add duplicated children and produce a reeeally deep tree.

    There is something wrong with your environment if you didn’t get a message warning you of the wrong method addObject with the SIGABORT.

    Btw, it’s hard to read. Lines like these

    [treenode setObject:[[NSMutableArray alloc] initWithObjects:AddItemToTree(treenode,*value, 0), nil]forKey:@"Children"];
    

    are easier to scan and less prone to mistakes if you write:

    NSString * const kChildren = @"Children";
    // ...
    NSMutableArray *children = [[NSMutableArray alloc] initWithObjects:AddItemToTree(treenode,*value, 0), nil];
    [treenode setObject:children forKey:kChildren];
    

    The style is not very objective-c-ish, you could use NSUInteger and NSData instead unsigned int and char arrays.


    You should first write a generic tree, then use it for your purposes. This is my tree example. It’s ugly but it’s mine. As you see, it’s common sense. You could set conditions like, two childs per node, and left child < root < right child, and then you would get a binary search tree which has better properties to find stuff. But that will take you a lot more code I guess.

    #import <Foundation/Foundation.h>
    
    typedef NS_ENUM(unsigned char, MyTreeVisitingOrder) {
        MyTreeOrderDepthFirst,
        MyTreeOrderValueFirst
    };
    
    #define Tree NSObject<MyTree>
    
    @protocol MyTree
    @property (nonatomic,strong) NSObject<NSCopying>* key;
    @property (nonatomic,strong) NSObject *value;
    @property (nonatomic,strong) NSMutableDictionary *children;
    -(void) insertChild:(Tree*)node;
    -(void) each:(void(^)(NSObject*))block order:(MyTreeVisitingOrder)order;
    @end
    
    
    @interface TreeImpl : NSObject <MyTree>
    -(id) init __attribute__((unavailable("disabled")));
    @end
    
    @implementation TreeImpl
    
    @synthesize key = _key;
    @synthesize value = _value;
    @synthesize children = _children;
    
    
    -(id) initWithKey:(NSObject<NSCopying>*)key value:(NSObject*)value {
        self = [super init];
        if (self){
            _key = key;
            _value = value;
            _children = [NSMutableDictionary new];
        }
        return self;
    }
    
    
    -(void) insertChild:(Tree*)node {
        [_children setObject:node forKey:node.key];
    }
    
    -(void) each:(void(^)(NSObject*))block order:(MyTreeVisitingOrder)order {
        switch (order) {
            case MyTreeOrderDepthFirst:{
                if (_children) {
                    for (id key in _children){
                        [[_children objectForKey:key] each:block order:order];
                    }
                }
                block(_value);
                break;
            }
            case MyTreeOrderValueFirst:{
                block(_value);
                if (_children) {
                    for (id key in _children){
                        [[_children objectForKey:key] each:block order:order];
                    }
                }
                break;
            }
        }
    }
    
    @end
    
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
    
            TreeImpl *a = [[TreeImpl alloc] initWithKey:@"A" value:@"A"];
            TreeImpl *b = [[TreeImpl alloc] initWithKey:@"B" value:@"B"];
            TreeImpl *c = [[TreeImpl alloc] initWithKey:@"C" value:@"C"];
            [a insertChild:b];
            [a insertChild:c];
    
            [a each:^(NSObject* value) {
                NSLog(@"> %@",value);
            } order:MyTreeOrderValueFirst];
            return EXIT_SUCCESS;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an SWT Tree that has icons at the top
I'm trying to build a supervisor tree where I have this structure: 1 root
I've been trying to build live555 according to this guide: https://github.com/boltonli/ohbee/tree/master/android/streamer/jni as well as
Hey all, so I am trying to build a simple binary tree that has
I am trying to build a tree structure that represents a parsed configuration file
I'm trying to build an expression tree for this linq query: so I can
I'm trying to build a tree with data in the nodes as well as
Problem: I am trying to build a recursive tree using a function and data
I'm trying to build an expression tree ( still ) but getting further! I
I am trying to build a Quadtree data structure(or let's just say a tree)

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.