BinaryTree

H

HelpMe

I want to implement a link-list based binary tree.Can anyone give me
some sorts of idea??I shall be very grateful to him.
 
H

HelpMe

I want to implement a link-list based binary tree.Can anyone give me
some sorts of idea??I shall be very grateful to him.
You can assume the binary tree to be a proper one.
 
L

Lew

Jeff said:
Ok.
Go ahead.
You're welcome.
You can assume this idea is the proper one.

<http://en.wikipedia.org/wiki/Binary_tree>
<http://en.wikipedia.org/wiki/Binary_search_tree>

Within the tree, each node has a value and a link set, since you say you want
to be link-based. I don't really think "linked list" applies; you are talking
about a "linked tree".

Here's an example implementation that permits only one node with a given value
in a tree:

<sscce>
package testit;
public class Node <T extends Comparable <? super T>>
implements Comparable <Node <T>>
{
private final T value;
private Node <T> left, right; // the links

public Node( final T v )
{
this.value = v;
}

public final T getValue()
{ return value; }

public final Node <T> getLeft()
{ return left; }
public final void setLeft( Node <T> v )
{ left = v; }

public final Node <T> getRight()
{ return right; }
public final void setRight( Node <T> v )
{ right = v; }

public int compareTo( Node <T> o )
{
return (this == o? 0
: o == null? 1
: value == null? (o.value == null? 0 : -1)
: o.value == null? 1 /* guarantee symmetry */
: value.compareTo( o.value ));
}

@Override public boolean equals( Object o )
{
try
{
return equals( getClass().cast(o) );
}
catch ( ClassCastException exc )
{
return false;
}
}

public boolean equals( Node <T> o )
{
return (this == o || (o != null
&& (value == null? o.value == null : value.equals( o.value ))
));
}

@Override public int hashCode()
{
return (value == null? 0 : value.hashCode());
}

@Override public String toString()
{
return value.toString();
}
}
</sscce>
 
A

Arved Sandstrom

HelpMe said:
I want to implement a link-list based binary tree.Can anyone give me
some sorts of idea??I shall be very grateful to him.

Data Structures and Algorithms in Java, Robert Lafore, SAMS Publishing,
2002.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top