Advertisement
Luninariel

Binary Tree - WIP

Apr 12th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.20 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. /**
  11.  * This Program Will Perform The Following:
  12.  * A Series of numbers are selected.
  13.  * Integers: 48,12,-31,75,16,89,69,-28, & 17
  14.  * Doubles: 73.2, 14.3, 19.2, 85.6, 92.7, -25.3, 17.2, 100.8 & 29.4
  15.  * The Numbers are added to instances of MyBinaryTreeMgr for their respective Data types.
  16.  * A Binary Tree is Formed by making the first number the Root of The tree.
  17.  * Numbers greater in value than the root, move to the right "Leaf" of the tree.
  18.  * Numbers less in value than the root, move to the left "Leaf" of the tree.
  19.  * Once the numbers are added, the tree can be traversed either InOrder, PreOrder, or PostOrder.
  20.  * Inorder Follows the rule of Left - Root - Right.
  21.  * PreOrder Follows the rule of Root - Left - Right.
  22.  * PostOrder Follows the rule of Left - Right - Root.
  23.  * A print out of each tree in each form of Traversal is provided.
  24.  */
  25.  
  26.  
  27. public class BinaryTreeNodeFinder {
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.  
  31.         //Create a print write for documentation to a file and printing assistance
  32.         PrintWriter outpt;
  33.         outpt = new PrintWriter(new File("JavaAbsOut6.txt"));
  34.  
  35.         //Create a new instance of the Binary Tree Manager to handle Integers.
  36.         MyBinaryTreeMgr<Integer> BtreeInt = new MyBinaryTreeMgr<Integer>();
  37.  
  38.  
  39.         //Add Integers to the Binary Tree BtreeInt
  40.         BtreeInt.insertnode(48);
  41.         BtreeInt.insertnode(12);
  42.         BtreeInt.insertnode(-31);
  43.         BtreeInt.insertnode(75);
  44.         BtreeInt.insertnode(16);
  45.         BtreeInt.insertnode(89);
  46.         BtreeInt.insertnode(69);
  47.         BtreeInt.insertnode(-28);
  48.         BtreeInt.insertnode(17);
  49.  
  50.  
  51.         //Print the Binary Tree BTreeInt InOrder.
  52.         System.out.println("\nPrinting this tree InOrder Left-Root-Right \n");
  53.         BtreeInt.inorder(outpt);
  54.  
  55.         //Print the binary Tree BTreeInt in PreOrder
  56.         System.out.println("\nPrinting this tree In Preorder Root-Left-Right \n");
  57.         BtreeInt.PreOrder(outpt);
  58.  
  59.         //Print the Binary Tree BtreeInt in PostOrder
  60.         System.out.println("\n Printing this tree in PostOrder Left-Right-Root");
  61.         BtreeInt.PostOrder(outpt);
  62.  
  63.         //Create a new instance of the Binary Tree Manager to handle Doubles
  64.         MyBinaryTreeMgr<Double> BtreeDoub = new MyBinaryTreeMgr<Double>();
  65.  
  66.         //Add Doubles to the Binary Tree BtreeDoub
  67.         BtreeDoub.insertnode(73.2);
  68.         BtreeDoub.insertnode(14.3);
  69.         BtreeDoub.insertnode(19.2);
  70.         BtreeDoub.insertnode(85.6);
  71.         BtreeDoub.insertnode(92.7);
  72.         BtreeDoub.insertnode(-25.3);
  73.         BtreeDoub.insertnode(-17.2);
  74.         BtreeDoub.insertnode(100.8);
  75.         BtreeDoub.insertnode(29.4);
  76.  
  77.  
  78.         //Print the Binary Tree BtreeDoub InOrder
  79.         System.out.println("\n Printing this tree inorder Left-Root-Right\n");
  80.         BtreeDoub.inorder(outpt);
  81.  
  82.         //Print the binary Tree Btreedoub in PreOrder
  83.         System.out.println("\nPrinting this tree In Preorder Root-Left-Right \n");
  84.         BtreeDoub.PreOrder(outpt);
  85.  
  86.         //Print the Binary Tree Btreedoub in PostOrder
  87.         System.out.println("\n Printing this tree in PostOrder Left-Right-Root");
  88.         BtreeDoub.PostOrder(outpt);
  89.         outpt.close();
  90.  
  91.     }
  92. }
  93.  
  94. //The Class MyBinaryTreeMgr, Generically handles Binary Trees
  95. class MyBinaryTreeMgr<T extends Comparable<T>> {
  96.     protected TreeNode<T> root;
  97.     protected int number;
  98.     protected T nodevalue;
  99.     protected TreeNode<T> left;
  100.     protected TreeNode<T> right;
  101.  
  102.     //Inner Class for a single Tree Node
  103.     private static class TreeNode<T extends Comparable> {
  104.         protected T nodevalue;
  105.         protected TreeNode<T> left;
  106.         protected TreeNode<T> right;
  107.  
  108.         public TreeNode(T x) {
  109.             nodevalue = x;
  110.             left = null;
  111.             right = null;
  112.         }
  113.  
  114.     }
  115.  
  116.     protected TreeNode<T> foundnode;
  117.     protected TreeNode<T> foundleftchild;
  118.     protected TreeNode<T> foundrightchild;
  119.     protected TreeNode<T> foundnodeparent;
  120.     protected TreeNode<T> currentnode;
  121.     protected TreeNode<T> currentleft;
  122.     protected TreeNode<T> currentright;
  123.     protected TreeNode<T> parentnode;
  124.  
  125.     public MyBinaryTreeMgr() {
  126.         root = null;
  127.         int number = 0;
  128.     }
  129.  
  130.     //Getter to find the number of nodes in a tree.
  131.     public int getnumber() {
  132.         return number;
  133.     }
  134.  
  135.     //Method to find and print the parent node of a found node.
  136.     public void printparent() {
  137.         if (foundnodeparent == null) {
  138.             System.out.println("Parent null");
  139.         } else {
  140.             System.out.println("Parent " + foundnodeparent.nodevalue);
  141.         }
  142.     }
  143.  
  144.     //Method to find and print the left node of a found node.
  145.     public void printleft() {
  146.         if (foundleftchild == null) {
  147.             System.out.println("Left Child null");
  148.         } else {
  149.             System.out.println("Left Child " + foundleftchild.nodevalue);
  150.         }
  151.     }
  152.  
  153.     //Method to find and print the right node of a found node.
  154.     public void printright() {
  155.         if (foundrightchild == null) {
  156.             System.out.println("Right child null");
  157.         } else {
  158.             System.out.println("Right Child " + foundrightchild.nodevalue);
  159.         }
  160.     }
  161.  
  162.     //Method used to create a node within the binary tree
  163.     protected TreeNode<T> CreateNode(T x) {
  164.         return new TreeNode(x);
  165.     }
  166.  
  167.     //Method to insert a value into the binary tree.
  168.     public int insertnode(T x) {
  169.         if (root == null) {
  170.             root = CreateNode(x);
  171.             number = 1;
  172.             return number;
  173.         } else {
  174.             TreeNode<T> parent = null;
  175.             TreeNode<T> current = root;
  176.             while (current != null)
  177.                 if (x.compareTo(current.nodevalue) < 0) {
  178.                     parent = current;
  179.                     current = current.left;
  180.                 } else if (x.compareTo(current.nodevalue) > 0) {
  181.                     parent = current;
  182.                     current = current.right;
  183.                 } else return -99;
  184.  
  185.             if (x.compareTo(parent.nodevalue) < 0) {
  186.                 parent.left = CreateNode(x);
  187.             } else parent.right = CreateNode(x);
  188.         }
  189.         number++;
  190.         return number;
  191.     }
  192.  
  193.     //Method to Find a Node within a Binary Tree.
  194.     public int FindNode(T x) {
  195.         int Nsearch = 0;
  196.         System.out.println("In Find Node looking for value " + x);
  197.         if (number == 0) {
  198.             Nsearch = 0;
  199.             return Nsearch;
  200.         } else {
  201.             if (x.compareTo(root.nodevalue) == 0) {
  202.                 System.out.println("Checking root node with value " + root.nodevalue);
  203.                 Nsearch = 2;
  204.                 foundnodeparent = parentnode = root;
  205.                 foundleftchild = root.left;
  206.                 foundrightchild = root.right;
  207.                 return Nsearch;
  208.             } else {
  209.                 parentnode = root;
  210.                 currentnode = root;
  211.                 currentleft = root.left;
  212.                 currentright = root.right;
  213.                 System.out.println("Findnode not root node right");
  214.             }
  215.             while (x.compareTo(currentnode.nodevalue) != 0) {
  216.                 System.out.println("We are searching currentnodevalue " + currentnode.nodevalue + " Value of X" + x);
  217.  
  218.                 if (x.compareTo(currentnode.nodevalue) > 0) {
  219.                     parentnode = currentnode;
  220.                     System.out.println("Chaining Right " + currentnode.nodevalue);
  221.                     if (currentnode.right == null) {
  222.                         Nsearch = 0;
  223.                         System.out.println("currentnode.right is null");
  224.                         return Nsearch;
  225.                     }
  226.                     currentnode = currentnode.right;
  227.                     System.out.println("We Move Right " + currentnode.nodevalue);
  228.                 } else {
  229.                     foundnodeparent = parentnode = currentnode;
  230.                     if (currentnode.left == null) {
  231.                         Nsearch = 0;
  232.                         return Nsearch;
  233.                     }
  234.                     currentnode = currentnode.left;
  235.                 }
  236.                 System.out.println("At end of chaining if currentnode value " + currentnode.nodevalue +
  237.                         "Value of x " + x);
  238.             }
  239.             System.out.println("We have found the value ");
  240.             foundleftchild = currentnode.left;
  241.             foundrightchild = currentnode.right;
  242.             Nsearch = 1;
  243.         }
  244.         return Nsearch;
  245.     }
  246.  
  247.     //Methods to print a Binary Tree InOrder
  248.     public void inorder(PrintWriter outp) {
  249.         //Start From Root Node
  250.         inorder(root, outp);
  251.     }
  252.  
  253.     protected void inorder(TreeNode<T> root, PrintWriter outp) {
  254.         if (root == null) return;
  255.         //Left
  256.         inorder(root.left, outp);
  257.         //Root
  258.         System.out.println(root.nodevalue + " ");
  259.         outp.print(root.nodevalue + " ");
  260.         //Right
  261.         inorder(root.right, outp);
  262.     }
  263.  
  264.     //Methods to print a Binary Tree in PreOrder
  265.     public void PreOrder(PrintWriter outp) {
  266.         // start from Root Node
  267.         PreOrder(root, outp);
  268.     }
  269.  
  270.     protected void PreOrder(TreeNode<T> root, PrintWriter outp) {
  271.         if (root == null) return;
  272.         // Root
  273.         System.out.println(root.nodevalue + " ");
  274.         outp.print(root.nodevalue + " ");
  275.  
  276.         // Left
  277.         PreOrder(root.left, outp);
  278.  
  279.         // Right
  280.         PreOrder(root.right, outp);
  281.     }
  282.  
  283.     //Methods to print a Binary Tree in Post Order
  284.     public void PostOrder(PrintWriter outp) {
  285.         // start from root node
  286.         PostOrder(root, outp);
  287.     }
  288.  
  289.     protected void PostOrder(TreeNode<T> root, PrintWriter outp) {
  290.         if (root == null) return;
  291.         // Left
  292.         PostOrder(root.left, outp);
  293.  
  294.         // Right
  295.         PostOrder(root.right, outp);
  296.  
  297.         // Root
  298.         System.out.println(root.nodevalue + " ");
  299.         outp.print(root.nodevalue + " ");
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement