Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.LinkedList;
  3.  
  4. class Node<T>
  5. {
  6.     T value;
  7.     Node<T> right;
  8.     Node<T> left;
  9.     Node<T> parent;
  10.  
  11.     Node(T value)
  12.     {
  13.         this.value = value;
  14.         right = left= parent = null;
  15.     }
  16. }
  17.  
  18. public class MyTreeSet<T extends Comparable<T> > {
  19.     private Node<T> root = null;
  20.     private int treeSize = 0;
  21.  
  22.     public int size()
  23.     {
  24.         return treeSize;
  25.     }
  26.  
  27.     public boolean add(T value)
  28.     {
  29.         Node<T> next = root;
  30.         Node<T> parent = null;
  31.         int diff;
  32.  
  33.         // Look to see if the Item is already in the Tree
  34.         while(next != null)
  35.         {
  36.             parent = next;
  37.             diff = value.compareTo(next.value);
  38.             if (diff == 0) // found match and can't add ... must be unique
  39.                 return false;
  40.             else if (diff > 0 )
  41.                 next = next.right;
  42.             else
  43.                 next = next.left;
  44.         }
  45.  
  46.         // Couldn't be found ... need to insert it.
  47.         Node<T> newNode = new Node<T>(value);
  48.         treeSize += 1;
  49.  
  50.         if (parent == null)
  51.             root = newNode; // Must be the first Node
  52.         else
  53.         {
  54.             // Create the parent, left/right links
  55.  
  56.             newNode.parent = parent;
  57.             diff = value.compareTo(parent.value);
  58.             if (diff > 0)
  59.                 parent.right = newNode;
  60.             else
  61.                 parent.left = newNode;
  62.         }
  63.  
  64.         return true;        
  65.     }
  66.  
  67.     public boolean contains(T value)
  68.     {
  69.         Node<T> next = root;
  70.         int diff;
  71.  
  72.         while(next != null)
  73.         {
  74.             diff = value.compareTo(next.value);
  75.             if (diff == 0) // found match
  76.                 return true;
  77.             else if (diff > 0 )
  78.                 next = next.right;
  79.             else
  80.                 next = next.left;
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     public Iterator<T> iterator()
  86.     {
  87.         return new MyIterator();
  88.     }
  89.  
  90.     /******* Inner class Iterator *****/
  91.     class MyIterator implements Iterator<T>
  92.     {
  93.         private Node<T> nextNode = null;
  94.  
  95.         MyIterator()
  96.         {
  97.             // Get the first node in the list
  98.             nextNode = root;
  99.             if (nextNode != null)
  100.                 goDownLeftLinks();
  101.         }
  102.  
  103.         private void goDownLeftLinks()
  104.         {
  105.             while (nextNode.left != null)
  106.                 nextNode = nextNode.left;
  107.         }
  108.         private void moveToNext()
  109.         {
  110.             if (nextNode == null) return; // must be done
  111.             if (nextNode.right != null)
  112.             {
  113.                 // Need to follow the tree on the right
  114.                 nextNode = nextNode.right;
  115.                 goDownLeftLinks();
  116.             }
  117.             else
  118.             {
  119.                 // We have already processed the left subtree, and there is no right tree.
  120.                 // We want to move up to find a parent where our nextNode is the left child
  121.                 // of the parent.  Note that nextNode has to be either a left or a right child.
  122.                 // So we keep climbing as long as nextNode is a right child.
  123.  
  124.                 Node<T> parent = nextNode.parent;
  125.                 while (parent != null && nextNode == parent.right)
  126.                 {
  127.                     nextNode = parent;
  128.                     parent = nextNode.parent;
  129.                 }
  130.                 // if nextNode is null, then we are done.
  131.                 nextNode = parent;
  132.             }
  133.         }
  134.  
  135.  
  136.         @Override
  137.         public boolean hasNext() {
  138.             if (nextNode== null)
  139.                 return false;
  140.             return true;
  141.         }
  142.  
  143.         @Override
  144.         public T next() {
  145.             T value = nextNode.value;
  146.             moveToNext();
  147.             return value;
  148.         }
  149.  
  150.         @Override
  151.         public void remove() {
  152.             throw new UnsupportedOperationException("Iterator.remove not implemented");
  153.  
  154.         }
  155.  
  156.     }
  157.     // ******************  End of Inner class for Iterator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement