From:
Lew <lew@lewscanon.com>
Newsgroups:
comp.lang.java.help
Date:
Sun, 16 Mar 2008 15:38:41 -0400
Message-ID:
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
}

BinNode root;
int size;
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))

{
throw new TreeEmptyException("Tree doesn't

exists");

//"Tree doesn't exist"

}

}

public

BinNode <T> addRight( Node <T> n, T o ) throws TreeEmptyException{
if(!exists(n)) throw new TreeEmptyException("Tree doesn't
exists");
}

void preOrder(Node n) {
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

Generated by PreciseInfo ™
From Jewish "scriptures":

Rabbi Yitzhak Ginsburg declared, "We have to recognize that
Jewish blood and the blood of a goy are not the same thing."
(NY Times, June 6, 1989, p.5).
{"ft_lang_label":"__label__en","ft_lang_prob":0.5155723,"math_prob":0.7741763,"size":2660,"snap":"2022-27-2022-33","text_gpt3_token_len":720,"char_repetition_ratio":0.12236446,"word_repetition_ratio":0.072234765,"special_character_ratio":0.29699248,"punctuation_ratio":0.18060836,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95532286,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-30T12:53:18Z\",\"WARC-Record-ID\":\"<urn:uuid:15bbfc21-ec30-4b3d-9e13-ff4e75834e2a>\",\"Content-Length\":\"13946\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2a35d27f-d752-40f0-b641-6e04681913cd>\",\"WARC-Concurrent-To\":\"<urn:uuid:54be792b-36ad-42e2-aa73-5eedab672c59>\",\"WARC-IP-Address\":\"45.79.224.231\",\"WARC-Target-URI\":\"https://preciseinfo.org/Convert/Articles_Java/Interface_Code/Java-Interface-Code-080316213841.html\",\"WARC-Payload-Digest\":\"sha1:B5YMRRKQHPQSR7PEXHLJTREJXNWKG4KW\",\"WARC-Block-Digest\":\"sha1:RZQ7TQAIL7LBT6JDW2AL634Z3GXNFROX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103821173.44_warc_CC-MAIN-20220630122857-20220630152857-00178.warc.gz\"}"}