Question: Add three methods in the binaryTree.java: public void delete(int m); // delete the node with key m public int countNodes();// count number of nodes in
Add three methods in the binaryTree.java:
public void delete(int m); // delete the node with key m
public int countNodes();// count number of nodes in the tree
public btNode search(int m);// search for a node with key m and return it
Then test the three methods in the test class
Test Class:
package binaryTreeLAB; import java.util.Scanner; public class test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* Creating object of BST */ binaryTree bst = new binaryTree(); System.out.println("Binary Search Tree Test "); char ch; int flag=0; /* Perform tree operations */ do { System.out.println("Enter integer element to insert."); bst.insert( scan.nextInt() ); System.out.println(" Do you want to continue (Type y or n) "); ch = scan.next().charAt(0); } while (ch == 'Y' || ch == 'y'); BTreePrinter.printNode(bst.root); System.out.print(" Pre order : "); bst.preorder(); /* System.out.print(" In order : "); bst.inorder(); System.out.print(" Post order : "); bst.postorder();*/ /*System.out.print(" Height of the tree is : "+bst.findHeight(bst.root));*/ } }
BTreePrinter:
package binaryTreeLAB; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BTreePrinter { public static
btNode:
package binaryTreeLAB; public class btNode { btNode left, right; int data; /* Constructor */ public btNode() { left = null; right = null; data = 0; } /* Constructor */ public btNode(int n) { left = null; right = null; data = n; } /* Function to set left node */ public void setLeft(btNode n) { left = n; } /* Function to set right node */ public void setRight(btNode n) { right = n; } /* Function to get left node */ public btNode getLeft() { return left; } /* Function to get right node */ public btNode getRight() { return right; } /* Function to set data to node */ public void setData(int d) { data = d; } /* Function to get data from node */ public int getData() { return data; } }
binaryTree:
public class binaryTree { protected btNode root; /* Constructor */ public binaryTree() { root = null; } /* Function to check if tree is empty */ public boolean isEmpty() { return root == null; } /* Functions to insert data */ public void insert(int data) { root = insert(root, data); } /* Function to insert data recursively */ private btNode insert(btNode node, int data) { if (node == null) node = new btNode(data); else { if (data <= node.getData()) node.left = insert(node.left, data); else node.right = insert(node.right, data); } return node; } /* Function for preorder traversal */ public void preorder() { preorder(root); } private void preorder(btNode r) { if (r != null) { System.out.print(r.getData() +" "); preorder(r.getLeft()); preorder(r.getRight()); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
