Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Binary Trees
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheZsterBunny




PostPosted: Mon Feb 07, 2005 10:18 pm   Post subject: (No subject)

right, moving back on topic.

Finally understood the mistake in my code. calling by value, rather than reference. well, something to that extent.

Still working on the del function, however.

code:

public class BTest
{
    static BTree oak;
    public static void main (String [] args)
    {
        oak = new BTree ();
        oak.add (new BNode (1));
        oak.add (new BNode (5));
        oak.add (new BNode (9));
        oak.add (new BNode (7));
        oak.add (new BNode (2));
        oak.add (new BNode (6));
        oak.add (new BNode (8));
        oak.add (new BNode (3));
        oak.add (new BNode (-4));
        oak.displayAll ();
    }
}
class BTree
{
    BNode root;

    public BTree ()
    {
        root = null;
    }


    public void add (BNode leaf)
    {
        if (root == null)
            root = leaf;
        else
            append (leaf, root);

    }


    public void del (BNode toPrune)
    {

    }


    public void displayAll ()
    {
        if (root != null)
            deeplook (root);
    }


    public void showPath (int value)
    {
        String path = "base";
        BNode tmp = root;
        while (tmp != null && tmp.val != value)
        {
            if (tmp.val > value)
            {
                tmp = tmp.l;
                path += ";left";
            }
            else
            {
                tmp = tmp.r;
                path += ";right";
            }
        }
        if (tmp.val == value)
            System.out.println (value + " @ " + path);
        else
            System.out.println (value + " not found");
    }


    private void append (BNode leaf, BNode branch)
    {
        if (branch.val > leaf.val)
            if (branch.l == null)
                branch.l = leaf;
            else
                append (leaf, branch.l);
        else
            if (branch.r == null)
                branch.r = leaf;
            else
                append (leaf, branch.r);
    }


    private void deeplook (BNode branch)
    {
        if (branch.l != null)
            deeplook (branch.l);
        //        System.out.print (branch.val);
        showPath (branch.val);
        if (branch.r != null)
            deeplook (branch.r);
    }
}

class BNode
{
    BNode l, r;
    int val;
    public BNode (int value)
    {
        val = value;
        l = r = null;
    }
}


-Z

--edit--
maybe next time i'll attach it ^_^
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 16 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: