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;
- public class BinaryTreeNodeFinder {
- public static void main(String[] args)throws Exception {
- PrintWriter outpt;
- outpt = new PrintWriter(new File("JavaAbsOut6.txt"));
- MyBinaryTreeMgr<Integer> BtreeInt = new MyBinaryTreeMgr<Integer>();
- BtreeInt.insertnode(65);
- BtreeInt.insertnode(32);
- BtreeInt.insertnode(75);
- BtreeInt.insertnode(16);
- BtreeInt.insertnode(45);
- BtreeInt.insertnode(81);
- System.out.println("This is the binary tree test there are " + BtreeInt.getnumber());
- outpt.println("This is the binary tree test there are " + BtreeInt.getnumber());
- System.out.println("Printing this tree inorder left-middle-right ");
- int x;
- System.out.println("\n Now lets find some nodes\n");
- System.out.println("First the root node ");
- x = 65;
- System.out.println("Lets find a node " + x + " Node finder returns " + BtreeInt.FindNode(x));
- System.out.println("\nParent node has value "); BtreeInt.printparent();
- System.out.println("Left node has value "); BtreeInt.printleft();
- System.out.println("Right node has value "); BtreeInt.printright();
- x = 65;
- System.out.println("Lets find a node " + x + " Node finder returns "); BtreeInt.FindNode(x);
- System.out.println("\nParent node has value " ); BtreeInt.printparent();
- System.out.println("Left node has value " ); BtreeInt.printleft();
- System.out.println("Right node has value "); BtreeInt.printright();
- x = 16;
- System.out.println("Lets find a node " + x + " Node finder returns "); BtreeInt.FindNode(x);
- System.out.println("\nParent node has value "); BtreeInt.printparent();
- System.out.println("Left node has value "); BtreeInt.printleft();
- System.out.println("Right node has value "); BtreeInt.printright();
- BtreeInt.inorder(outpt);
- BtreeInt.insertnode(78);
- System.out.println("This is the binary tree test there are " + BtreeInt.getnumber());
- outpt.println("This is the binary tree test there are " + BtreeInt.getnumber());
- System.out.println("Printing this tree inorder left-middle-right ");
- BtreeInt.inorder(outpt);
- MyBinaryTreeMgr<Double> BtreeDoub = new MyBinaryTreeMgr<Double>();
- BtreeDoub.insertnode(32.9);
- BtreeDoub.insertnode(47.3);
- BtreeDoub.insertnode(89.4);
- BtreeDoub.insertnode(15.3);
- BtreeDoub.insertnode(2.5);
- BtreeDoub.insertnode(103.2);
- BtreeDoub.insertnode(18.1);
- BtreeDoub.insertnode(16.2);
- System.out.println("\nThis is the binary tree test there are " + BtreeDoub.getnumber());
- outpt.println("This is the binary tree test there are " + BtreeDoub.getnumber());
- System.out.println("Printing this tree inorder left-middle-right\n");
- Double x1 = 47.3;
- System.out.println("Lets find a node " + x1 + " Node finder returns " + BtreeDoub.FindNode(x1));
- System.out.println("Parent node has value "); BtreeDoub.printparent();
- System.out.println("Left node has value "); BtreeDoub.printleft();
- System.out.println("Right node has value "); BtreeDoub.printright();
- x1 = 15.3;
- System.out.println("Lets find a node " + x1 + " Node finder returns " + BtreeDoub.FindNode(x1));
- System.out.println("Parent node has value "); BtreeDoub.printparent();
- System.out.println("Left node has value "); BtreeDoub.printleft();
- System.out.println("Right node has value "); BtreeDoub.printright();
- BtreeDoub.inorder(outpt);
- outpt.close();
- }
- }
- class MyBinaryTreeMgr<T extends Comparable>{
- protected TreeNode<T> root;
- protected int number;
- protected T nodevalue;
- protected TreeNode<T> left;
- protected TreeNode<T> right;
- 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;
- }
- public int getnumber() {
- return number;
- }
- public void printparent() {
- if (foundnodeparent == null) {
- System.out.println("Parent null");
- } else {
- System.out.println("Parent " + foundnodeparent.nodevalue);
- }
- }
- public void printleft() {
- if (foundleftchild == null) {
- System.out.println("Left Child null");
- } else {
- System.out.println("Left Child " + foundleftchild.nodevalue);
- }
- }
- public void printright() {
- if (foundrightchild == null) {
- System.out.println("Right child null");
- } else {
- System.out.println("Right Child " + foundrightchild.nodevalue);
- }
- }
- protected TreeNode<T> CreateNode(T x) {
- return new TreeNode(x);
- }
- 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;
- }
- 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;
- }
- public void inorder(PrintWriter outp){
- inorder(root,outp);
- }
- protected void inorder(TreeNode<T> root, PrintWriter outp){
- if (root == null)return;
- inorder(root.left, outp);
- System.out.println(root.nodevalue+" ");
- outp.print(root.nodevalue+" ");
- inorder(root.right,outp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement