Creating A Tree In Java

Sep 9, 2014

how to create a tree data structure in java.I tried with a class consisting of node field and arraylist child nodes. but it does not satisfy the requirement.the requirement is that,

root: child1,child2,child3
child1:child4,child5
child2:child6,child7

on traversing it should print all the nodes as given above.no node should have same elements as child.

View Replies


ADVERTISEMENT

Java Tree Structure - Build Tree Based On Traversal Results

Jun 17, 2014

How to do draw the original binary tree based on traversal results?

A binary tree has this pre-order traversal result: A,B,D,H,I,E,F,C,G,K,J (TreeNodes) And the same tree gives the following in-order traversal: B,H,I,D,A,C,F,E,K,G,J. Can you draw the tree structure?

View Replies View Related

Swing/AWT/SWT :: Creating New Leaves In Tree

Aug 26, 2014

Having trouble adding leaves to my tree. Developing a stand alone Java app using mainly SWT and Eclipse JavaEE on Windows.The tree is like this:

root
------parentA
--------------childA1
--------------childA2
--------------childA3
------parentB
------parentC
--------------childC1
--------------childC2

My requirement: when a child is selected/clicked I want to create a grandchild (there could be from zero to say 12 grandchildren). Thus if user clicks/selects childA2:

root
------parentA
--------------childA1
--------------childA2
----------------------grandchildA21
--------------childA3
------parentB
------parentC
--------------childC1
--------------childC2

Codewise I have:

import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

Question one: do I need any further tree related imports to get this to work?

final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE );
tree.setHeaderVisible(true);
tree.setBounds(975, 00, 375, 600);

[code]...

And it is here I've tried lots of options but it's like bleak mid winter - no new leaves What seems odd is that I can see the selected/clicked child text item.getText() but if I look at the index it is always -1. And I thought the index would let me create the leaf.

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

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

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

Java N-node Tree

Mar 30, 2015

I'm trying to write Java code for the following figure. Where, if node:4 will create following separate 4 trees. Number of node in

LEVEL1 (yellow) : n node.
LEVEL2 (green) : n-1 node.
LEVEL3 (blue) : n-2 node.

See image for node:4.
4node.jpg

The example out for node:4 :
012
013
021
023
031
032 and so on.

View Replies View Related

Java B-Tree Insert (no Libraries)

Apr 11, 2014

My task is to implement a B-Tree data structure in Java using no libraries.

My current problem is inserting when the root is full, thus the middle key goes to root.keys[0] and then it get the left and right side which are new Nodes separating and inserting their keys. My problem is that for some reason there is a random second number when in the root and secondly my boolean leaf is always false meaning it never detects that the Tree is deeper than root.

The coding makes sense to me and I have tried printing everywhere but still can't seem to find the problem.

