Create Binary Tree From A String Of Letters

Jun 15, 2014

Here is the problem:

Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that this will not be a search tree; there’s no quick way to find a given node. You may end up with something like this:

It also says all Letters must be Leaves

Now I had it almost similar to that picture, but it wasn't right. So ive been working on it but im getting some very strange (and frustrating) output from the following methods.

Ive included the display method just for reference. The book told me to use it so I haven't edited it. I believe my main issue is with my (incomplete) insert() method. The output goes into an infinite loop despite having a return statement break the while loop when a character is inserted.

The way I see to solve the problem is just add a (+) whenever a new subtree needs to be created. Say I add A and B, then it first creates a subtree at the root with a (+) and afterwards lists A and B as its leaves. If I insert a C, it should be able to simply move to the right child of the root and deposit the C there.

package pkg4333_hwk1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class Main {

[Code] ....

View Replies


ADVERTISEMENT

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

GUI Program To Convert All Lowercase Letters In A String To Uppercase Letters

Mar 21, 2015

Write a GUI program to convert all lowercase letters in a string to uppercase letters, and vice versa.

For example, Alb34eRt will be converted to aLB34ErT.

View Replies View Related

Binary Tree - Storing Each Word As String And Int Frequency For Each Time Word Occurs

Apr 27, 2015

I have built a binary tree, from a file. In each node, I am storing each word as a string, and an int frequency for each time the word occurs. For the assignment, I need to find how many words occur only once in the file. I wrote the program, but for some reason I am getting a number different from what my professor is expecting.

As far as I know, this file has loaded into the tree correctly, because all of my other answers in the assignment are correct. What am I doing wrong?

public void findUnique() {
System.out.println("There are " + findUniqueWords(root, 0) + " unique words.");
}
private int findUniqueWords(Node subTree, int uniqueCount) {
// Base Case: At the end of the branch
if(subTree == null){
return uniqueCount;

[Code] ....

View Replies View Related

Binary Expression Tree

Apr 23, 2015

So everything in my program is working EXCEPT when it comes to calculating the result. I am supposed to evaluate the expression using postorder traversal to return the answer. I am honestly not sure how I would get the postorder traversal to return the answer to the expression since the only thing we really went over was how it re-ordered the expression so that it would end in the postorder/postfix order. Anyways, currently the way that I have the public int evaluate(Node node)method set up is giving me a result of 0, which obviously is not correct.

Here's the section that I'm having issues with:

public int evaluate(Node node){
if(node.isLeaf()){
return Integer.parseInt(node.value);
}
int result = 0;
int left = evaluate(node.left);
int right = evaluate(node.right);

[code]....

View Replies View Related

Binary Tree Depth

Apr 2, 2014

I have a list of people with a matching telephone extension number, the people are organised in a hierachy tree with some colleagues at the same level and then some junior colleagues. I have been trying to write code that can find the tree height of any given member but I am unable too. Here is my code so far but the left and right are not working because I have not declared them any where in my code.

Java Code:

//hierarchy rank section 7
// **Depth** from node to root (bottom up)
public int rank(Member p1){
//to do

[code]....

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

Inserting Into A Regular Binary Tree?

Nov 26, 2014

I know that for a binary search tree the insert() method is easy. You just kept comparing the values of the nodes, and you would know whether to go left or right depending on the value of the node.However, I am stumped when trying to figure out how to add a node into a normal binary tree where you just keep adding the nodes to the leftmost side, regardless of value.Here is my attempt at the insert() method:

public class RegularTree extends BinaryTree {
public CompleteTree() {
super();
}

public void insert(Comparable item) {
if(this.isEmpty()) {
this.setRoot(new TreeNode(item));
return;

[code]....

View Replies View Related

How To View Binary Tree Using Eclipse

Aug 7, 2014

Is there a way that I can view my entire binary tree on Eclipse? I remember the old IDE that I used, jGrasp, had a feature in its debugging that would allow me to see exactly what was going on and I want to know if Eclipse has the same thing.

View Replies View Related

Binary Threaded Tree Comparing

Dec 4, 2014

I have a little problem in my code. Using a threaded binary tree and inorder traversal check if every next element is equal to the previous element + 1. You are allowed to use additional functions as long as they are not recursive. I have implemented everything. Problem is that i cant compare elements as i should. They are type E.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.util.ArrayList;

[code]....

View Replies View Related

Java Binary Tree Code

Apr 5, 2014

I have written a code to get all the nodes below a current Node and it is working but I need to get it to look better if possible. To get the number of nodes below I have created an Array List, which I then go to the first Node below and add all the people to the Array list on the same level till I get to the end of the level, then I go down again until I cannot go down any more. Is there a way I can have my code without having to use an array List? I have put my code below. The brieff about the tree is that you have a parent and below the Parent are children who can have brothers and sisters next to them and after that they also have children below that. The method is trying to find the number of children below any given child

private LinkedList<Node> TempQueue = new LinkedList<Node>();
public int noOfYoungerChildren(Member p1){
Node tmp = find(p1);
return countYoungerChildren(tmp);

[code]....

View Replies View Related

JSP :: Binary Tree - Loops In JSTL

Jan 31, 2014

I create a binary tree and now I would like to display the result in my jsp with conditions.

Each node has 4 elements (id, question, answer, and leftnode rightnode)

If the user clicks "Yes", the program goes to the left of the tree and if he answers "no" to the right of the tree.

For example, the initial question is "you're a man?" If he answers "yes" we will go to the left and page view "you're a singer?" If he answers "no" page displays "you're French? ".

In java, it would

cursor [i]. GetQuestion ()

to the original question.

Then

cursor [i] getQuestion. GetLeftnode ()
For yes and
cursor [i]. GetRightnode (). GetQuestion
for no.

Until the, everything works normally, but when I want to loop until there is no longer any issue by

<c:when test="${not empty cursor[i].getQuestion() }">

The program stops at the first loop and stops it that cursor is not empty

Here is my complete code jstl

<c:set var="cursor" value="${nodes}" scope="session"></c:set>
<c:set var="i" value="0" scope="session"></c:set>
<form>
<c:choose>
<c:when test="${not empty cursor[i].getQuestion() }">
<c:out value="i vaut ${i } au debut de la boucle"></c:out>

[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

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

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

How To Get Correct Postorder In Binary Tree And Evaluate It

Apr 23, 2015

The assignment is meant to understand and practice tree concepts, tree traversals and recursion. I am requested to use the tree to print the expressions by three ways and evaluate the expression in post-order. This is my node class,

package hw10;
public class Node {
String value;
Node left, right;
public Node(String value) {

[Code] .....

When i test my code, some trouble like this:

Enter a prefix expression: - + 10 * 2 8 3
prefix expression:
- + 10 * 2 8 3
postfix expression:
+ 10 * 2 8 3 -
infix expression:
( ( 10 + ( 2 * 8 ) ) - 3 )

[Code] .....

the error lines:

75: double left = evaluate(node.left);
107: evaluate(root);

In the Instruction, which said:

- int evaluate(Node node)

Given the root node of an expression tree, evaluates the expression using post-order traversal and returns the answer. main method Testing logic.

And I found the postfix expression was wrong, it should be: 10 2 8 * + 3 -

I think maybe the tree's order had some problems result in these cases or...

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

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 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

Construct Binary Tree And Deserialize Used File

Mar 23, 2014

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

[Code] ....

So I am having trouble with trying to get the main method to work. The program is supposed to construct a BinaryTree, and deserialize a file that is used for the Tree.

View Replies View Related

Method Creation - Take A String With Duplicate Letters And Return Same String Without Duplicates

Nov 7, 2014

How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.

View Replies View Related

Parsing Arithmetical Expression - Creating Binary Tree?

Dec 2, 2014

Write a Java program to create a binary expression tree in which:  leaves are (double) numbers, and interior nodes are binary algebraic operators). As an example, the expression  1 2 + 3 *  could be represented as:

The input to the program is an arithmetical expression (already) in postfix notation. Use for testing the sample :   

4 8 2 / * 10  7 ‐ + ( equivalent to 4*8/2+(10‐7) ) .  

After creating the corresponding expression tree, print it’s traversal in Pre‐Order, In‐Order, and Post‐Order.Evaluate the expression and print its final value (for this example, result should be: 19.00).

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







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