Advertisement
Luninariel

Binary Tree Find & Delete - WIP

Apr 22nd, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.53 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: 78,39,52,28,33,14,16,4,35,105,85,92,80,97,110, & 99
  14.  * The Numbers are added to instances of MyBinaryTreeMgr for their respective Data type.
  15.  * A Binary Tree is Formed by making the first number the Root of The tree.
  16.  * Numbers greater in value than the root, move to the right "Leaf" of the tree.
  17.  * Numbers less in value than the root, move to the left "Leaf" of the tree.
  18.  * Once the numbers are added, the tree can be traversed either InOrder, PreOrder, or PostOrder.
  19.  * Inorder Follows the rule of Left - Root - Right.
  20.  * PreOrder Follows the rule of Root - Left - Right.
  21.  * PostOrder Follows the rule of Left - Right - Root.
  22.  * A print out of each tree in each form of Traversal is provided.
  23.  * From there specific nodes of the tree are deleted.
  24.  * First we delete 28.
  25.  * Then we traverse the tree Inorder, Preorder, and PostOrder again.
  26.  * Then we delete 105.
  27.  * Then we traverse the tree Inorder, Preorder, and PostOrder again.
  28.  * Finally we delete 110.
  29.  * Then we traverse the tree Inorder, Preorder, and PostOrder one last time.
  30.  */
  31.  
  32.  
  33. public class BinaryTreeNodeFinder {
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.  
  37.         //Create a print write for documentation to a file and printing assistance
  38.         PrintWriter outpt;
  39.         outpt = new PrintWriter(new File("JavaAbsOut6.txt"));
  40.  
  41.         //Create a new instance of the Binary Tree Manager to handle Integers.
  42.         MyBinaryTreeMgr<Integer> BtreeInt = new MyBinaryTreeMgr<Integer>();
  43.  
  44.  
  45.         //Add Integers to the Binary Tree BtreeInt
  46.         BtreeInt.insertnode(78);
  47.         BtreeInt.insertnode(39);
  48.         BtreeInt.insertnode(52);
  49.         BtreeInt.insertnode(28);
  50.         BtreeInt.insertnode(33);
  51.         BtreeInt.insertnode(14);
  52.         BtreeInt.insertnode(16);
  53.         BtreeInt.insertnode(4);
  54.         BtreeInt.insertnode(35);
  55.         BtreeInt.insertnode(105);
  56.         BtreeInt.insertnode(85);
  57.         BtreeInt.insertnode(92);
  58.         BtreeInt.insertnode(80);
  59.         BtreeInt.insertnode(97);
  60.         BtreeInt.insertnode(110);
  61.         BtreeInt.insertnode(99);
  62.  
  63.  
  64.         //Print the Binary Tree BTreeInt InOrder.
  65.         System.out.println("\nPrinting The Integer Tree InOrder Left-Root-Right \n");
  66.         BtreeInt.inorder(outpt);
  67.  
  68.         //Print the binary Tree BTreeInt in PreOrder
  69.         System.out.println("\nPrinting The Integer Tree In Preorder Root-Left-Right \n");
  70.         BtreeInt.PreOrder(outpt);
  71.  
  72.         //Print the Binary Tree BtreeInt in PostOrder
  73.         System.out.println("\n Printing The Integer Tree in PostOrder Left-Right-Root\n");
  74.         BtreeInt.PostOrder(outpt);
  75.  
  76.         //Create values for the values we're going to be deleting.
  77.         int firstfind = 28;
  78.         int secondfind = 105;
  79.         int thirdfind = 110;
  80.  
  81.  
  82.         //Find the First node we're attempting to delete.
  83.         System.out.println("\nFinding: "+firstfind);
  84.         BtreeInt.FindNode(firstfind);
  85.  
  86.  
  87.         //Delete the first node we're attempting to delete from the tree.
  88.         System.out.println("\nDeleting "+firstfind+" From The Tree");
  89.         BtreeInt.deleteNode(firstfind);
  90.  
  91.  
  92.         //Print the tree Inorder after deleting firstfind
  93.         System.out.println("\n"+firstfind+" Has Been Deleted From The Tree. Listing Tree InOrder\n");
  94.         BtreeInt.inorder(outpt);
  95.  
  96.         //Print the tree in PreOrder after deleting firstfind
  97.         System.out.println("\n"+firstfind+" Has Been Deleted From The Tree. Listing Tree In PreOrder\n");
  98.         BtreeInt.PreOrder(outpt);
  99.  
  100.         //Print the tree in PostOrder after deleting firstfind
  101.         System.out.println("\n"+firstfind+" Has Been Deleted From The Tree. Listing Tree In PostOrder\n");
  102.         BtreeInt.PostOrder(outpt);
  103.  
  104.         //Find the second node we're attempting to delete.
  105.         System.out.println("\nFinding: "+secondfind);
  106.         BtreeInt.FindNode(secondfind);
  107.  
  108.         //Delete the first node we're attempting to delete from the tree.
  109.         System.out.println("\nDeleting "+secondfind+" From The Tree");
  110.         BtreeInt.deleteNode(secondfind);
  111.  
  112.  
  113.         //Print the tree Inorder after deleting secondfind
  114.         System.out.println("\n"+firstfind+", & "+secondfind+" Have Been Deleted From The Tree. Listing Tree InOrder\n");
  115.         BtreeInt.inorder(outpt);
  116.  
  117.         //Print the tree in PreOrder after deleting secondfind
  118.         System.out.println("\n"+firstfind+", & "+secondfind+" Have Been Deleted From The Tree. Listing Tree In PreOrder\n");
  119.         BtreeInt.PreOrder(outpt);
  120.  
  121.         //Print the tree in PostOrder after deleting secondfind
  122.         System.out.println("\n"+firstfind+", & "+secondfind+" Have Been Deleted From The Tree. Listing Tree In PostOrder\n");
  123.         BtreeInt.PostOrder(outpt);
  124.  
  125.         //Find the Third node we're attempting to delete.
  126.         System.out.println("\nFinding: "+thirdfind);
  127.         BtreeInt.FindNode(thirdfind);
  128.  
  129.         //Delete the third node we're attempting to delete from the tree.
  130.         System.out.println("\nDeleting "+thirdfind+" From The Tree");
  131.         BtreeInt.deleteNode(thirdfind);
  132.  
  133.         //Print the tree Inorder after deleting thirdfind
  134.         System.out.println("\n"+firstfind+", "+secondfind+", & "+thirdfind+" Have Been Deleted From The Tree. Listing Tree InOrder\n");
  135.         BtreeInt.inorder(outpt);
  136.  
  137.         //Print the tree in PreOrder after deleting secondfind
  138.         System.out.println("\n"+firstfind+", "+secondfind+", & "+thirdfind+" Have Been Deleted From The Tree. Listing Tree In PreOrder\n");
  139.         BtreeInt.PreOrder(outpt);
  140.  
  141.         //Print the tree in PostOrder after deleting secondfind
  142.         System.out.println("\n"+firstfind+", "+secondfind+", & "+thirdfind+" Have Been Deleted From The Tree. Listing Tree In PostOrder\n");
  143.         BtreeInt.PostOrder(outpt);
  144.        
  145.         outpt.close();
  146.  
  147.     }
  148. }
  149.  
  150. //The Class MyBinaryTreeMgr, Generically handles Binary Trees
  151. class MyBinaryTreeMgr<T extends Comparable<T>> {
  152.     protected TreeNode<T> root;
  153.     protected int number;
  154.     protected T nodevalue;
  155.     protected TreeNode<T> left;
  156.     protected TreeNode<T> right;
  157.  
  158.     //Inner Class for a single Tree Node
  159.     private static class TreeNode<T extends Comparable> {
  160.         protected T nodevalue;
  161.         protected TreeNode<T> left;
  162.         protected TreeNode<T> right;
  163.  
  164.         public TreeNode(T x) {
  165.             nodevalue = x;
  166.             left = null;
  167.             right = null;
  168.         }
  169.  
  170.     }
  171.  
  172.     protected TreeNode<T> foundnode;
  173.     protected TreeNode<T> foundleftchild;
  174.     protected TreeNode<T> foundrightchild;
  175.     protected TreeNode<T> foundnodeparent;
  176.     protected TreeNode<T> currentnode;
  177.     protected TreeNode<T> currentleft;
  178.     protected TreeNode<T> currentright;
  179.     protected TreeNode<T> parentnode;
  180.  
  181.     public MyBinaryTreeMgr() {
  182.         root = null;
  183.         int number = 0;
  184.     }
  185.  
  186.     //Getter to find the number of nodes in a tree.
  187.     public int getnumber() {
  188.         return number;
  189.     }
  190.  
  191.     //Method to find and print the parent node of a found node.
  192.     public void printparent() {
  193.         if (foundnodeparent == null) {
  194.             System.out.println("Parent null");
  195.         } else {
  196.             System.out.println(foundnodeparent.nodevalue);
  197.         }
  198.     }
  199.  
  200.     //Method to find and print the left node of a found node.
  201.     public void printleft() {
  202.         if (foundleftchild == null) {
  203.             System.out.println("Left Child null");
  204.         } else {
  205.             System.out.println(foundleftchild.nodevalue);
  206.         }
  207.     }
  208.  
  209.     //Method to find and print the right node of a found node.
  210.     public void printright() {
  211.         if (foundrightchild == null) {
  212.             System.out.println("Right child null");
  213.         } else {
  214.             System.out.println(foundrightchild.nodevalue);
  215.         }
  216.     }
  217.  
  218.     //Method used to create a node within the binary tree
  219.     protected TreeNode<T> CreateNode(T x) {
  220.         return new TreeNode(x);
  221.     }
  222.  
  223.     //Method to insert a value into the binary tree.
  224.     public int insertnode(T x) {
  225.         if (root == null) {
  226.             root = CreateNode(x);
  227.             number = 1;
  228.             return number;
  229.         } else {
  230.             TreeNode<T> parent = null;
  231.             TreeNode<T> current = root;
  232.             while (current != null)
  233.                 if (x.compareTo(current.nodevalue) < 0) {
  234.                     parent = current;
  235.                     current = current.left;
  236.                 } else if (x.compareTo(current.nodevalue) > 0) {
  237.                     parent = current;
  238.                     current = current.right;
  239.                 } else return -99;
  240.  
  241.             if (x.compareTo(parent.nodevalue) < 0) {
  242.                 parent.left = CreateNode(x);
  243.             } else parent.right = CreateNode(x);
  244.         }
  245.         number++;
  246.         return number;
  247.     }
  248.  
  249.     //Method to Find a Node within a Binary Tree.
  250.     public int FindNode(T x) {
  251.         int Nsearch = 0;
  252.         if (number == 0) {
  253.             Nsearch = 0;
  254.             return Nsearch;
  255.         } else {
  256.             if (x.compareTo(root.nodevalue) == 0) {
  257.                 Nsearch = 2;
  258.                 foundnodeparent = parentnode = root;
  259.                 foundleftchild = root.left;
  260.                 foundrightchild = root.right;
  261.                 return Nsearch;
  262.             } else {
  263.                 parentnode = root;
  264.                 currentnode = root;
  265.                 currentleft = root.left;
  266.                 currentright = root.right;
  267.             }
  268.             while (x.compareTo(currentnode.nodevalue) != 0) {
  269.                 if (x.compareTo(currentnode.nodevalue) > 0) {
  270.                     parentnode = currentnode;
  271.                     if (currentnode.right == null) {
  272.                         Nsearch = 0;
  273.                         return Nsearch;
  274.                     }
  275.                     currentnode = currentnode.right;
  276.                 } else {
  277.                     foundnodeparent = parentnode = currentnode;
  278.                     if (currentnode.left == null) {
  279.                         Nsearch = 0;
  280.                         return Nsearch;
  281.                     }
  282.                     currentnode = currentnode.left;
  283.                 }
  284.             }
  285.             foundleftchild = currentnode.left;
  286.             foundrightchild = currentnode.right;
  287.             Nsearch = 1;
  288.         }
  289.         return Nsearch;
  290.     }
  291.  
  292.     //Method to Delete a value from a Binary Tree
  293.  
  294.     // This method mainly calls deleteRec()
  295.     void deleteNode(T key)
  296.     {
  297.         root = deleteRec(root, key);
  298.     }
  299.  
  300.     /* A recursive function to insert a new key in BST */
  301.     TreeNode<T> deleteRec(TreeNode<T> root, T key)
  302.     {
  303.         /* Base Case: If the tree is empty */
  304.         if (root == null)  return root;
  305.  
  306.         /* Otherwise, recur down the tree */
  307.         //if (x.compareTo(root.nodevalue) == 0)
  308.         if (key.compareTo(root.nodevalue) < 0)
  309.             root.left = deleteRec(root.left, key);
  310.         else if (key.compareTo(root.nodevalue) > 0)
  311.             root.right = deleteRec(root.right, key);
  312.  
  313.             // if key is same as root's key, then This is the node to be deleted
  314.         else
  315.         {
  316.             // node with only one child or no child
  317.             if (root.left == null)
  318.                 return root.right;
  319.             else if (root.right == null)
  320.                 return root.left;
  321.  
  322.             // node with two children: Get the inorder successor (smallest in the right subtree)
  323.             root.nodevalue = minValue(root.right);
  324.  
  325.             // Delete the inorder successor
  326.             root.right = deleteRec(root.right, root.nodevalue);
  327.         }
  328.  
  329.         return root;
  330.     }
  331.     T minValue(TreeNode<T> root)
  332.     {
  333.         T minv = root.nodevalue;
  334.         while (root.left != null)
  335.         {
  336.             minv = root.left.nodevalue;
  337.             root = root.left;
  338.         }
  339.         return minv;
  340.     }
  341.  
  342.  
  343.  
  344.     //Methods to print a Binary Tree InOrder
  345.     public void inorder(PrintWriter outp) {
  346.         //Start From Root Node
  347.         inorder(root, outp);
  348.     }
  349.  
  350.     protected void inorder(TreeNode<T> root, PrintWriter outp) {
  351.         if (root == null) return;
  352.         //Left
  353.         inorder(root.left, outp);
  354.         //Root
  355.         System.out.println(root.nodevalue + " ");
  356.         outp.print(root.nodevalue + " ");
  357.         //Right
  358.         inorder(root.right, outp);
  359.     }
  360.  
  361.     //Methods to print a Binary Tree in PreOrder
  362.     public void PreOrder(PrintWriter outp) {
  363.         // start from Root Node
  364.         PreOrder(root, outp);
  365.     }
  366.  
  367.     protected void PreOrder(TreeNode<T> root, PrintWriter outp) {
  368.         if (root == null) return;
  369.         // Root
  370.         System.out.println(root.nodevalue + " ");
  371.         outp.print(root.nodevalue + " ");
  372.  
  373.         // Left
  374.         PreOrder(root.left, outp);
  375.  
  376.         // Right
  377.         PreOrder(root.right, outp);
  378.     }
  379.  
  380.     //Methods to print a Binary Tree in Post Order
  381.     public void PostOrder(PrintWriter outp) {
  382.         // start from root node
  383.         PostOrder(root, outp);
  384.     }
  385.  
  386.     protected void PostOrder(TreeNode<T> root, PrintWriter outp) {
  387.         if (root == null) return;
  388.         // Left
  389.         PostOrder(root.left, outp);
  390.  
  391.         // Right
  392.         PostOrder(root.right, outp);
  393.  
  394.         // Root
  395.         System.out.println(root.nodevalue + " ");
  396.         outp.print(root.nodevalue + " ");
  397.     }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement