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
ADVERTISEMENT
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
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
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
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
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
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
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
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
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
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
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
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
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
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
May 10, 2014
This was my initial question: I am confused as to why this won't return the desired value. I keep running into an infinite loop or data is returned that I searched.
I still need to work on whether a user enters a search that doesn't exist......that is where I am stuck....
code has been updated......
int low = 0;
int high = subArray.length;
while (low < high) {
System.out.println("You have entered the while loop");
int mid = (low + high) / 2;
[Code] ....
View Replies
View Related
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
View Related
May 21, 2015
Code for search number from tuple tree? I want to add into my code below. My idea below is to search number from the tree and then print the number. For example, enter input for event is 4, input for strength is 3. Then generate tuple tree. Then user enter number for searching. That number will check every branches in the tuple tree. Then it will printout the number.
Main class code.
package treejava.main;
import java.util.Scanner;
import treejava.utils.TreeUtils;
public class MainClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("----Welcome to the Tree Java-Based program-----");
[Code] ....
View Replies
View Related
Apr 15, 2014
We have this piece of code and we must make a search for a key. if the key exist it returns true if not false. Plus we must insert a key in the class. If it is already in there we say hey its already in and we don t put it again...
package askisi2;
import java.util.*;
public class mtree {
protected class tnode {
public int k1;
public int k2;
public int k3;
[Code] ....
View Replies
View Related
May 26, 2015
I have problem in searching for the words from my text file.. im using binary search technique
private boolean doSearchQuery(String searchQuery) throws IOException {
Log.i(TAG, "in doSearchQuery, query string: " + searchQuery);
boolean result = false;
toSort = new ArrayList<String>();
myTv = (TextView) findViewById(R.id.myFile);
[Code] .....
View Replies
View Related
Mar 24, 2014
I am trying to make a program that compares the 3 different search algorithms(sequential/binary/hashing). I have already made the sequential search algorithm, binary and hashing part of the code.My program OPENS a data file, and then OPENS a key data file. and then searches through them using both. How to go about the binary and hashing search algorithm (in the driver program).
*I have included a zip file with:
-base program
-driver program
-data file
-key data file
View Replies
View Related
Apr 26, 2015
package bisecsearchsmccr;
import java.util.Random;
public class BisecSearchSMcCr
{
public static void main(String[] args) {
String[] names =
[Code] ....
I get this error:
run:
Johann
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 41
at bisecsearchsmccr.BisecSearchSMcCr.bisect(BisecSearchSMcCr.java:72)
at bisecsearchsmccr.BisecSearchSMcCr.main(BisecSearchSMcCr.java:42)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
View Replies
View Related
Apr 20, 2015
I have created a binary search with 3 subsets, some aslo call it a ternary search and have come up with a minor problem. If you run the code as posted below it just runs until you quit it. If anyother value in the array is searched it is found.
/*
public class BinarySearchDemo {
public static void main(String[] args) {
String[] songs = {"Ace Of Spades", "Beyond the Realms Of Death", "Breaking The Law",
[Code].....
View Replies
View Related
Dec 7, 2014
How do I make it so I am able to enter any number rather then just the numbers in the arrays? I want it to be able to find the position of any number between 0-100.
import javax.swing.*;
public class BinarySearch {
public static void main(String[] args) {
int array[] = new int[11];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
array[5] = 60;
[code]...
View Replies
View Related
Sep 15, 2014
The point is to use a binary search to determine how many steps it would take to get to int X, int Y in a grid that is N by N. Picture a parking lot that is square and your car is at (x,y). You can honk your horn from your key to determine the direction of the car. I need to return the amount of steps to get to the car. You can't step diagonally. I am currently getting an error that causes an infinite loop and I can't fix it.
public class ParkingLot {
public int search(int N, int X, int Y) {
int minX = 0, maxY = N, minY = 0, maxX = N;
int num = 0;
int curX, curY;
int newCurX, newCurY;
curX = (minX + maxX)/2;
curY = (minY + maxY)/2;
while (curX != X || curY != Y)
[code]....
View Replies
View Related
Dec 10, 2014
Righto, so I've crafted a binary search and a sequential search. The sequential search works perfectly fine.
However; my binary search doesn't. If I enter in incorrect data, it tells me the data I entered was incorrect. But if I enter in correct data, my sequential search tells me my datas correct, but binary search tells me I'm still incorrect. Here's my binary search + the test program.
public class ValidatorWilson
{
int[] accountNumbers = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
public boolean partTwo(int numberCheck)
{
if (accountNumbers.length == 0)
[Code] ....
View Replies
View Related