
#import <objc/Object.h>
#import "TreeNode.h"

@implementation TreeNode : Object
/* id datum;         */
/* TreeNode *left;   */
/* TreeNode *right;  */

+ new
{
   return [[self alloc] init];
}

+ new: value
{
   return [[self new] setData: value];
}

+ new: (TreeNode *)left_value with: (TreeNode *)right_value
{
   TreeNode *t = [self new];
   [t setLeft: left_value];
   [t setRight: right_value];
   return t;
}

+ new: value with: (TreeNode *)left_value and: (TreeNode *)right_value
{
   TreeNode *t = [self new: value];
   [t setLeft: left_value];
   [t setRight: right_value];
   return t;
}

- getData
{
   return datum;
}

- (TreeNode *) left
{
   return left;
}

- (TreeNode *) right
{
   return right;
}

- setData: value
{
   id old_value = datum;
   datum = value;
   return old_value;
}

- setLeft: (TreeNode *)child
{
   left = child;
   return self;
}

- setRight: (TreeNode *)child
{
   right = child;
   return self;
}

- (int) count
{
   int l = 0;
   int r = 0;
   if ( left != NIL ) l = [left count];
   if ( right != NIL ) r = [right count];
   return 1 + l + r;
}

- (int) depth
{
   int l = 0;
   int r = 0;
   if ( left != NIL ) l = [left depth];
   if ( right != NIL ) r = [right depth];
   return 1 + ( l > r ? l : r );
}

- (int) width
{
   int w = 1;
   if ( left != NIL )
   {
      w = [left width];
   }
   if ( right != NIL )
   {
      w = w + [right width];
   }
   return w;
}

- (BOOL) balanced
{
   /* worry about this later */
   return FALSE;
}

@end
