Guest User

BTNode

a guest
Apr 26th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. //*******************************************************************
  2. //  BTNode.java       Java Foundations
  3. //
  4. //  Represents a node in a binary tree with a left and right child.
  5. //  Therefore this class also represents the root of a subtree.
  6. //*******************************************************************
  7.  
  8. package javafoundations;
  9.  
  10. public class BTNode<T>
  11. {
  12.    protected T element;
  13.    protected BTNode<T> left, right;
  14.  
  15.    //-----------------------------------------------------------------
  16.    //  Creates a new tree node with the specified data.
  17.    //-----------------------------------------------------------------
  18.    public BTNode (T element)
  19.    {
  20.       this.element = element;
  21.       left = right = null;
  22.    }
  23.  
  24.    //-----------------------------------------------------------------
  25.    //  Returns the element stored in this node.
  26.    //-----------------------------------------------------------------
  27.    public T getElement()
  28.    {
  29.       return element;
  30.    }
  31.  
  32.    //-----------------------------------------------------------------
  33.    //  Sets the element stored in this node.
  34.    //-----------------------------------------------------------------
  35.    public void setElement (T element)
  36.    {
  37.       this.element = element;
  38.    }
  39.  
  40.    //-----------------------------------------------------------------
  41.    //  Returns the left subtree of this node.
  42.    //-----------------------------------------------------------------
  43.    public BTNode<T> getLeft()
  44.    {
  45.       return left;
  46.    }
  47.  
  48.    //-----------------------------------------------------------------
  49.    //  Sets the left child of this node.
  50.    //-----------------------------------------------------------------
  51.    public void setLeft (BTNode<T> left)
  52.    {
  53.       this.left = left;
  54.    }
  55.  
  56.    //-----------------------------------------------------------------
  57.    //  Returns the right subtree of this node.
  58.    //-----------------------------------------------------------------
  59.    public BTNode<T> getRight()
  60.    {
  61.       return right;
  62.    }
  63.  
  64.    //-----------------------------------------------------------------
  65.    //  Sets the right child of this node.
  66.    //-----------------------------------------------------------------
  67.    public void setRight (BTNode<T> right)
  68.    {
  69.       this.right = right;
  70.    }
  71.  
  72.    //-----------------------------------------------------------------
  73.    //  Returns the element in this subtree that matches the
  74.    //  specified target. Returns null if the target is not found.
  75.    //-----------------------------------------------------------------
  76.    public BTNode<T> find (T target)
  77.    {
  78.       BTNode<T> result = null;
  79.  
  80.       if (element.equals(target))
  81.          result = this;
  82.       else
  83.       {
  84.          if (left != null)
  85.             result = left.find(target);
  86.          if (result == null && right != null)
  87.             result = right.find(target);
  88.       }
  89.  
  90.       return result;
  91.    }
  92.  
  93.    //-----------------------------------------------------------------
  94.    //  Returns the number of nodes in this subtree.
  95.    //-----------------------------------------------------------------
  96.    public int count()
  97.    {
  98.       int result = 1;
  99.  
  100.       if (left != null)
  101.          result += left.count();
  102.  
  103.       if (right != null)
  104.          result += right.count();
  105.  
  106.       return result;
  107.    }
  108.  
  109.    //-----------------------------------------------------------------
  110.    //  Performs an inorder traversal on this subtree, updating the
  111.    //  specified iterator.
  112.    //-----------------------------------------------------------------
  113.    public void inorder (ArrayIterator<T> iter)
  114.    {
  115.       if (left != null)
  116.          left.inorder (iter);
  117.  
  118.       iter.add (element);
  119.  
  120.       if (right != null)
  121.          right.inorder (iter);
  122.    }
  123.  
  124.    //-----------------------------------------------------------------
  125.    //  The following methods are left as programming projects.
  126.    //-----------------------------------------------------------------
  127.    // public void preorder (ArrayIterator<T> iter) { }
  128.    // public void postorder (ArrayIterator<T> iter) { }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment