Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Definition of the node
  2. class BinaryTreeNode
  3. {
  4.     //Instance Variable
  5.     BinaryTreeNode left;
  6.     int data;
  7.     BinaryTreeNode right;
  8.     //Constructor
  9.     public BinaryTreeNode(int data)
  10.     {
  11.         this.data=data;
  12.         left=null;
  13.         right=null;
  14.     }
  15. }
  16. //Definition of Binary Tree
  17. public class BinaryTree
  18. {
  19.     //Instance variable
  20.     protected BinaryTreeNode root;
  21.     //Instance method and constructors
  22.     public BinaryTree()
  23.     //default constructor
  24.     //postcondition:root=null
  25.     {
  26.         //write the method definition here
  27.         root=null;
  28.     }
  29.     public void destroyTree()
  30.     //method to destroy the binary tree
  31.     //postcondition:root=null
  32.     {
  33.         root=null;
  34.         System.gc();
  35.        
  36.     }
  37.     public Boolean isEmpty()
  38.     //method to determine whether the binary tree is empty.
  39.     //postcondition:Returns true if binary tree is empty, false
  40.     //otherwise
  41.     {
  42.         return(root==null);
  43.     }
  44.     public int treeHeight()
  45.     //method to determine the height of the binary tree.
  46.     //postcondition:the height of the binary tree is returned
  47.     {
  48.         return height(root);
  49.     }
  50.    
  51.     private int height(BinaryTreeNode root){
  52.         if(root == null)
  53.             return 0;
  54.         else
  55.             return 1 + max(height(root.left),height(root.right));
  56.     }
  57.     private int max(int l,int r){
  58.         if(l>r){
  59.             return l;
  60.         }
  61.         else{
  62.             return r;
  63.         }
  64.     }
  65.    
  66.     public void insertNode(int item)
  67.     //method to insert node in the binary tree.
  68.     //postcondition: A node is created and inserted in the binary tree.
  69.     {
  70.         if(root==null){
  71.             root=new BinaryTreeNode(item);
  72.         }
  73.         else{
  74.             root=insertNode(item,root);
  75.         }
  76.     }
  77.    
  78.     private BinaryTreeNode insertNode(int item,BinaryTreeNode root)
  79.     {
  80.         if(root==null){
  81.             root=new BinaryTreeNode(item);
  82.         }
  83.         else if(item<root.data){
  84.                 root.left=insertNode(item,root.left);
  85.         }
  86.         else if(item>root.data){
  87.                 root.right=insertNode(item,root.right);
  88.            
  89.         }
  90.         return root;
  91.     }
  92.     public void inorderTraversal()
  93.     //method to do an inorder traversal of the binary tree
  94.     //postcondition: the nodes of the binary tree are output in the
  95.     //inorder sequence
  96.     {
  97.         inorderTraversal(root);
  98.         System.out.println();
  99.     }
  100.     private void inorderTraversal(BinaryTreeNode root){
  101.         if(root!=null){
  102.             inorderTraversal(root.left);
  103.             System.out.print(root.data+" ");
  104.             inorderTraversal(root.right);
  105.            
  106.         }
  107.     }
  108.     public void preorderTraversal()
  109.     //method to do a preorder traversal of the binary tree.
  110.     //postcondition:The nodes of the binary tree are output in the
  111.     //preorder sequence.
  112.     {
  113.         preorderTraversal(root);
  114.         System.out.println();
  115.     }
  116.     private void preorderTraversal(BinaryTreeNode root){
  117.         if(root!=null){
  118.             System.out.print(root.data+" ");
  119.             preorderTraversal(root.left);
  120.             preorderTraversal(root.right);
  121.            
  122.         }
  123.     }
  124.     public void postorderTraversal()
  125.     //method to do a postorder traversal of the binary tree.
  126.     //postcondition:The nodes of the binary tree are output in the
  127.     //postorder sequence.
  128.     {
  129.         postorderTraversal(root);
  130.         System.out.println();
  131.     }
  132.     private void postorderTraversal(BinaryTreeNode root){
  133.         if(root!=null){
  134.             postorderTraversal(root.left);
  135.             postorderTraversal(root.right);
  136.             System.out.print(root.data+" ");          
  137.         }
  138.     }
  139.     public int leaveCount(){
  140.         return leaveCount(root);
  141.     }
  142.     private int leaveCount(BinaryTreeNode root){
  143.         if(root==null){
  144.             return 0;
  145.         }
  146.         else if(root.left==null&&root.right==null){
  147.             return 1;
  148.         }
  149.         else{
  150.             return leaveCount(root.left)+leaveCount(root.right);
  151.         }
  152.     }
  153.     public int nodeCount(){
  154.         return nodeCount(root);
  155.     }
  156.     public int nodeCount(BinaryTreeNode root){
  157.         if(root==null){
  158.             return 0;
  159.         }
  160.         else{
  161.             return nodeCount(root.left)+nodeCount(root.right)+1;
  162.         }
  163.        
  164.     }
  165. }