Answers for "treeset in java"

5

trees in java

package MyPackage;
  
public class Tree { 
    static class Node {    
    int value; 
        Node left, right; 
          
        Node(int value){ 
            this.value = value; 
            left = null; 
            right = null; 
        } 
    } 
       
    public void insert(Node node, int value) {
        if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of " + node.value); node.left = new Node(value); } } else if (value > node.value) {
          if (node.right != null) {
            insert(node.right, value);
          } else {
            System.out.println("  Inserted " + value + " to right of "
                + node.value);
            node.right = new Node(value);
          }
        }
      }
     public void traverseInOrder(Node node) {
        if (node != null) {
            traverseInOrder(node.left);
            System.out.print(" " + node.value);
            traverseInOrder(node.right);
        }
     }
     
     public static void main(String args[]) 
    { 
    Tree tree = new Tree();
                Node root = new Node(5);
                System.out.println("Binary Tree Example");
                System.out.println("Building tree with root value " + root.value);
                tree.insert(root, 2);
                tree.insert(root, 4);
                tree.insert(root, 8);
                tree.insert(root, 6);
                tree.insert(root, 7);
                tree.insert(root, 3);
                tree.insert(root, 9);
                System.out.println("Traversing tree in order");
                tree.traverseLevelOrder();
                
              }
}
Posted by: Guest on May-03-2020
0

TreeSet in java

// java treeset example
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetExamples
{
   public static void main(String[] args)
   {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("red");
      ts.add("blue");
      ts.add("green");
      ts.add("red");
      Iterator<String> iterate = ts.iterator();
      while(iterate.hasNext())
      {
         System.out.println(iterate.next());
      }
   }
}
Posted by: Guest on November-26-2020
0

java treeset

import java.util.*;

// Declare the variable using the interface of the object for flexibility.
// Non-primative data types only.
Set<String> movies = new TreeSet<String>();

movies.add(E);
// TreeSet will be sorted by element.
// Work with any comparable object.

movies.add("The Shining");
movies.add("Dawn of the Dead");
movies.add("Zombieland");
Posted by: Guest on March-16-2020
0

treeset in java

- TreeSet sorts the order and don't accept null
Posted by: Guest on January-06-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language