Java Program to Implement TreeSet API

This Java program is to Implement TreeSet Collection API. A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

Here is the source code of the Java program to Implement TreeSet Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.NavigableSet;
  5. import java.util.SortedSet;
  6. import java.util.TreeSet;
  7.  
  8. public class TreeSetImpl<E>
  9. {
  10.     private TreeSet<E> treeSet;
  11.  
  12.      /*
  13.       * Constructs a new, empty tree set, sorted according to the natural
  14.       * ordering of its elements.
  15.       */
  16.      public TreeSetImpl()
  17.      {
  18.          treeSet = new TreeSet<E>();
  19.      }
  20.  
  21.      /*
  22.       * Constructs a new tree set containing the elements in the specified
  23.       * collection, sorted according to the natural ordering of its elements.
  24.       */
  25.      public TreeSetImpl(Collection<? extends E> c)
  26.      {
  27.          treeSet = new TreeSet<E>(c);
  28.      }
  29.  
  30.      /*
  31.       * Constructs a new, empty tree set, sorted according to the specified
  32.       * comparator.
  33.       */
  34.      public TreeSetImpl(Comparator<? super E> comparator)
  35.      {
  36.          treeSet = new TreeSet<E>(comparator);
  37.      }
  38.  
  39.      /*
  40.       * Constructs a new tree set containing the same elements and using the same
  41.       * ordering as the specified sorted set.
  42.       */
  43.      public TreeSetImpl(SortedSet<E> s)
  44.      {
  45.          treeSet = new TreeSet<E>(s);
  46.      }
  47.  
  48.      /* Adds the specified element to this set if it is not already present. */
  49.      public boolean add(E e)
  50.      {
  51.          return treeSet.add(e);
  52.      }
  53.  
  54.      /* Adds all of the elements in the specified collection to this set. */
  55.      public boolean addAll(Collection<? extends E> c)
  56.      {
  57.          return treeSet.addAll(c);
  58.      }
  59.  
  60.      /*
  61.       * Returns the least element in this set greater than or equal to the given
  62.       * element, or null if there is no such element.
  63.       */
  64.      public E ceiling(E e)
  65.      {
  66.          return treeSet.ceiling(e);
  67.      }
  68.  
  69.      /* Removes all of the elements from this set. */
  70.      public void clear()
  71.      {
  72.          treeSet.clear();
  73.      }
  74.  
  75.      /* Returns a shallow copy of this TreeSet instance. */
  76.      public Object clone()
  77.      {
  78.          return treeSet.clone();
  79.      }
  80.  
  81.      /*
  82.       * Returns the comparator used to order the elements in this set, or null if
  83.       * this set uses the natural ordering of its elements.
  84.       */
  85.      public Comparator<? super E> comparator()
  86.      {
  87.          return treeSet.comparator();
  88.      }
  89.  
  90.      /* Returns true if this set contains the specified element. */
  91.      public boolean contains(Object o)
  92.      {
  93.          return treeSet.contains(o);
  94.      }
  95.  
  96.      /* Returns an iterator over the elements in this set in descending order. */
  97.      public Iterator<E> descendingIterator()
  98.      {
  99.          return treeSet.descendingIterator();
  100.      }
  101.  
  102.      /* Returns a reverse order view of the elements contained in this set. */
  103.      public NavigableSet<E> descendingSet()
  104.      {
  105.          return treeSet.descendingSet();
  106.      }
  107.  
  108.      /* Returns the first (lowest) element currently in this set. */
  109.      public E first()
  110.      {
  111.          return treeSet.first();
  112.      }
  113.  
  114.      /*
  115.       * Returns the greatest element in this set less than or equal to the given
  116.       * element, or null if there is no such element.
  117.       */
  118.      public E floor(E e)
  119.      {
  120.          return treeSet.floor(e);
  121.      }
  122.  
  123.     /*
  124.      * Returns a view of the portion of this set whose elements are strictly
  125.      * less than toElement.
  126.      */
  127.     public SortedSet<E> headSet(E toElement)
  128.     {
  129.         return treeSet.headSet(toElement);
  130.     }
  131.  
  132.     /*
  133.      * Returns a view of the portion of this set whose elements are less than
  134.      * (or equal to, if inclusive is true) toElement.
  135.      */
  136.     public NavigableSet<E> headSet(E toElement, boolean inclusive)
  137.     {
  138.         return treeSet.headSet(toElement, inclusive);
  139.     }
  140.  
  141.     /*
  142.      * Returns the least element in this set strictly greater than the given
  143.      * element, or null if there is no such element.
  144.      */
  145.     public E higher(E e)
  146.     {
  147.         return treeSet.higher(e);
  148.     }
  149.  
  150.     /* Returns true if this set contains no elements. */
  151.     public boolean isEmpty()
  152.     {
  153.         return treeSet.isEmpty();
  154.     }
  155.  
  156.     /* Returns an iterator over the elements in this set in ascending order. */
  157.     public Iterator<E> iterator()
  158.     {
  159.         return treeSet.iterator();
  160.     }
  161.  
  162.     /* Returns the last (highest) element currently in this set. */
  163.     public E last()
  164.     {
  165.         return treeSet.last();
  166.     }
  167.  
  168.     /*
  169.      * Returns the greatest element in this set strictly less than the given
  170.      * element, or null if there is no such element.
  171.      */
  172.     public E lower(E e)
  173.     {
  174.         return treeSet.lower(e);
  175.     }
  176.  
  177.     /*
  178.      * Retrieves and removes the first (lowest) element, or returns null if this
  179.      * set is empty.
  180.      */
  181.     public E pollFirst()
  182.     {
  183.         return treeSet.pollFirst();
  184.     }
  185.  
  186.     /*
  187.      * Retrieves and removes the last (highest) element, or returns null if this
  188.      * set is empty.
  189.      */
  190.     public E pollLast()
  191.     {
  192.         return treeSet.pollLast();
  193.     }
  194.  
  195.     /* Removes the specified element from this set if it is present. */
  196.     public boolean remove(Object o)
  197.     {
  198.         return treeSet.remove(o);
  199.     }
  200.  
  201.     /* Returns the number of elements in this set (its cardinality). */
  202.     public int size()
  203.     {
  204.         return treeSet.size();
  205.     }
  206.  
  207.     /*
  208.      * Returns a view of the portion of this set whose elements range from
  209.      * fromElement to toElement.
  210.      */
  211.     public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,	E toElement, boolean toInclusive)
  212.     {
  213.         return treeSet.subSet(fromElement, fromInclusive, toElement,toInclusive);
  214.     }
  215.  
  216.     /*
  217.      * Returns a view of the portion of this set whose elements range from
  218.      * fromElement, inclusive, to toElement, exclusive.
  219.      */
  220.     public SortedSet<E> subSet(E fromElement, E toElement)
  221.     {
  222.         return treeSet.subSet(fromElement, toElement);
  223.     }
  224.  
  225.     /*
  226.      * Returns a view of the portion of this set whose elements are greater than
  227.      * or equal to fromElement.
  228.      */
  229.     public SortedSet<E> tailSet(E fromElement)
  230.     {
  231.         return treeSet.tailSet(fromElement);
  232.     }
  233.  
  234.     /*
  235.      * Returns a view of the portion of this set whose elements are greater than
  236.      * (or equal to, if inclusive is true) fromElement.
  237.      */
  238.     public NavigableSet<E> tailSet(E fromElement, boolean inclusive)
  239.     {
  240.         return treeSet.tailSet(fromElement, inclusive);
  241.     }
  242.  
  243.     public static void main(String... arg)
  244.     {
  245.         TreeSetImpl<Integer> treeSet = new TreeSetImpl<Integer>();
  246.  
  247.         treeSet.add(10);
  248.         treeSet.add(20);
  249.         treeSet.add(30);
  250.         treeSet.add(40);
  251.         treeSet.add(5);
  252.         treeSet.add(-10);
  253.  
  254.         Iterator<Integer> titerator = treeSet.iterator();
  255.         System.out.println("the elements are");
  256.         while (titerator.hasNext())
  257.         {
  258.             System.out.print(titerator.next() + "\t");
  259.         }
  260.         System.out.println();
  261.  
  262.         System.out.println("the size of the TreeSet is " + treeSet.size());
  263.  
  264.         System.out.println("the ceiling of -100 in the TreeSet is "+ treeSet.ceiling(-100));
  265.  
  266.         Iterator<Integer> revitr = treeSet.descendingIterator();
  267.         System.out.println("the reverse order is ");
  268.         while (revitr.hasNext())
  269.         {
  270.             System.out.print(revitr.next() + "\t");
  271.         }
  272.         System.out.println();
  273.  
  274.         System.out.println("the lowest element in the set is " + treeSet.first());
  275.         System.out.println("the floor of 20 in treeSet is " + treeSet.floor(20));
  276.  
  277.         System.out.println("the last element in the set is " + treeSet.last());
  278.         System.out.println("element 20 removed is " + treeSet.remove(20));
  279.  
  280.         if (treeSet.isEmpty())
  281.             System.out.println("the set is empty");
  282.         else
  283.             System.out.println("the set is not empty");
  284.  
  285.         NavigableSet<Integer> navigableSet;
  286.         Iterator<Integer> itr;
  287.  
  288.         System.out.println("the headSet of 40 is  ");
  289.         navigableSet = treeSet.headSet(40, true);
  290.         itr = navigableSet.iterator();
  291.  
  292.         while (itr.hasNext())
  293.         {
  294.             System.out.println(itr.next() + "\t");
  295.         }
  296.         System.out.println();
  297.  
  298.         System.out.println("the subset between 5 and 30 ");
  299.         navigableSet = treeSet.subSet(5, true, 30, true);
  300.         itr = navigableSet.iterator();
  301.         while (itr.hasNext())
  302.         {
  303.             System.out.println(itr.next() + "\t");
  304.         }
  305.         System.out.println();
  306.  
  307.         System.out.println("the tailSet of 10 is  ");
  308.         navigableSet = treeSet.tailSet(10, true);
  309.         itr = navigableSet.iterator();
  310.         while (itr.hasNext())
  311.         {
  312.             System.out.println(itr.next() + "\t");
  313.         }
  314.         System.out.println();
  315.     }
  316. }


$javac TreeSetImpl.java
$java TreeSetImpl
the elements are
-10	5	10	20	30	40	
the size of the TreeSet is 6
the ceiling of -100 in the TreeSet is -10
the reverse order is 
40	30	20	10	5	-10	
the lowest element in the set is -10
the floor of 20 in treeSet is 20
the last element in the set is 40
element 20 removed is true
the set is not empty
the headSet of 40 is  
-10	
5	
10	
30	
40	
 
the subset between 5 and 30 
5	
10	
30	
 
the tailSet of 10 is  
10	
30	
40

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.