Advertisement
Luninariel

Binary Tree - Example

Apr 12th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.03 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.IOException;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.lang.*;
  8. import java.util.NoSuchElementException;
  9.  
  10. public class BinaryTreeNodeFinder {
  11.  
  12.     public static void main(String[] args)throws Exception {
  13.         PrintWriter outpt;
  14.         outpt = new PrintWriter(new File("JavaAbsOut6.txt"));
  15.  
  16.         MyBinaryTreeMgr<Integer> BtreeInt = new MyBinaryTreeMgr<Integer>();
  17.         BtreeInt.insertnode(65);
  18.         BtreeInt.insertnode(32);
  19.         BtreeInt.insertnode(75);
  20.         BtreeInt.insertnode(16);
  21.         BtreeInt.insertnode(45);
  22.         BtreeInt.insertnode(81);
  23.         System.out.println("This is the binary tree test there are " + BtreeInt.getnumber());
  24.         outpt.println("This is the binary tree test there are " + BtreeInt.getnumber());
  25.         System.out.println("Printing this tree inorder left-middle-right ");
  26.         int x;
  27.         System.out.println("\n Now lets find some nodes\n");
  28.         System.out.println("First the root node ");
  29.         x = 65;
  30.         System.out.println("Lets find a node " + x + " Node finder returns " + BtreeInt.FindNode(x));
  31.         System.out.println("\nParent node has value ");  BtreeInt.printparent();
  32.         System.out.println("Left node has value "); BtreeInt.printleft();
  33.         System.out.println("Right node has value "); BtreeInt.printright();
  34.         x = 65;
  35.         System.out.println("Lets find a node " + x + " Node finder returns "); BtreeInt.FindNode(x);
  36.         System.out.println("\nParent node has value " ); BtreeInt.printparent();
  37.         System.out.println("Left node has value " ); BtreeInt.printleft();
  38.         System.out.println("Right node has value "); BtreeInt.printright();
  39.         x = 16;
  40.         System.out.println("Lets find a node " + x + " Node finder returns "); BtreeInt.FindNode(x);
  41.         System.out.println("\nParent node has value "); BtreeInt.printparent();
  42.         System.out.println("Left node has value "); BtreeInt.printleft();
  43.         System.out.println("Right node has value "); BtreeInt.printright();
  44.  
  45.         BtreeInt.inorder(outpt);
  46.  
  47.         BtreeInt.insertnode(78);
  48.         System.out.println("This is the binary tree test there are " + BtreeInt.getnumber());
  49.         outpt.println("This is the binary tree test there are " + BtreeInt.getnumber());
  50.         System.out.println("Printing this tree inorder left-middle-right ");
  51.         BtreeInt.inorder(outpt);
  52.  
  53.         MyBinaryTreeMgr<Double> BtreeDoub = new MyBinaryTreeMgr<Double>();
  54.  
  55.         BtreeDoub.insertnode(32.9);
  56.         BtreeDoub.insertnode(47.3);
  57.         BtreeDoub.insertnode(89.4);
  58.         BtreeDoub.insertnode(15.3);
  59.         BtreeDoub.insertnode(2.5);
  60.         BtreeDoub.insertnode(103.2);
  61.         BtreeDoub.insertnode(18.1);
  62.         BtreeDoub.insertnode(16.2);
  63.  
  64.         System.out.println("\nThis is the binary tree test there are " + BtreeDoub.getnumber());
  65.         outpt.println("This is the binary tree test there are " + BtreeDoub.getnumber());
  66.         System.out.println("Printing this tree inorder left-middle-right\n");
  67.  
  68.         Double x1 = 47.3;
  69.         System.out.println("Lets find a node " + x1 + " Node finder returns " + BtreeDoub.FindNode(x1));
  70.         System.out.println("Parent node has value "); BtreeDoub.printparent();
  71.         System.out.println("Left node has value "); BtreeDoub.printleft();
  72.         System.out.println("Right node has value "); BtreeDoub.printright();
  73.  
  74.         x1 = 15.3;
  75.         System.out.println("Lets find a node " + x1 + " Node finder returns " + BtreeDoub.FindNode(x1));
  76.         System.out.println("Parent node has value "); BtreeDoub.printparent();
  77.         System.out.println("Left node has value "); BtreeDoub.printleft();
  78.         System.out.println("Right node has value "); BtreeDoub.printright();
  79.  
  80.         BtreeDoub.inorder(outpt);
  81.         outpt.close();
  82.  
  83.     }
  84. }
  85.  
  86. class MyBinaryTreeMgr<T extends Comparable>{
  87.     protected TreeNode<T> root;
  88.     protected int number;
  89.     protected T nodevalue;
  90.     protected TreeNode<T> left;
  91.     protected TreeNode<T> right;
  92.  
  93.     private static class TreeNode<T extends Comparable> {
  94.         protected T nodevalue;
  95.         protected TreeNode<T> left;
  96.         protected TreeNode<T> right;
  97.  
  98.         public TreeNode(T x) {
  99.             nodevalue = x;
  100.             left = null;
  101.             right = null;
  102.         }
  103.  
  104.     }
  105.  
  106.     protected TreeNode<T> foundnode;
  107.     protected TreeNode<T> foundleftchild;
  108.     protected TreeNode<T> foundrightchild;
  109.     protected TreeNode<T> foundnodeparent;
  110.     protected TreeNode<T> currentnode;
  111.     protected TreeNode<T> currentleft;
  112.     protected TreeNode<T> currentright;
  113.     protected TreeNode<T> parentnode;
  114.  
  115.     public MyBinaryTreeMgr() {
  116.         root = null;
  117.         int number = 0;
  118.     }
  119.  
  120.     public int getnumber() {
  121.         return number;
  122.     }
  123.  
  124.     public void printparent() {
  125.         if (foundnodeparent == null) {
  126.             System.out.println("Parent null");
  127.         } else {
  128.             System.out.println("Parent " + foundnodeparent.nodevalue);
  129.         }
  130.     }
  131.  
  132.     public void printleft() {
  133.         if (foundleftchild == null) {
  134.             System.out.println("Left Child null");
  135.         } else {
  136.             System.out.println("Left Child " + foundleftchild.nodevalue);
  137.         }
  138.     }
  139.  
  140.     public void printright() {
  141.         if (foundrightchild == null) {
  142.             System.out.println("Right child null");
  143.         } else {
  144.             System.out.println("Right Child " + foundrightchild.nodevalue);
  145.         }
  146.     }
  147.  
  148.     protected TreeNode<T> CreateNode(T x) {
  149.         return new TreeNode(x);
  150.     }
  151.  
  152.     public int insertnode(T x) {
  153.         if (root == null) {
  154.             root = CreateNode(x);
  155.             number = 1;
  156.             return number;
  157.         }
  158.         else {
  159.             TreeNode<T> parent = null;
  160.             TreeNode<T> current = root;
  161.             while (current != null)
  162.                 if (x.compareTo(current.nodevalue) < 0) {
  163.                     parent = current;
  164.                     current = current.left;
  165.                 } else if (x.compareTo(current.nodevalue) > 0) {
  166.                     parent = current;
  167.                     current = current.right;
  168.                 } else return -99;
  169.  
  170.             if (x.compareTo(parent.nodevalue) < 0) {
  171.                 parent.left = CreateNode(x);
  172.             } else parent.right = CreateNode(x);
  173.         }
  174.         number++;
  175.         return number;
  176.     }
  177.     public int FindNode(T x){
  178.         int Nsearch = 0;
  179.         System.out.println("In Find Node looking for value "+x);
  180.         if (number==0){
  181.             Nsearch = 0;
  182.             return Nsearch;
  183.         }
  184.         else {
  185.             if(x.compareTo(root.nodevalue) == 0){
  186.                 System.out.println("Checking root node with value "+ root.nodevalue);
  187.                 Nsearch = 2;
  188.                 foundnodeparent = parentnode = root;
  189.                 foundleftchild = root.left;
  190.                 foundrightchild = root.right;
  191.                 return Nsearch;
  192.             }
  193.             else {
  194.                 parentnode = root;
  195.                 currentnode = root;
  196.                 currentleft = root.left;
  197.                 currentright = root.right;
  198.                 System.out.println("Findnode not root node right");
  199.             }
  200.             while (x.compareTo(currentnode.nodevalue) != 0){
  201.                 System.out.println("We are searching currentnodevalue "+ currentnode.nodevalue + " Value of X" + x);
  202.  
  203.                 if (x.compareTo(currentnode.nodevalue)>0){
  204.                     parentnode = currentnode;
  205.                     System.out.println("Chaining Right "+ currentnode.nodevalue);
  206.                     if (currentnode.right==null){
  207.                         Nsearch = 0;
  208.                         System.out.println("currentnode.right is null");
  209.                         return Nsearch;
  210.                     }
  211.                     currentnode = currentnode.right;
  212.                     System.out.println("We Move Right "+ currentnode.nodevalue);
  213.                 }
  214.                 else {
  215.                     foundnodeparent = parentnode = currentnode;
  216.                     if (currentnode.left==null){
  217.                         Nsearch=0;
  218.                         return Nsearch;
  219.                     }
  220.                     currentnode = currentnode.left;
  221.                 }
  222.                 System.out.println("At end of chaining if currentnode value "+ currentnode.nodevalue +
  223.                         "Value of x "+x);
  224.             }
  225.             System.out.println("We have found the value ");
  226.             foundleftchild = currentnode.left;
  227.             foundrightchild = currentnode.right;
  228.             Nsearch = 1;
  229.         }
  230.         return  Nsearch;
  231.     }
  232.     public  void inorder(PrintWriter outp){
  233.         inorder(root,outp);
  234.     }
  235.     protected void inorder(TreeNode<T> root, PrintWriter outp){
  236.         if (root == null)return;
  237.         inorder(root.left, outp);
  238.         System.out.println(root.nodevalue+" ");
  239.         outp.print(root.nodevalue+" ");
  240.         inorder(root.right,outp);
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement