Binary Search Tree - Calling FindSmallest Method In Main Class

Nov 3, 2014

How should I call my findSmallest method in the main class.. Here is the code:

public class Tester {
public static void main(String[] args){
try {
BinaryTree<Integer> myTree2 = new BinaryTree<Integer>();
 
myTree2.insert(5);
myTree2.insert(2);
myTree2.insert(7);

[Code] ....

So the question is what kind of parameter I should pass in myTree2.findSmallest()??? Here is my findSmallest method..

public E findSmallest(Node<E> parent){
if(parent.left.left == null){
E returnValue = parent.left.data;
parent.left = parent.left.right;
return returnValue;
} else {
return findSmallest(parent.left);
}
}

View Replies


ADVERTISEMENT

Creating Search Method For Binary Search Tree

Apr 22, 2014

I want to create a search method that returns the frequency of a word in the search method.

public class IndexTree {
private class TreeNode {
TreeNode left;
String word;
int frequency;
TreeNode right;

[Code] .....

View Replies View Related

Ability To Search A Binary Search Tree And Return The Number Of Probes

Sep 1, 2014

I'm trying to build a method that can search a binary search tree for a specific target and then return the number of probes it took to get there. It seems to me that the best way to do this would be a recursive search method and a counter that tracks the number of calls. But I'm struggling with how to implement this. Here's the code I have so far. what works/doesn't work with the method.

// Method to search the tree for a specific name and
// return the number of probes
public T search(BTNode<T> btNode) {

[Code]....

View Replies View Related

Calling Method Of A Class From Main Class?

Aug 31, 2014

// Add range to Vehicle.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon

// Display the range.
void range() {
System.out.println("Range is " + fuelcap * mpg);

[Code] ....

I'm compiling it in Eclipse and this continues to show in the console display

Minivan can carry 7. Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:34)

View Replies View Related

How To Implement Simple Binary Search Tree

May 17, 2015

I am trying to implement a simple binary search tree . How can I make a node in the tree which should store two pieces of information: a String variable called name, and a int variable called mark. ..

public class BinarySearchTree<E> implements Comparable<E> {
public BinaryTree<E> root;
int size;
int mark;
String name;
// Constructor
public BinarySearchTree()

[Code]...

View Replies View Related

Not Getting Correct Results In Binary Search Tree

Aug 17, 2014

I don't see any nodes that I add. Not sure why getting this error.

duplicate found
Exception in thread "main" java.lang.NullPointerException
at binarysearchtree.delete(binarysearchtree.java:111)
at binarysearchtree.main(binarysearchtree.java:196)
Java Result: 1
public class node<T>

[Code] .....

View Replies View Related

Finding Height Of Binary Search Tree?

Nov 13, 2014

So for an assignment I have to write a method that finds that height of this BST. I wrote this:

public int height(Node node)
{
if (node == null)
{
return 0;
}
else
{
return 1 +
Math.max(height(node.left),
height(node.right));
}
}

Which makes sense in my head. However, I added a couple of nodes in and realized I'm not sure how to call this method.

public static void main(String[] args) {
Tree<String, String> tree = new Tree<String, String>();
tree.add("A", "1");//some added stuff
tree.add("B", "2");
tree.add("C", "3");
tree.add("D", "4");
tree.add("E", "5");
tree.add("F", "6");
tree.add("G", "7");
tree.add("H", "8");
System.out.println(tree);
System.out.println(tree.height());

What exactly do I put in the parenthesis after tree.height? If you need to see more of my code I can do that but it is quite lengthy.

View Replies View Related

How To Compare String In Binary Search Tree

Aug 31, 2014

I'm trying to implement a Binary Search Tree that accepts strings. I cannot figure out how to compare the string value in my add method against the node object. While I could make the node class data be the string type I am trying to make the code be as reusable as possible. So my question is this, is there a simple way I can compare the two that I am missing?

public class BTNode<E> {
private E data;
private BTNode<E> left, right;
//constructor
public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight) {
data = initialData;
left = initialLeft;
right = initialRight;

[Code] ....

View Replies View Related

Insert Numbers In Binary Search Tree

Oct 13, 2011

I am trying to make binary search tree...I am trying to construct the left part of binary search tree with root node set as 70...It is not working ...My code is

public class Node {
/**
* @param args
*/

int root;
Node left;
Node right;
public void insertNode(Node node, int num) {
//Node nodeRoot = this;
//root of tree we have set to 70...constructing only left of tree with root=70

[Code] .....

View Replies View Related

Binary Search Tree To Graphic Text File

Nov 24, 2014

I am trying to draw a binary node tree to a text file.

public class BinaryTreeExample {
public static void main(String[] args) {
new BinaryTreeExample().run();
} static class Node {
Node left;
Node right;
int value;
public Node(int value) {

[Code] .....

This will output:

Building tree with rootvalue25
=================================
Inserted 11 to left of node 25
Inserted 15to right of node 11
Inserted 16to right of node 15
Inserted 23to right of node 16
Inserted 79to right of node 25
Traversing tree in order
=================================
Traversed 11
Traversed 15
Traversed 16
Traversed 23
Traversed 25
Traversed 79

I need to print this information in the form of a graphic to a text file. so for example:

OUTPUT IN TEXT FILE
25
/
11 79
etc...

View Replies View Related

Searching Binary Search Tree For Movie By Rating

May 8, 2015

The purpose of this function is to use a Movie object and a binary search tree to find all movies that have been read in through a file that have a certain rating (G,PG,PG-13,R). The error I am getting is that it seems I am not traversing fully through the tree and therefore only some movies with the rating I search for are output, not all of them with that rating. I have tried figuring out why this is happening and at first I thought my tree was unbalanced, but I am sure it is simply because I am not traversing the tree correctly. I think my implementation in the main is close to what I need but it needs some tweaking. This is my BST class which I created and required to use for this purpose.

public class BinarySearchTree {
/**
* The head of the tree.
*/
private Node root;
/**
* Constructor that declares the head of tree to null.
*/
public BinarySearchTree() {
root = null;
}
/**
* Returns null if head of tree is empty.
* @return null if head of tree is empty.
*/
public boolean isEmpty(){

[Code]...

View Replies View Related

Unable To Remove Node From Binary Search Tree?

Apr 1, 2015

So in my binary search tree I'm trying to write a method that deletes the idem but for some reason it's not working. I have two things, I have the method and then another piece of code that tests the deletion method. Here's the actual method
 
public void delete(String word) {
root = deleteItem(root, word);
}
protected TreeNode deleteItem(TreeNode r, String word) {
if (r == null){
return r;
}
if(word.compareTo(r.item.getWord()) < 0){

[Code]...

Here's the test code

message = "Test: deleting 'word' -- ";
t = new BSTRefBased();
try {
t.delete("word");
result = t.getRootItem().getWord().equals(null);
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));

So for some reason it keeps saying that the test failed. Why does it keep saying that.

View Replies View Related

Why Reference Node Doesn't Change It Value In Binary Search Tree

Feb 10, 2015

the problem was :

i wrote it on paper with my examples.and just for laughts , i uploaded another picture that dont belong to my problem. its just my way to understand binary search tree implementation , so i wanted so share.just for laught : looks like the map of the universe.

now my problem : why after i changed Current to Current.left, Parent still has the value of root. i draw and wrote it on a paper. to be clear as possible - picture 2 :

View Replies View Related

Constructing Expression Trees In Java By Using Binary Search Tree

May 6, 2014

I Write a Java program to parse a syntactically correct arithmetical expression and produce an equivalent Expression TREE. Remember, in an expression tree the terminal nodes are variables and constants, and the interior nodes are operators (such as +,-,*,/).

For instance the expression: (1 + 2) * 3 can be represented by the following tree:

INPUT Expression
(1 +2)*3
POSTORDER Expression
1 2 + 3 *
TREE [root=asd.reg.Node@56ddc58]

But when i Execute the program it Shows only Prefix and postfix .But the requered output is in inorder preorder and postorder so how to solve these error

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.tree.TreeNode;
public class InToPost {
private static String str;

[Code] .....

View Replies View Related

Importing Values From Text File Into Binary Search Tree

Nov 25, 2014

importing values from a text file into a Binary Search Tree. Currently my tree is hard coded w/ values in the BTreePrinterTest Class.

BTreePrinter Class:
Java Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

[code]...

My input text file will be in the following format, each row is 1 tree, each empty line between the rows means a new tree.Java Code:

80 45 14 1 2 3 4 51

2 4 7 8 9 6 3 4

1 2 3 4 5 6 7 9 mh_sh_highlight_all('java');

View Replies View Related

Linked Binary Search Tree - Creating Node Object

Apr 5, 2015

I'm trying to use LinkedBinarySearchTree but a lot of the variables are protected in BinaryTreeNode. I am creating a BinaryTreeNode object but it still isn't allowing me to use them. The variables I am trying to use are element, left, and right.

import ch11.exceptions.*;
import ch10.LinkedBinaryTree;
import ch10.BinaryTreeNode;
/**
* LinkedBinarySearchTree implements the BinarySearchTreeADT interface with links.

*/
public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>
implements BinarySearchTreeADT<T>

[Code] ....

View Replies View Related

Binary Search Tree Dictionary Returning All Words Misspelled

Feb 12, 2015

The assignment is to be able to read two files, a dictionary file, and a file you want to spell check. I've also attached the test files too.

When reading small files where there is only one word per line it works fine. But when I try to test a paragraph it returns all the words as misspelt.

Here are all of the classes that were given for this assignment. I've made some modifications to try to get them to work properly.

BinarySearchTree.java

// ADT binary search tree.
// Assumption: A tree contains at most one item with a given search key at any time.

public class BinarySearchTree extends BinaryTreeBasis {
// inherits isEmpty(), makeEmpty(), getRootItem(), and
// the use of the constructors from BinaryTreeBasis

[Code] .....

As you can see I read in the dictionary file and the spellcheck file, construct a new BST, and then insert each word from the dictionary into the tree.

Then I make an empty arraylist for the misspelt words. While searching through the spellcheck file, if it finds any incorrect words, it adds them to the list then returns the list after it's done reading through the file.

But for some reason it prints out every single word in the test file and I can't figure out why.

Attached File(s)

dictionary_large.txt (1.83MB)
dictionary_small.txt (225bytes)
test_large.txt (553bytes)
test_small.txt (52bytes)

View Replies View Related

Recursive Method Using Technique Calling Tree

May 9, 2014

a)Write a method that recursively displays any given character the specified number of times on one line.For example, the call: displayRowOf Characters(,5);

Produce a line: *****Write another method that uses a for-loop to perform the same process.

B is something like this ?
for (i=1; i<=n; i++)
i= '*' * n;
System.out.print(i);

View Replies View Related

Binary Tree Insert Method

Mar 24, 2014

So here is the code that I have. The problem is my insert method. It compiles, but it doesn't do so well in the test class I have set up for it.

package SystemandDesign.BinaryTree;
 mport java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.io.ObjectInputStream;
import java.util.Scanner;

[code]....

View Replies View Related

Find Node Method Within Binary Tree

Mar 29, 2015

I have some work where I have to create a binary tree. Anyway I am stuck on one of the classes which has several methods I must fill in! The code is below

The headers for each method are: public Node find (member p1)The method find(Member p1) is used to return the node which contains a particular member. It is a private method because the interface to CabinetTree should not provide direct access to the internal nodes. It makes use of the method Node findNode (Member p1, Node current)

and the next method is : Node findNode(Member p1, Node current) which searches the tree from current node to find a node whose value is equal to the Member p1, i.e. a node in the tree such that p1.equals(current.getMember()) is true. If there is no such node then the method should return null.

public class CabinetTree {
private Node head;
CabinetTree(Member p1) {
head = new Node(p1, null);

[Code] ....

View Replies View Related

Binary Tree Delete Method - NullPointerException

Oct 4, 2014

Iva tried flipping the entire code upside down (expression) and No matter what I do the delete function returns a null pointer exception after my while loops takes the pointer right to where it should be.

package bp;
import java.time.LocalDate;
public class BinaryTree implements IBinaryTree {
private Node root = null;
private int sizeOfTree = 0;

[Code] ....

View Replies View Related

Binary Tree Java Test Class

Mar 24, 2014

So, managed to get my Binary Search Tree to work properly:

package SystemandDesign.BinaryTree;
 import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.io.ObjectInputStream;
import java.util.Scanner;
 
[Code] ....

No matter how many times I keep rewritting the testInsert method I get a chock full of errors.

View Replies View Related

Calling A Method From Main

Jan 26, 2014

The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.

The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.

Java Code:

package plant.nursery;
import java.util.ArrayList;
import java.util.Scanner;
/**Class to create a plant for a nursery.
public class PlantNursery

[code]....

View Replies View Related

Calling A Method To Main

Nov 17, 2014

I'm trying to call a method to my main method, but I can't seem to get it to work and it keeps resulting in a compile error.

import java.io.*;
import java.awt.Point;
import java.text.DecimalFormat;
import java.util.Random;
public class Chase {
public static void main(String args[]){

[Code] .....

View Replies View Related

Calling A Method To The Main

Sep 10, 2014

I have been reading about methods and I do have a beginner level understanding on how they work. I was trying to mess around and make a dog calculator using methods. I ran into a small snag; I cannot get the method to call to the main or the program to compile correctly. The first code below is the original. To me it looks like (based off of some examples I looked at) there should be no problems, but NetBeans gives me a few errors. 1) line 8- "cannot find symbol, variable dogYearCalc; 2) line 18 illegal start of expression; and 3) line 22 - unreachable statement.

import javax.swing.JOptionPane;
public class KrisExample {
public static void main (String[] args) {
double dogYears = 0;
JOptionPane.showInputDialog (null,"Please enter you dog's age in human years:");

[Code] ....

Someone told me that I was calling dogYearCalc without any arguments in your main method. I take that to mean that I needed to add it to the main, so I did here:

public static void main (String[] args, double dogYearCalc) {

That got rid of my first error, but then when I tried to run the program NetBeans said that I have no main class, so switched back to the original program above.

I thought that when I calling the dogYearCalc method on line 10 was the whole purpose of using a method. It seems to me that putting it somewhere in the main is counter productive.

View Replies View Related

Flowchart - Using Binary Search Method

Apr 19, 2015

I am currently taking a class thats requires me to use flowcharts in a way t figure out algithrims This is the flowchart that i need to use: [URL] .....

This is my current code.

public int binSearch(int target) {
int first = 0;
int last = count -1;
int found =0;
int middle=0;
while (first <=last && found == 0)

[Code] ......

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved