Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Binary Tree in java(Generic)

Reply
Thread Tools

Binary Tree in java(Generic)

 
 
HelpMe
Guest
Posts: n/a
 
      02-20-2008
Please help me.I want to make a binary tree in java.I started but
couldnot accomplish.Can anyone help me to complete .Reply soon
please.My Program is:-

import java.io.*;

interface Node {
T getData();
int getId();
}

interface BinTree<T> {
Node getLeft (Node n);
Node getRight (Node n);
Node[] leaves();
Node parent(Node n);
int numOfChildren(Node n);
}

class ArrayBinTree<T> implements BinTree<T> {
class ArrayBinTreeNode<T> implements Node {
T data;
int id;
T getData {return data;}
int getId {return id;}

}

ArrayBinTreeNode () { //default constructor
id = 0;
data = null;
}
ArrayBinTree (int s){ //copy constructor
id = s;
data = null;
}

Node[] tree;
int numOfNodes;
BinTree(int size) {
tree = new Node[size];
numOfNodes = 0;
}

Node getLeft(Node n) {
return tree [2*n.getId()+1];
}

Node getRight(Node n) {
return tree [2*n.getId()+2];
}
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      02-20-2008

HelpMe wrote:
> Please help me.I want to make a binary tree in java.I started but
> couldnot accomplish.Can anyone help me to complete .Reply soon
> please.My Program is:-


not a program, as it's posted.

Start here:

public class BinaryTree {

Object root;
BinaryTree right;
BinaryTree left;

}

Forget about interfaces, polymorphism, and generics
until you can produce a plain old data structure, you
can add the fancy stuff later.

Here's a link that was helpful to me.
<http://www.brpreiss.com/books/opus5/html/book.html>


>
> import java.io.*;
>
> interface Node {
> T getData();
> int getId();
> }
>
> interface BinTree<T> {
> Node getLeft (Node n);
> Node getRight (Node n);
> Node[] leaves();
> Node parent(Node n);
> int numOfChildren(Node n);
> }
>
> class ArrayBinTree<T> implements BinTree<T> {
> class ArrayBinTreeNode<T> implements Node {
> T data;
> int id;
> T getData {return data;}
> int getId {return id;}
>
> }
>
> ArrayBinTreeNode () { //default constructor
> id = 0;
> data = null;
> }
> ArrayBinTree (int s){ //copy constructor
> id = s;
> data = null;
> }
>
> Node[] tree;
> int numOfNodes;
> BinTree(int size) {
> tree = new Node[size];
> numOfNodes = 0;
> }
>
> Node getLeft(Node n) {
> return tree [2*n.getId()+1];
> }
>
> Node getRight(Node n) {
> return tree [2*n.getId()+2];
> }



 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      02-21-2008

Jeff Higgins wrote:
>
> HelpMe wrote:
>> Please help me.I want to make a binary tree in java.I started but
>> couldnot accomplish.Can anyone help me to complete .Reply soon
>> please.My Program is:-

>
> not a program, as it's posted.
>
> Start here:
>
> public class BinaryTree {
>
> Object root;
> BinaryTree right;
> BinaryTree left;
>
> }
>
> Forget about interfaces, polymorphism, and generics
> until you can produce a plain old data structure, you
> can add the fancy stuff later.
>
> Here's a link that was helpful to me.
> <http://www.brpreiss.com/books/opus5/html/book.html>
>
>


import java.io.Serializable;

@SuppressWarnings("unchecked")
public class Node
implements Cloneable, Serializable, Comparable {

private static final long serialVersionUID = 1L;

private final Comparable data;

public Node(Comparable data) {
this.data = data; }

public int height() {
// TODO
return -1; }

public int depth() {
// TODO
return -1; }

public Object getData() {
return data; }

public int compareTo(Object that) {
if(that instanceof Node) {
return this.data.compareTo(((Node)that).data);
} throw new ClassCastException(); }

@Override
protected Object clone()
throws CloneNotSupportedException {
return super.clone(); }

@Override
public String toString() {
return data.toString(); }
}


import java.io.Serializable;
import java.util.Iterator;

@SuppressWarnings("unused")
public class BinaryTree
implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

private Node root;
private BinaryTree right;
private BinaryTree left;

public BinaryTree(){};

public BinaryTree (Node root) {
this.root = root; }

public Node getRoot() {
return root; }

public int height() {
// TODO
return -1; }

private class PreOrderIterator
implements Iterator<Node> {

@Override
public boolean hasNext() {
// TODO
return false; }

@Override
public Node next() {
// TODO
return null; }

@Override
public void remove() {
// TODO
}
}
}


public class TestTree {

public static void main(String[] args) {

Node root = new Node("root");
BinaryTree tree = new BinaryTree(root);
System.out.println(tree.getRoot()); }

}



>>
>> import java.io.*;
>>
>> interface Node {
>> T getData();
>> int getId();
>> }
>>
>> interface BinTree<T> {
>> Node getLeft (Node n);
>> Node getRight (Node n);
>> Node[] leaves();
>> Node parent(Node n);
>> int numOfChildren(Node n);
>> }
>>
>> class ArrayBinTree<T> implements BinTree<T> {
>> class ArrayBinTreeNode<T> implements Node {
>> T data;
>> int id;
>> T getData {return data;}
>> int getId {return id;}
>>
>> }
>>
>> ArrayBinTreeNode () { //default constructor
>> id = 0;
>> data = null;
>> }
>> ArrayBinTree (int s){ //copy constructor
>> id = s;
>> data = null;
>> }
>>
>> Node[] tree;
>> int numOfNodes;
>> BinTree(int size) {
>> tree = new Node[size];
>> numOfNodes = 0;
>> }
>>
>> Node getLeft(Node n) {
>> return tree [2*n.getId()+1];
>> }
>>
>> Node getRight(Node n) {
>> return tree [2*n.getId()+2];
>> }

>
>



 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-21-2008
Jeff Higgins wrote:
> @SuppressWarnings("unchecked")
> public class Node
> implements Cloneable, Serializable, Comparable {


It would be fun to genericize this such that the "unchecked" warnings need not
be suppressed.

--
Lew
The wolf ate the moon tonight.
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      02-21-2008

Lew wrote:
> Jeff Higgins wrote:
>> @SuppressWarnings("unchecked")
>> public class Node
>> implements Cloneable, Serializable, Comparable {

>
> It would be fun to genericize this such that the "unchecked" warnings need
> not be suppressed.
>


How do you do that?

JH


 
Reply With Quote
 
Lord Zoltar
Guest
Posts: n/a
 
      02-21-2008
On Feb 20, 1:33*pm, HelpMe <ShahilAkh...@gmail.com> wrote:
> Please help me.I want to make a binary tree in java.I started but
> couldnot accomplish.Can anyone help me to complete .Reply soon
> please.My Program is:-


The Java API has a TreeMap class. Would that suffice?
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-21-2008
Jeff Higgins wrote:
> Lew wrote:
>> Jeff Higgins wrote:
>>> @SuppressWarnings("unchecked")
>>> public class Node
>>> implements Cloneable, Serializable, Comparable {

>> It would be fun to genericize this such that the "unchecked" warnings need
>> not be suppressed.
>>

>
> How do you do that?


public class Node implements Cloneable, Serializable,
Comparable <Node>
{
....

--
Lew
 
Reply With Quote
 
voorth
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 3:25 pm, Lord Zoltar <lord.zol...@gmail.com> wrote:
> On Feb 20, 1:33 pm, HelpMe <ShahilAkh...@gmail.com> wrote:
>
> > Please help me.I want to make a binary tree in java.I started but
> > couldnot accomplish.Can anyone help me to complete .Reply soon
> > please.My Program is:-

>
> The Java API has a TreeMap class. Would that suffice?


I'm afraid not. The TreeMap is an implementation of SortedMap that
stores its keys in a Red-Black tree. It is not a tree implementation.
 
Reply With Quote
 
Lord Zoltar
Guest
Posts: n/a
 
      02-21-2008

> > The Java API has a TreeMap class. Would that suffice?

>
> I'm afraid not. The TreeMap is an implementation of SortedMap that
> stores its keys in a Red-Black tree. It is not a tree implementation.


I'm pretty sure TreeMap is a tree implementation, as it implements a
Red-Black tree (and Red-Black trees are a form of binary search tree).
Do you need the actual source code for an implementation of a tree and
not just an API to use a tree implementation?
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      02-21-2008

Lew wrote:
> Jeff Higgins wrote:
>> Lew wrote:
>>> Jeff Higgins wrote:
>>>> @SuppressWarnings("unchecked")
>>>> public class Node
>>>> implements Cloneable, Serializable, Comparable {
>>> It would be fun to genericize this such that the "unchecked" warnings
>>> need not be suppressed.
>>>

>>
>> How do you do that?

>
> public class Node implements Cloneable, Serializable,
> Comparable <Node>
> {
> ...


Oh! OK that's kinda neat, thanks.

import java.io.Serializable;

public class Node<T extends Comparable<T>>
implements Cloneable, Serializable, Comparable<Node<T>> {

private static final long serialVersionUID = 1L;

private final T data;

public Node(T data) {
this.data = data; }

public int height() {
// TODO
return -1; }

public int depth() {
// TODO
return -1; }

public T getData() {
return data; }

@Override
protected Object clone()
throws CloneNotSupportedException {
return super.clone(); }

@Override
public String toString() {
return data.toString(); }

@Override
public int compareTo(Node<T> that) {
return this.data.compareTo(that.data); }
}


import java.io.Serializable;
import java.util.Iterator;

@SuppressWarnings("unused")
public class BinaryTree<T extends Comparable<T>>
implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

private Node<T> root;
private BinaryTree<T> right;
private BinaryTree<T> left;

public BinaryTree() {};

public BinaryTree(Node<T> root) {
this.root = root; }

public Node<T> getRoot() {
return root; }

public int height() {
// TODO
return -1; }

private class PreOrderIterator
implements Iterator<Node<T>> {

@Override
public boolean hasNext() {
// TODO
return false; }

@Override
public Node<T> next() {
// TODO
return null; }

@Override
public void remove() {
// TODO
}
}
}


public class TestTree {

@SuppressWarnings("boxing")
public static void main(String[] args) {

Node<Integer> root =
new Node<Integer>(Integer.MAX_VALUE);
BinaryTree<Integer> tree =
new BinaryTree<Integer>(root);
System.out.println(tree.getRoot());
}
}



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary tree search vs Binary search Bogdan C Programming 22 10-21-2010 09:46 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 2 10-31-2007 02:58 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 1 10-30-2007 11:01 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 4 10-30-2007 08:21 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57