title: Building a USB Tree in Objective C
body: <p>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.<BR>
  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!</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  #define MAXCHILDREN 0xf
  
  NSDictionary* AddItemToTree(NSDictionary* nodeEntry, unsigned int value, int depth)
  {
      // Convert the value into a set of nibbles
      char *bytes = (char *)&amp;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];
  }
  </code></pre>
  
  <p>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.<BR>
  My tree will need to look something like this:</p>
  
  <pre><code>F-
   |--F-
   |   |--2
   |
   |--2
  </code></pre>
  
  <p>This tree should be true even if an attempt is made to add the third byte array.<BR>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.<BR>So please, experts, can someone tell me what I'm doing wrong? Thank you for your time.</p>