Binary tree test program. - Java Data Structure

Java examples for Data Structure:Tree

Description

Binary tree test program.

Demo Code

import java.security.SecureRandom;

class TreeNode<T extends Comparable<T>> 
{
   TreeNode<T> leftNode; /*  w ww.j a  v a2s.  c o m*/
   T data; // node value
   TreeNode<T> rightNode; 

   public TreeNode(T nodeData)
   { 
      data = nodeData;              
      leftNode = rightNode = null; // node has no children
   } 

   // locate insertion point and insert new node; ignore duplicate values
   public void insert(T insertValue)
   {
      // insert in left subtree
      if (insertValue.compareTo(data) < 0) 
      {
         // insert new TreeNode
         if (leftNode == null)
            leftNode = new TreeNode<T>(insertValue);
         else // continue traversing left subtree recursively
            leftNode.insert(insertValue); 
      }
      // insert in right subtree
      else if (insertValue.compareTo(data) > 0) 
      {
         // insert new TreeNode
         if (rightNode == null)
            rightNode = new TreeNode<T>(insertValue);
         else // continue traversing right subtree recursively
            rightNode.insert(insertValue); 
      } 
   } 
} 

// class Tree definition
class Tree<T extends Comparable<T>>
{
   private TreeNode<T> root;

   // constructor initializes an empty Tree of integers
   public Tree() 
   { 
      root = null; 
   } 

   // insert a new node in the binary search tree
   public void insertNode(T insertValue)
   {
      if (root == null)
         root = new TreeNode<T>(insertValue); // create root node
      else
         root.insert(insertValue); // call the insert method
   } 

   // begin preorder traversal
   public void preorderTraversal()
   { 
      preorderHelper(root); 
   } 

   // recursive method to perform preorder traversal
   private void preorderHelper(TreeNode<T> node)
   {
      if (node == null)
         return;

      System.out.printf("%s ", node.data); // output node data
      preorderHelper(node.leftNode); // traverse left subtree
      preorderHelper(node.rightNode); // traverse right subtree
   } 

   // begin inorder traversal
   public void inorderTraversal()
   { 
      inorderHelper(root); 
   } 

   // recursive method to perform inorder traversal
   private void inorderHelper(TreeNode<T> node)
   {
      if (node == null)
         return;

      inorderHelper(node.leftNode); // traverse left subtree
      System.out.printf("%s ", node.data); // output node data
      inorderHelper(node.rightNode); // traverse right subtree
   } 

   // begin postorder traversal
   public void postorderTraversal()
   { 
      postorderHelper(root); 
   } 

   // recursive method to perform postorder traversal
   private void postorderHelper(TreeNode<T> node)
   {
      if (node == null)
         return;
  
      postorderHelper(node.leftNode); // traverse left subtree
      postorderHelper(node.rightNode); // traverse right subtree
      System.out.printf("%s ", node.data); // output node data
   } 
}


public class Main 
{
   public static void main(String[] args)
   {
      Tree<Integer> tree = new Tree<Integer>();
      SecureRandom randomNumber = new SecureRandom();

      System.out.println("Inserting the following values: ");

      // insert 10 random integers from 0-99 in tree 
      for (int i = 1; i <= 10; i++) 
      {
         int value = randomNumber.nextInt(100);
         System.out.printf("%d ", value);
         tree.insertNode(value);
      } 

      System.out.printf("%n%nPreorder traversal%n");
      tree.preorderTraversal(); 

      System.out.printf("%n%nInorder traversal%n");
      tree.inorderTraversal(); 

      System.out.printf("%n%nPostorder traversal%n");
      tree.postorderTraversal(); 
      System.out.println();
   } 
}

Result


Related Tutorials