Please help for a LinkBinaryTreeInJava

Discussion in 'Java' started by Basanta, Mar 16, 2008.

  1. Basanta

    Basanta Guest

    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
     
    Basanta, Mar 16, 2008
    #1
    1. Advertising

  2. Basanta writes:

    > LinkBinTree(Object O) {
    > root = new BinNode(0);
    > }


    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.)
     
    Jussi Piitulainen, Mar 16, 2008
    #2
    1. Advertising

  3. Basanta

    Lew Guest

    Jussi Piitulainen wrote:
    > Basanta writes:
    >
    >> LinkBinTree(Object O) {
    >> root = new BinNode(0);
    >> }

    >
    > 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
     
    Lew, Mar 17, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JC
    Replies:
    4
    Views:
    8,030
    Jeff C
    Jul 8, 2004
  2. Replies:
    4
    Views:
    565
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    711
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,826
    Mike Hewson
    Jan 7, 2005
  5. mera
    Replies:
    7
    Views:
    1,156
Loading...

Share This Page