public class BTree {
/*
1. You may not modify the public interface of this class. You may however add any additional methods and/or field which you may require to aid you in the completion of this assignment.
 
2. You will have to design and implement a Node class. The BTree should house Integer objects.
 
3. You will notice that there are some overloaded methods, some of which work for Integer objects and some with primitive type int. You have to find a way to implement the methods to work with both types.
*/
 
class BTreeNode { 
boolean leaf = true;
int numKeys = 1;
int mOrder;
Integer keys[];

[Code] ....

View Replies View Related

Java B-Tree Insert (no Libraries)

Apr 11, 2014

My task is to implement a B-Tree data structure in Java using no libraries.

My current problem is inserting when the root is full, thus the middle key goes to root.keys[0] and then it get the left and right side which are new Nodes separating and inserting their keys. My problem is that for some reason there is a random second number when in the root and secondly my boolean leaf is always false meaning it never detects that the Tree is deeper than root.

The coding makes sense to me and I have tried printing everywhere but still can't seem to find the problem.

public class BTree {
/*
1. You may not modify the public interface of this class. You may however add any additional methods and/or field which you may require to aid you in the completion of this assignment.
2. You will have to design and implement a Node class. The BTree should house Integer objects.
3. You will notice that there are some overloaded methods, some of which work for Integer objects and some with primitive type int. You have to find a way to implement the methods to work with both types.
*/

class BTreeNode {
boolean leaf = true;
int numKeys = 1;
int mOrder;
Integer keys[];

[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

Parsing Tree Structure In Java

Jun 26, 2015

I have an excel file with data listed as the following, i m trying to parse down using POI
 
A
     B
          C
               D1
               D2
          F
               G1
               G2
               G3
          M
               S1
     R
          T             
     U
L
  X
     Y
         Z
 
is it possible to generate an output like the following
 
A
A-->B
A-->B-->C
A-->B-->C-->D1
A-->B-->C-->D2
A-->B-->F
A-->B-->F-->G1
A-->B-->F-->G2
A-->B-->F-->G3
A-->B-->M
A-->B-->M-->S1
A-->R
A-->R-->T
A-->U
L
L-->X
L-->X-->Y
L-->X-->Y-->Z
 
I have been trying from quite some time but havent figured out the logic ...

View Replies View Related

Find Minimum Spanning Tree Java

Mar 20, 2015

I am using the following code: But doesn't work fine. I try to find the Minimum spanning and print it out from house to which hose.

public class MinimumSpanning {
public static void main(String[] args) {
// [][] edge= are price between each hose edgs.
int [][] edge= {{0,7,9,12,0,0,0},
{7,0,0,10,0,0,0},
{10,0,0,12,0,0,0},
{12,10,12,0,11,0,10},
{0,0,0,11,0,4,8},
{0,0,0,0,4,0,6},
{0,0,0,10,8,6,0}};

[code]....

It should only print one time. Example between K to L prints many time.

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

Build Tree Structure From Array In Java

May 31, 2014

I never used tree, node etc. Consider an array of strings that come from html tags. This needs to be turned into a tree structure. Here, any Hn is the child of the most recent Hn-1

String[] Headers = {"H1", "H1", "H2", "H3", "H3", "H2", "H2", "H3",
"H4", "H2", "H2", "H2", "H1", "H2", "H2", "H3", "H4", "H4", "H2" };

Desired output -

ROOT
H1
H1
...H2
......H3
......H3
...H2
...H2
......H3
.........H4
...H2
...H2
...H2
H1
...H2
...H2
......H3
.........H4
.........H4
...H2

ANSWER -

class Example {
static class Node {
final String name;
final int indent;
Collection<Node> children = new LinkedList<> ();

[Code] .....

View Replies View Related

Swing/AWT/SWT :: JTree - Drag And Drop Inside One Tree - Java 1.6

Jul 16, 2008

I am trying to drag and drop tree nodes within the same JTree. I have a code which uses Java 1.2 java.awt.dnd.I would like to use TransferHandler and newer implementation.I have found a code which works when JTree is drop target but there is no code where Jtree is drag source and drop target.

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

Creating Rectangle In Java

Apr 16, 2015

I have a problem with creating a rectangle in Java. When I create the first rectangle all is ok. But the next, is the problem.

View Replies View Related

Creating XML File In Java

Jul 30, 2014

I'm getting a DOMException, "HIERARCHY_REQUEST_ERR". I know that the problem is from the following code towards the bottom in my function, but I don't know how to deal with it. When I get rid of doc.appendChild(staff);, I get rid of the problem, but it obviously doesn't add the new entry to my root element.

Element staff = doc.createElement("Staff");
doc.appendChild(staff);

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;

[Code] .....

View Replies View Related

Creating Task Manager Using Java

Dec 5, 2010

I want to create a TASK MANAGER for windows OS using java. Of course i am not expecting any code snippets, that is, what are the classes to be included and moreover HOW to do it.

View Replies View Related

CheckerBoard Won't Appear - Creating A Game In Java

Mar 8, 2015

Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing.

The section of were my board should be present is just a black space. Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong.

CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
application.pack(); // Use preferred size of content to set size of application.
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
application.setLocation( (screensize.width - application.getWidth())/2,
(screensize.height - application.getHeight())/2 );

[Code] ....

I have noticed is that within the BoardComplete(), if you change the setBackground(Color.BLACK) to WHITE, it will change the colour of the section to white, but the checkboard is still unshown.

View Replies View Related

Creating Java Linear Algorithm

Oct 13, 2014

I'm finding common elements in a collection of arrays. This is my code so far.

import java.util.ArrayList;
import java.util.List;
public class CommonElements //< T extends Comparable<T> >
{
Comparable[] comparable;
Comparable[] tempArr;
Comparable[] tempArr1;
Comparable[] newArray = new Comparable[tempArr.length];
int comparisonCount;
 
 [code]....

I'm using this method to sort through my array. The method accepts a collection of arrays (of varying length and of any type) as input, the algorithm input should be no greater than n(k-1). I know I can solve it using a quadratic algorithm, but it won't meet the requirement of the assignment. I did have an idea of storing all my algorithm in one giant array. After storing as one giant array I was going to sort it. After sorting I was going to compare each array side by side and see if they are the same. Here is note my teacher provided.

Note About Testing You will need to develop several sets of test collections for testing your algorithm. The grading rubric mentions covering the case where all the test collections have the same length, as well as covering the case where the test collections are of different lengths. You will also need to think about what constitutes the worst case scenario for this algorithm, since only that scenario will make your analysis of total comparisons performed a meaningful one. You can use the formulas in the grading rubric to tell you how many comparisons you should expect in the quadratic and linear cases. For example, if you have 5 total collections (1 query collection and 4 test collections), each of which contains 10 elements, the total number of comparisons performed in the worst case should be: (k - 1)N2, which for k = 10 and N = 10 is: (5 - 1)102, or 400 comparisons. For the linear algorithm, you should only have N*(k - 1), which is 10*(5 - 1), or 40 comparisons.

Here is Q/A that my teacher provided:

1. Are the elements in the collections already sorted?The input collections may or may not be sorted. Don’t assume that they are sorted.

2. Can I sort the elements in the collections? Am I supposed to sort the elements in thecollections?Yes, you can sort the elements in the collections. It is not required that you do so, however..

3. If I sort the collections, should I count the element comparisons that are performed by the sorting algorithm?No. The only element comparisons you should count are the ones that are directly used to find the common elements. Ignore comparisons performed by any sorting algorithms you use.

4. How do I extract an individual collection from the Object[] collections argument of the findCommonElements method?You will need to typecast each collection as type Comparable[]. For example:Comparable[] currentCollection = (Comparable[])collections[i];

5. Why are we using a one-dimensional Object array instead of a two-dimensional array of type Comparable (e.g., Comparable[][])?In Java, arrays are treated as objects, so only a variable of type Object[] can contain arrays. A variable of type Comparable[] can only contain Comparable elements, not arrays. A variable of type Comparable[][] also can only store Comparable elements. Although it is possible to organize the elements such that all the elements at indexes [1][i], for example, are considered to comprise a collection, this is not the same as having an array that contains other arrays. The additional bookkeeping is required to keep a 2D array organized is more complex than simply performing the typecast described above.

6. Can I use other data structures besides arrays; e.g., HashMaps?No. Although it is easier from a programming perspective to use higher order data structures like HashMaps to find common elements, using these structures simply hides the details. If you were to examine the implementations of those structures, you would probably find that they use relatively inefficient algorithms such as sequential searches to find elements. Additionally, if you use library components, you have no way of counting the number of comparisons performed, since you don’t have access to the source code of those components.

7. Is this a trick question? I can’t think of any way to get below NlogN comparisons.It is not a trick question. It is possible to solve this problem using a number of comparisons proportional to (k – 1) * N.

View Replies View Related

How To Prevent Java From Creating Object

Mar 6, 2014

I just want to ask about a kind inheritance.Let say I have an interface MachineCode.I also have different classes, Binary, Hex and Octal that implements MachineCode

Question: How can I prevent java to create an Object like this:

Binary bin = new Binary();
Hex hex = new Hex();
Octal octal = new Octal();

those declaration above must be compile error,

I want to create Objects of Binary, Hex, and Octal this way:
MachineCode bin = new Binary();
MachineCode hex = new Hex();
MachineCode octal = new Octal();

View Replies View Related

Creating A Guessing Game In Java

Nov 30, 2014

I am creating a Guessing game program in java code. I am having an issue with the guess class and main/tester class running. The instructions for the game are The computer generates a random # and the user must guess that number in 7 or fewer guesses. If the guesses exceed 7, then the game is over and the user is asked if they want to 'play again?'These are things I need to incorporate into my code:

If Statement

A Loop of some kind
At least three imported methods
At least two methods you create
Obtains input from the user
At least two instance variables
At least two local variables
Some form of concatenation
At least two calculations

import java.util.Random;
public class Guess
{
int computersNumber; // A random number picked by the computer.
int usersGuess = 0; // A number entered by user as a guess.
int guessCount = 0; // Number of guesses the user has made.

[code]...

View Replies View Related

Process To Use Java SE API In Netbeans While Creating GUI?

Mar 3, 2014

I would like to use java se api names like, undo, redo, stylededitorkit, htmleditorkit in editorpane swing. So, I don't understand how to use these apis.

View Replies View Related

Creating JLabels - Slider In Java Swing

Feb 22, 2014

I have a problem with slider which i want to create jlabels in a panel by sliding the slider and get the value but the jlabel doesn't show in jpanel. Below is the code :

JPanel PanelBoxes;
JPanel panel;
JLabel c;
public static void main(String[] args){
DividePanel div = new DividePanel();
div.go();

[Code] ....

View Replies View Related

Creating Tiled Based Map Game In Java?

May 10, 2014

I'm trying to create a tile based map JPanel but all I get is a white screen. I'm fairly new to the Java Swing and AWT package so I've been watching tutorials on YouTube so learn as much as I can. I don't know where I'm going wrong.

I've got three classes: Window.java which includes the Main method, Panel.java which is the JPanel and Tile.java to draw all the images into an array.

Window.java:

import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
setTitle("Project");
setSize(500, 400);
setLocationRelativeTo(null);

[Code] ....

Panel.java:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class Panel extends JPanel implements Runnable {
private Image dbImage;

[code]....

I've checked through everything and still cannot find what I'm doing wrong. I did try different codes but I just got errors instead.

View Replies View Related







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