Home > Archive > Java Help > March 2008 > Please help for a LinkBinaryTreeInJava
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Please help for a LinkBinaryTreeInJava
|
|
| Basanta 2008-03-16, 7:50 pm |
| I am new in this group,I don't know how far it would help me.
I tried to make a linked based binary tree following the book "data
structures and algorithms in java" By goodRich and tamassia.I did all
but I wanted to implement a function to determine whether a particular
node exists or not.All I want to implement a function boolean
exists(node n).Can anyone help??My program is as follows:-
import java.io.*;
interface Node {
Object getData();
int getID();
}
class TreeEmptyException extends Exception {
TreeEmptyException(String message) {
super(message);
}
}
class LinkBinTree {
BinNode root;
int size;
LinkBinTree(Object O) {
root = new BinNode(0);
}
class BinNode implements Node {
int id;
BinNode left;
BinNode right;
Object data;
BinNode(int iden) {
id = iden;
left = null;
right = null;
data = null;
}
BinNode(Object O) {
id = 0;
left = null;
right = null;
data = null;
}
public Object getData() {return data;}
public int getID() { return id;}
void addLeft(Object obj) {
BinNode b = new BinNode(obj);
left = b;
}
void addRight(Object obj) {
BinNode r = new BinNode(obj);
right = r;
}
BinNode addLeft(Node n,Object O) throws TreeEmptyException
{
if(!exists(n)) throw new TreeEmptyException("Tree doesn't
exists");
return n.addLeft(O);
}
BinNode addRight(Node n,Object O) throws TreeEmptyException{
if(!exists(n)) throw new TreeEmptyException("Tree doesn't
exists");
return n.addRight(O);
}
void preOrder(Node n) {
LinkQueueApp<Integer> q =new LinkQueueApp<Integer>();
int p=n.getID();
q.enqueue(p);
while(!q.isEmpty()) {
p =q.dequeue();
System.out.println("The pre-order is : "
+n.getData());
for(int i=p;(i==p+1) || (i==p+2)&&i<=size;i++)
q.enqueue(i);
}
}
void boolean exists(Node n) {
if(Node == root) return;
else {
if(Node
| |
| Mark Space 2008-03-16, 7:50 pm |
| Basanta wrote:
> I am new in this group,I don't know how far it would help me.
> I tried to make a linked based binary tree following the book "data
> structures and algorithms in java" By goodRich and tamassia.I did all
> but I wanted to implement a function to determine whether a particular
> node exists or not.All I want to implement a function boolean
> exists(node n).Can anyone help??My program is as follows:-
>
> import java.io.*;
>
> interface Node {
> Object getData();
> int getID();
> }
This is kind of interesting. Does your book explain why nodes have both
a data element and an ID? Why not just use the data?
I feel this may make your tree special in some regard, and affect the
answer to your question. More info would help here. How do you search?
Do you look for data? ID? both?
| |
| Basanta 2008-03-16, 7:50 pm |
| On Mar 16, 10:52 pm, Mark Space <marksp...@sbc.global.net> wrote:
> Basanta wrote:
>
>
>
> This is kind of interesting. Does your book explain why nodes have both
> a data element and an ID? Why not just use the data?
>
> I feel this may make your tree special in some regard, and affect the
> answer to your question. More info would help here. How do you search?
> Do you look for data? ID? both?
I look only for ID.
| |
| Mark Space 2008-03-16, 7:50 pm |
| Basanta wrote:
> I look only for ID.
Can I ask why? What's the idea there?
Also, are ID comparable? Do they have a sorted order?
| |
| HelpMe 2008-03-16, 7:50 pm |
| On Mar 16, 11:26 pm, Mark Space <marksp...@sbc.global.net> wrote:
> Basanta wrote:
>
> Can I ask why? What's the idea there?
>
> Also, are ID comparable? Do they have a sorted order?
No,but I want to implement functions addLeft and addRight for which
existence of the node where adding is performed is mandatory.
| |
| Andrew Marcus 2008-03-16, 7:50 pm |
| On Mar 16, 11:46 pm, HelpMe <ShahilAkh...@gmail.com> wrote:
> On Mar 16, 11:26 pm, Mark Space <marksp...@sbc.global.net> wrote:
>
>
>
> I want to implement exists for add Left functions.
> No,but I want to implement functions addLeft and addRight for which
> existence of the node where adding is performed is mandatory.
| |
| Andrew Marcus 2008-03-16, 7:50 pm |
| On Mar 16, 11:50 pm, Andrew Marcus <mainhoonanja...@gmail.com> wrote:[color=darkred]
> On Mar 16, 11:46 pm, HelpMe <ShahilAkh...@gmail.com> wrote:
>
>
>
>
See,both of you helpme and basanta.It's so simple I don't think you
require help.Try it out.
| |
|
| Basanta wrote:
> I am new in this group,I don't know how far it would help me.
> I tried to make a linked based binary tree following the book "data
> structures and algorithms in java" By goodRich and tamassia.I did all
> but I wanted to implement a function to determine whether a particular
> node exists or not.All I want to implement a function boolean
> exists(node n).Can anyone help??My program is as follows:-
>
> import java.io.*;
>
> interface Node {
> Object getData();
> int getID();
> }
A generic approach (as a matter of style I include the redundant 'public' in
interface declarations):
interface Node <T>
{
T getData();
Integer getId(); // could parameterize ID type
}
> class LinkBinTree {
> BinNode root;
> int size;
> LinkBinTree(Object O) {
> root = new BinNode(0);
> }
Avoid exorbitant indentation. Up to four spaces per level will suffice.
class BinNode <T> implements Node <T>
> {
T data;
Integer id;
> BinNode left;
> BinNode right;
public BinNode( Integer iden )
> {
> id = iden;
> left = null;
> right = null;
> data = null;
You don't need to set things to null again.
}
> BinNode(Object O) {
Why do you have this constructor?
Parameters should have names that start with a lower-case letter: 'o', not 'O'.
> id = 0;
> left = null;
> right = null;
> data = null;
You don't need to set things to null or zero again.
> }
public T getData() {return data;}
> public int getID() { return id;}
>
>
> void addLeft( T obj ) {
> BinNode b = new BinNode(obj);
> left = b;
You don't need 'b':
left = new BinNode( obj );
> }
>
> void addRight( T obj ) {
> BinNode r = new BinNode(obj);
> right = r;
> }
>
public
> BinNode <T> addLeft(Node <T> n, T o ) // lower-case 'o'!
throws TreeEmptyException
> {
> if(!exists(n))
// braces please:
{
throw new TreeEmptyException("Tree doesn't
> exists");
//"Tree doesn't exist"
}
> return n.addLeft( o );
> }
>
public
> BinNode <T> addRight( Node <T> n, T o ) throws TreeEmptyException{
> if(!exists(n)) throw new TreeEmptyException("Tree doesn't
> exists");
> return n.addRight(O);
> }
>
> void preOrder(Node n) {
> LinkQueueApp<Integer> q =new LinkQueueApp<Integer>();
> int p=n.getID();
> q.enqueue(p);
> while(!q.isEmpty()) {
> p =q.dequeue();
> System.out.println("The pre-order is : "
> +n.getData());
> for(int i=p;(i==p+1) || (i==p+2)&&i<=size;i++)
> q.enqueue(i);
> }
>
> }
>
> void boolean exists( Node <T> n ) {
> if(Node == root) return;
You don't show a variable 'root'.
> else {
> if(Node
>
Are you searching for a certain Node, or a Node with a certain value?
public static void boolean exists( BinNode <T> root, T value )
{
if ( root == null )
{ return false; }
return (root.value == null? value == null : root.value.equals( value ))
|| exists( root.left, value ) || exists( root.right, value );
}
--
Lew
| |
|
| Jussi Piitulainen wrote:
> Basanta writes:
>
>
> Set your font so that the letter O and the digit 0 look different.
> (Same for I, l and 1, but your code happens to exhibit O and 0.)
He doesn't set a font. His message is in 'text/plain', meaning the font
you're seeing is set in your client.
I am disappointed that the OP multiposted. There are many answers in
clj.help, for those reading clj.programmer.
--
Lew
|
|
|
|
|