I have an ordered binary tree class in the data property that I store id data so that the tree can store any data.

When creating a new node, I compare the values โ€‹โ€‹([tmp.data compare: Node.data]) and, depending on this, I follow the left branch or the right one.

Here is an example: I add the root @ "6", and then add the node with the data @ "12" and it goes along the left branch. I understand that 1 <6, but the second line seems to be longer and should go to the right.

Tell me what am I doing wrong?

    1 answer 1

    The length of the string can be compared as follows:

    NSString *a = @"a"; NSString *b = @"bb"; if (b.length > a.length) { // true } 

    The values โ€‹โ€‹of the string are compared as follows:

     NSString *a = @"a"; NSString *b = @"bb"; if ([a isEqualToString:b]) { // false } 
    • I have a data type id in my node, i.e. Not the fact that there will be a line. I compare with the compare method, which returns NSComparisonResult - DaskOFF
    • one
      @DaskOFF before comparing it is always necessary to lead the necessary type to NSObject *obj; NSString *myString = obj; [myString isEqualToString @"some string"] - Aleksey Alybin
    • those. I can not set up some kind of comparison type that will work with all types, but for example, will I have to redefine a method in my types? - DaskOFF
    • 3
      and it is better to check the type before comparing [obj isKindOfClass:[NSString class]] - Alexey Alybin