title: How do I find the minimum value of a binary tree?
body: <p>I'm trying to find the minimum value of a binary tree. Every time I run my code I get a long 5 digit number like '32675'. I'm pretty sure my understanding of pointers is wrong, but I'm not positive. If I could get some advice I'd really appreciate it. Thanks!</p>
  
  <p><strong>Node definition</strong></p>
  
  <pre><code>@interface Node:NSObject {
      @property (nonatomic, strong) Node *left;
      @property (nonatomic, strong) Node *right;
      @property (nonatomic, assign) int *value;
  }
  
  -(id)initWithValue:(int)val {
      self = [super init];
      if(self) {
          self.value = &amp;(val);
          self.left = nil;
          self.right = nil;
      }
      return self;
  }
  </code></pre>
  
  <p><strong>Insert Algorithms for Tree</strong></p>
  
  <pre><code>-(void)insertValue:(int)value {
      Node *node = [[Node alloc] initWithValue:value];
      [self insertNode:node];
  }
  
  -(void)insertNode:(Node *)node {
      if (root == nil) {
          root = node;
      } else {
          [node insertNode:node];
      }
  }
  </code></pre>
  
  <p><strong>Insert Algorithms for Node</strong></p>
  
  <pre><code>-(void)insertNode:(Node *)node{
      if (node.value &lt; self.value) {
          [self insertOnLeft:node];
      } else {
          [self insertOnRight:node];
      }
  }
  
  -(void)insertOnLeft:(Node *)node {
      if (self.left == nil) {
          self.left = node;
      } else {
          [self.left insertNode:node];
      }
  }
  
  -(void)insertOnRight:(Node *)node {
      if (self.right == nil) {
          self.right = node;
      } else {
          [self.right insertNode:node];
      }
  }
  </code></pre>
  
  <p><strong>3 values go in to my tree:</strong></p>
  
  <pre><code>[tree insertValue:4];
  [tree insertValue:6];
  [tree insertValue:2];
  
  int min = [tree findMinimum];
  </code></pre>
  
  <p><strong>Tree's findMinimum method is called</strong></p>
  
  <pre><code>-(int)findMinimum {
      assert(root != nil);
      return [root findMinimum];
  }
  </code></pre>
  
  <p><strong>Which call's root's findMinimum - root is a node</strong></p>
  
  <pre><code>-(int)findMinimum {
      Node *node = self;
      int min = 0;
      while (node != nil) {
          min = *(node.value);
          node = node.left;
      }
      return min;
  }
  </code></pre>