Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Please help...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 8 Topic 15884 of 16566
Post > Topic >>

Re: Please help for a LinkBinaryTreeInJava

by Lew <lew@[EMAIL PROTECTED] > Mar 16, 2008 at 03:38 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:-
> 
> im****t 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
 




 8 Posts in Topic:
Please help for a LinkBinaryTreeInJava
Basanta <mainhoonanjaa  2008-03-16 10:42:29 
Re: Please help for a LinkBinaryTreeInJava
Mark Space <markspace@  2008-03-16 10:52:12 
Re: Please help for a LinkBinaryTreeInJava
Basanta <mainhoonanjaa  2008-03-16 11:04:18 
Re: Please help for a LinkBinaryTreeInJava
Mark Space <markspace@  2008-03-16 18:26:35 
Re: Please help for a LinkBinaryTreeInJava
HelpMe <ShahilAkhtar@[  2008-03-16 11:46:10 
Re: Please help for a LinkBinaryTreeInJava
Andrew Marcus <mainhoo  2008-03-16 11:50:04 
Re: Please help for a LinkBinaryTreeInJava
Andrew Marcus <mainhoo  2008-03-16 11:53:18 
Re: Please help for a LinkBinaryTreeInJava
Lew <lew@[EMAIL PROTEC  2008-03-16 15:38:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 16:21:14 CST 2008.