1

I was just having a simple implementation query.

So I make a BST by using the following code:

class Node{

    int data;
    Node left=null;
    Node right=null;
    Node link=null;

    public Node(int d)
    {
        data=d;
    }

    public void append(int d)
    {
        Node n=this;
        Node nval=new Node(d);
        if(n==null)
        {
            n.data=d;
        }
        else
        {   boolean done=false;
            while(!done)
            {
                 if(n.data<=d)
                 {
                     if(n.left==null)
                     {
                         n.left=nval;
                         done=true;
                     System.out.println("Data Entered "+nval.data);
                     }
                     else
                     {
                         n=n.left;
                     }
                 }
                 else
                 if(n.data>d)
                 {
                     if(n.right==null)
                     {
                         n.right=nval;
                         done=true;
                     System.out.println("Data Entered "+nval.data);
                     }
                     else
                     {
                         n=n.right;
                     }
                 }
            }
        }
    }
}

Now, I started applying Breadth first and Depth First search on it. I was having genuine issues with doing this.

For DFS, I have to add in the left and right values of the current node that is placed on a stack right? How would I program this? I was having problems with doing this using a Linked List? Can someone tell me how the data structure or pointers should be?

The same thing happens with BFS. In case I wasn't clear before, my main problem is removing an array element and then replacing it with its children.

3
  • 1
    if (n == null) { n.data = d; } Are you trying to get NullPointerExceptions or is this a typo? Commented Dec 25, 2012 at 1:41
  • @ValekHalfHeart - Not to worry, a couple lines before there's an assignment Node n = this; so n can never be null. Commented Dec 25, 2012 at 1:43
  • Yeah, I just got that!! Thanks! Commented Dec 25, 2012 at 1:58

2 Answers 2

2

A Queue (FIFO) generally works well for a BFS. It need not be a Priority Queue, but it often is because giving weights is very common:

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out). Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

Basic "algorithm" rules for BFS with a Queue:

  1. Put initial state(s) in Q (the Queue)
  2. Take the head of the Q (see remove)
  3. Do something with the value taken (e.g. parent -> [child1, child2 ..])
  4. Append any results from step #3 to the tail of the Q (see add)
  5. Go back to step #2 until Q is empty or other end-case is achieved

Arrays are just a PITA to deal with past initialization and iteration. "Slicing" and "resizing" tends to be particularly painful in Java.

Sign up to request clarification or add additional context in comments.

Comments

0

for DFS(FILO), u just need to use a stack

for each node, push his right child to the stack first, then push the left child

for BFS(FIFO), u should use queue as @pst mentioned

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.