Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.IOException;
- import javax.swing.*;
- import java.util.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.lang.*;
- import java.util.NoSuchElementException;
- /**
- * This Program Will Perform The Following:
- * A Series of numbers are selected.
- * Integers: 48,12,-31,75,16,89,69,-28, & 17
- * Doubles: 73.2, 14.3, 19.2, 85.6, 92.7, -25.3, 17.2, 100.8 & 29.4
- * The Numbers are added to instances of MyBinaryTreeMgr for their respective Data types.
- * A Binary Tree is Formed by making the first number the Root of The tree.
- * Numbers greater in value than the root, move to the right "Leaf" of the tree.
- * Numbers less in value than the root, move to the left "Leaf" of the tree.
- * Once the numbers are added, the tree can be traversed either InOrder, PreOrder, or PostOrder.
- * Inorder Follows the rule of Left - Root - Right.
- * PreOrder Follows the rule of Root - Left - Right.
- * PostOrder Follows the rule of Left - Right - Root.
- * A print out of each tree in each form of Traversal is provided.
- */
- public class BinaryTreeNodeFinder {
- public static void main(String[] args) throws Exception {
- //Create a print write for documentation to a file and printing assistance
- PrintWriter outpt;
- outpt = new PrintWriter(new File("JavaAbsOut6.txt"));
- //Create a new instance of the Binary Tree Manager to handle Integers.
- MyBinaryTreeMgr<Integer> BtreeInt = new MyBinaryTreeMgr<Integer>();
- //Add Integers to the Binary Tree BtreeInt
- BtreeInt.insertnode(48);
- BtreeInt.insertnode(12);
- BtreeInt.insertnode(-31);
- BtreeInt.insertnode(75);
- BtreeInt.insertnode(16);
- BtreeInt.insertnode(89);
- BtreeInt.insertnode(69);
- BtreeInt.insertnode(-28);
- BtreeInt.insertnode(17);
- //Print the Binary Tree BTreeInt InOrder.
- System.out.println("\nPrinting this tree InOrder Left-Root-Right \n");
- BtreeInt.inorder(outpt);
- //Print the binary Tree BTreeInt in PreOrder
- System.out.println("\nPrinting this tree In Preorder Root-Left-Right \n");
- BtreeInt.PreOrder(outpt);
- //Print the Binary Tree BtreeInt in PostOrder
- System.out.println("\n Printing this tree in PostOrder Left-Right-Root");
- BtreeInt.PostOrder(outpt);
- //Create a new instance of the Binary Tree Manager to handle Doubles
- MyBinaryTreeMgr<Double> BtreeDoub = new MyBinaryTreeMgr<Double>();
- //Add Doubles to the Binary Tree BtreeDoub
- BtreeDoub.insertnode(73.2);
- BtreeDoub.insertnode(14.3);
- BtreeDoub.insertnode(19.2);
- BtreeDoub.insertnode(85.6);
- BtreeDoub.insertnode(92.7);
- BtreeDoub.insertnode(-25.3);
- BtreeDoub.insertnode(-17.2);
- BtreeDoub.insertnode(100.8);
- BtreeDoub.insertnode(29.4);
- //Print the Binary Tree BtreeDoub InOrder
- System.out.println("\n Printing this tree inorder Left-Root-Right\n");
- BtreeDoub.inorder(outpt);
- //Print the binary Tree Btreedoub in PreOrder
- System.out.println("\nPrinting this tree In Preorder Root-Left-Right \n");
- BtreeDoub.PreOrder(outpt);
- //Print the Binary Tree Btreedoub in PostOrder
- System.out.println("\n Printing this tree in PostOrder Left-Right-Root");
- BtreeDoub.PostOrder(outpt);
- outpt.close();
- }
- }
- //The Class MyBinaryTreeMgr, Generically handles Binary Trees
- class MyBinaryTreeMgr<T extends Comparable<T>> {
- protected TreeNode<T> root;
- protected int number;
- protected T nodevalue;
- protected TreeNode<T> left;
- protected TreeNode<T> right;
- //Inner Class for a single Tree Node
- private static class TreeNode<T extends Comparable> {
- protected T nodevalue;
- protected TreeNode<T> left;
- protected TreeNode<T> right;
- public TreeNode(T x) {
- nodevalue = x;
- left = null;
- right = null;
- }
- }
- protected TreeNode<T> foundnode;
- protected TreeNode<T> foundleftchild;
- protected TreeNode<T> foundrightchild;
- protected TreeNode<T> foundnodeparent;
- protected TreeNode<T> currentnode;
- protected TreeNode<T> currentleft;
- protected TreeNode<T> currentright;
- protected TreeNode<T> parentnode;
- public MyBinaryTreeMgr() {
- root = null;
- int number = 0;
- }
- //Getter to find the number of nodes in a tree.
- public int getnumber() {
- return number;
- }
- //Method to find and print the parent node of a found node.
- public void printparent() {
- if (foundnodeparent == null) {
- System.out.println("Parent null");
- } else {
- System.out.println("Parent " + foundnodeparent.nodevalue);
- }
- }
- //Method to find and print the left node of a found node.
- public void printleft() {
- if (foundleftchild == null) {
- System.out.println("Left Child null");
- } else {
- System.out.println("Left Child " + foundleftchild.nodevalue);
- }
- }
- //Method to find and print the right node of a found node.
- public void printright() {
- if (foundrightchild == null) {
- System.out.println("Right child null");
- } else {
- System.out.println("Right Child " + foundrightchild.nodevalue);
- }
- }
- //Method used to create a node within the binary tree
- protected TreeNode<T> CreateNode(T x) {
- return new TreeNode(x);
- }
- //Method to insert a value into the binary tree.
- public int insertnode(T x) {
- if (root == null) {
- root = CreateNode(x);
- number = 1;
- return number;
- } else {
- TreeNode<T> parent = null;
- TreeNode<T> current = root;
- while (current != null)
- if (x.compareTo(current.nodevalue) < 0) {
- parent = current;
- current = current.left;
- } else if (x.compareTo(current.nodevalue) > 0) {
- parent = current;
- current = current.right;
- } else return -99;
- if (x.compareTo(parent.nodevalue) < 0) {
- parent.left = CreateNode(x);
- } else parent.right = CreateNode(x);
- }
- number++;
- return number;
- }
- //Method to Find a Node within a Binary Tree.
- public int FindNode(T x) {
- int Nsearch = 0;
- System.out.println("In Find Node looking for value " + x);
- if (number == 0) {
- Nsearch = 0;
- return Nsearch;
- } else {
- if (x.compareTo(root.nodevalue) == 0) {
- System.out.println("Checking root node with value " + root.nodevalue);
- Nsearch = 2;
- foundnodeparent = parentnode = root;
- foundleftchild = root.left;
- foundrightchild = root.right;
- return Nsearch;
- } else {
- parentnode = root;
- currentnode = root;
- currentleft = root.left;
- currentright = root.right;
- System.out.println("Findnode not root node right");
- }
- while (x.compareTo(currentnode.nodevalue) != 0) {
- System.out.println("We are searching currentnodevalue " + currentnode.nodevalue + " Value of X" + x);
- if (x.compareTo(currentnode.nodevalue) > 0) {
- parentnode = currentnode;
- System.out.println("Chaining Right " + currentnode.nodevalue);
- if (currentnode.right == null) {
- Nsearch = 0;
- System.out.println("currentnode.right is null");
- return Nsearch;
- }
- currentnode = currentnode.right;
- System.out.println("We Move Right " + currentnode.nodevalue);
- } else {
- foundnodeparent = parentnode = currentnode;
- if (currentnode.left == null) {
- Nsearch = 0;
- return Nsearch;
- }
- currentnode = currentnode.left;
- }
- System.out.println("At end of chaining if currentnode value " + currentnode.nodevalue +
- "Value of x " + x);
- }
- System.out.println("We have found the value ");
- foundleftchild = currentnode.left;
- foundrightchild = currentnode.right;
- Nsearch = 1;
- }
- return Nsearch;
- }
- //Methods to print a Binary Tree InOrder
- public void inorder(PrintWriter outp) {
- //Start From Root Node
- inorder(root, outp);
- }
- protected void inorder(TreeNode<T> root, PrintWriter outp) {
- if (root == null) return;
- //Left
- inorder(root.left, outp);
- //Root
- System.out.println(root.nodevalue + " ");
- outp.print(root.nodevalue + " ");
- //Right
- inorder(root.right, outp);
- }
- //Methods to print a Binary Tree in PreOrder
- public void PreOrder(PrintWriter outp) {
- // start from Root Node
- PreOrder(root, outp);
- }
- protected void PreOrder(TreeNode<T> root, PrintWriter outp) {
- if (root == null) return;
- // Root
- System.out.println(root.nodevalue + " ");
- outp.print(root.nodevalue + " ");
- // Left
- PreOrder(root.left, outp);
- // Right
- PreOrder(root.right, outp);
- }
- //Methods to print a Binary Tree in Post Order
- public void PostOrder(PrintWriter outp) {
- // start from root node
- PostOrder(root, outp);
- }
- protected void PostOrder(TreeNode<T> root, PrintWriter outp) {
- if (root == null) return;
- // Left
- PostOrder(root.left, outp);
- // Right
- PostOrder(root.right, outp);
- // Root
- System.out.println(root.nodevalue + " ");
- outp.print(root.nodevalue + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement