Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me to fix the error below. I'm trying to search an element from a binary search tree, extractig the serach values from

Can someone help me to fix the error below. I'm trying to search an element from a binary search tree, extractig the serach values from a text file.

Exception in thread "main" java.lang.NullPointerException

at Trees.search(Trees.java:71)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:79)

at Trees.search(Trees.java:82)

at Trees.search(Trees.java:79)

at Trees.searchKey(Trees.java:86)

at Trees.inputUser(Trees.java:64)

at Trees.main(Trees.java:359)

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class Trees {

public static class Node {

private static int height;

int key;

Node left, right;

public Node(int item) {

height = 0;

key = item;

left = right = null;

}

}

// Root of BST

private static ArrayList list;

private static ArrayList searchFile;

Node root;

private String fileName;

private static String outputFile;

private String outputString;

// Constructor

public Trees() {

list = new ArrayList();

searchFile = new ArrayList();

root = null;

fileName = "";

outputFile = "P8Output.txt";

// TODO Auto-generated constructor stub

}

public void inputUser() throws IOException{

Trees tree = new Trees ();

Scanner input = new Scanner(System.in);

System.out.println("**********Welcome to Binary Search Tree application.**********");

System.out.println("You shall be asked to type the name of the file containing numbers.");

/**System.out.println("Please enter the name of the file.");

tree.getNodesCount();

fileName = input.nextLine();*/

tree.readTheFiles();

tree.readSearchFiles();

//tree.writeBinaryTree();

System.out.println(" BALANCED BINARY SERACH TREE ");

//writeReport("BALANCED BINARY SERACH TREE ");

//tree.writeBalancedBinaryTree();

System.out.println("The leaf count of binary tree is : "

+ tree.getLeafCount());

//tree.inorder();

for(Integer elem : searchFile) {

tree.searchKey(elem);

}

}//User Instructtion and his/her input to the program

// A utility function to search a given key in BST

public Node search(Node root, int key)

{

System.out.println(root.key);

// Base Cases: root is null or key is present at root

if (root==null || root.key==key)

return root;

// val is greater than root's key

if (root.key > key)

return search(root.left, key);

// val is less than root's key

return search(root.right, key);

}

public void searchKey(int key) {

root = search(root, key);

//System.out.println(root.key);

}

public int getLeafCount()

{

return getLeafCount(root);

}

public int getLeafCount(Node node)

{

if (node == null)

return 0;

if (node.left == null && node.right == null)

return 1;

else

return getLeafCount(node.left) + getLeafCount(node.right);

}

// This method mainly calls insertRec()

public void insert(int key) {

root = insertRec(root, key);

}

/* A recursive function to insert a new key in BST */

public Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */

if (root == null) {

root = new Node(key);

return root;

}

/* Otherwise, recur down the tree */

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */

return root;

}

// This method mainly calls InorderRec()

public void inorder() {

inorderRec(root);

}

// A utility function to do inorder traversal of BST

public void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.println(root.key);

inorderRec(root.right);

}

}

public int height(Node N) {

if (N == null)

return 0;

return Node.height;

}

// A utility function to get maximum of two integers

public int max(int a, int b) {

return (a > b) ? a : b;

}

// A utility function to right rotate subtree rooted with y

// See the diagram given above.

public Node rightRotate(Node y) {

Node x = y.left;

Node T2 = x.right;

// Perform rotation

x.right = y;

y.left = T2;

// Update heights

Node.height = max(height(y.left), height(y.right)) + 1;

Node.height = max(height(x.left), height(x.right)) + 1;

// Return new root

return x;

}

// A utility function to left rotate subtree rooted with x

// See the diagram given above.

public Node leftRotate(Node x) {

Node y = x.right;

Node T2 = y.left;

// Perform rotation

y.left = x;

x.right = T2;

// Update heights

Node.height = max(height(x.left), height(x.right)) + 1;

Node.height = max(height(y.left), height(y.right)) + 1;

// Return new root

return y;

}

// Get Balance factor of node N

public int getBalance(Node N) {

if (N == null)

return 0;

return height(N.left) - height(N.right);

}

public Node insert(Node node, int key) {

/* 1. Perform the normal BST insertion */

if (node == null)

return (new Node(key));

if (key < node.key)

node.left = insert(node.left, key);

else if (key > node.key)

node.right = insert(node.right, key);

else // Duplicate keys not allowed

return node;

/* 2. Update height of this ancestor node */

Node.height = 1 + max(height(node.left),

height(node.right));

/* 3. Get the balance factor of this ancestor

node to check whether this node became

unbalanced */

int balance = getBalance(node);

// If this node becomes unbalanced, then there

// are 4 cases Left Left Case

if (balance > 1 && key < node.left.key)

return rightRotate(node);

// Right Right Case

if (balance < -1 && key > node.right.key)

return leftRotate(node);

// Left Right Case

if (balance > 1 && key > node.left.key) {

node.left = leftRotate(node.left);

return rightRotate(node);

}

// Right Left Case

if (balance < -1 && key < node.right.key) {

node.right = rightRotate(node.right);

return leftRotate(node);

}

/* return the (unchanged) node pointer */

return node;

}

public void readTheFiles(){

Trees tree = new Trees();

int integerContent = 0;

File file = new File("numbers.txt");

try {

Scanner sc = new Scanner(new FileInputStream(file));

while (sc.hasNextLine()){

integerContent = sc.nextInt();

list.add(integerContent);

}

sc.close();

}catch(FileNotFoundException fnf){

System.out.println("File not found.");

System.exit(0);

}

catch (Exception e) {

System.out.println(" Program terminated Safely...");

}

for(Integer elem : list){

insert(elem);

}

}// end of readTheFilesForInt

public void readSearchFiles(){

Trees tree = new Trees();

int integerContent = 0;

File file = new File("search.txt");

try {

Scanner sc = new Scanner(new FileInputStream(file));

while (sc.hasNextLine()){

integerContent = sc.nextInt();

searchFile.add(integerContent);

}

sc.close();

}catch(FileNotFoundException fnf){

System.out.println("File not found.");

System.exit(0);

}

catch (Exception e) {

System.out.println(" Program terminated Safely...");

}

for(Integer elem : list){

insert(elem);

}

}// end of readTheFilesForInt

public void writeBinaryTree() throws IOException {

Trees tree = new Trees();

double startSystemTime = System.nanoTime();

for(Integer elem : list){

insert(elem);

}

double endSystemTime = System.nanoTime();

double cpuTime = (double)(endSystemTime - startSystemTime);

System.out.println("Unsorted list is " + cpuTime + " Milli-seconds");

outputString = "Unsorted list is " + cpuTime + " Milli-seconds ";

writeReport(outputString);

// Sorted list

Collections.sort(list);

startSystemTime = System.nanoTime();

for(Integer elem : list){

insert(elem);

}

endSystemTime = System.nanoTime();

cpuTime = (double)(endSystemTime - startSystemTime);

System.out.println("Sorted list is " + cpuTime + " Milli-seconds");

outputString = "Sorted list is " + cpuTime + " Milli-seconds ";

writeReport(outputString);

}

public void writeBalancedBinaryTree() throws IOException {

Trees tree = new Trees();

double startSystemTime = System.nanoTime();

for(Integer elem : list){

insert(elem);

}

double endSystemTime = System.nanoTime();

double cpuTime = (double)(endSystemTime - startSystemTime);

System.out.println("Unsorted list is " + cpuTime + " Milli-seconds");

outputString = "Unsorted list is " + cpuTime + " Milli-seconds ";

writeReport(outputString);

// Sorted list

Collections.sort(list);

startSystemTime = System.nanoTime();

for(Integer elem : list){

insert(elem);

}

endSystemTime = System.nanoTime();

cpuTime = (double)(endSystemTime - startSystemTime);

System.out.println("Sorted list is " + cpuTime + " Milli-seconds");

outputString = "Sorted list is " + cpuTime + " Milli-seconds ";

writeReport(outputString);

}

/**

* Writes the report with the data from the trial

*/

private static void writeReport(String reportString){

try{

BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFile), true));

writer.write(reportString);

writer.close();

}catch(Exception ex){

System.out.println("try a different file name");

}

}// end of writeRpeorts

public static void main(String[] args) throws IOException {

Trees tree = new Trees();

tree.inputUser();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions

Question

10:16 AM Sun Jan 29 Answered: 1 week ago

Answered: 1 week ago