Binary Tree Traversal

Standard

I thought long before of writing this post after discussing the binary tree generation in reverse lexicographic order but unfortunately it got delayed. So lets not make any more delay in discussing binary tree traversal.

Following are the general way of traversing binary tree, which you can find in any data structure book:-

1) Preorder :-

sample recursive code:

preorder(node)
{
           if(node!=null)
           {
                visit(node);
                preorder(left(node));
                preorder(right(node));
           }
}

Non-recursive traversal:-

preorder(root)
{
            p=root;
            stack S;
            while(p is not null or S is not empty) 
            {
              if(p is not null)
              {
                    visit(p);
                    push(p,S);
                    if(left(p) is not null)
                            p=left(p);
                   else
                            p=right(p);
             }
             else
             {
                      do
                     {
                           q=S.pop();
                           if( S is not empty)
                                rtptr=right(item(S));
                           else
                                 rtptr=null
                     }while(S is not empty and q is rtptr);
                     p=rtptr;
           }

}

2) Inorder :-

inorder(node)
{
         if(node!=null)
         {
             inorder(left(node));
              visit(node);
             inorder(right(node));
        }
}

3) Postorder :-

postorder(node)
{
       if(node!=null)
       {
             postorder(left(node))
             postorder(right(node));
             visit(node);
        }
}

4) Level-wise binary tree traversal.

We can have some more traversal based on the way we traverse left and right child. But here all the traversal require some extra data structure like stack. Hence we will discuss some more tree traversal which are space effective.

Link Inversion Traversal

This is very space effective and beautiful algorithm. In which we assign 1 bit extra information which store  0 or 1.  Initially, all the node have 0. As we descend to the right, we tag that node as 1 and make the right pointer of the node point to parent, whose right subtree is to be traversed. And as we descend to the left, we tag that node as 0 and make the left pointer point to parent, whose left subtree is to be traversed. Once we finish the traversing the subtree, can look at the tag of the node and decide whether it was the left or right subtree of the parent whose traversal has just been finished, this way we traverse back up to root. While traversing, we should restore the tag and its pointers also.

This tree traversal does not require any additional data structure and it just need n extra bit to tag each node. Lets improve this also.

Robson Traversal

This is even more space effective than the “Link Inversion Traversal”, since it does not require even  n bits extra information. It uses the leave node to store the information, basically it connects all the leave node and use them as stack and hence we need an extra pointer to point to start or end of this stack and we call that pointer as stack and one more extra pointer we will call that top,which will point to the last (recent) traversed node whose right tree we are traversing and which have non null left subtree and stack pointer’s right will point to the next most recent and its left pointer will point to the next leave node who will store the similar information.

Here is the algorithm:-


If the node's left subtree is being traversed, then
        its left pointer is pointing to its predecessor, and its right pointer is undisturbed.

If the node's right subtree is being traversed, then
        if the node has a null left subtree, then
                its right pointer is pointing to its predecessor
        else
                its left pointer is pointing to its predecessor, and its right pointer is pointing   to the root of the node's left subtree.


Here is the java implementation of above discussed tree traversal:-

package edu.temple.pt.assignment;
/**
 * 
 * @author aakash
 *
 * @param  <  T  &rt;   
 */
public class Node <  T  &rt;    {

	/*
	 * taking one extra tag data for link inversion traversal, not needed in any other traversal.
	 */
	private int tag=0;
	public int getTag() {
		return tag;
	}
	public void setTag(int tag) {
		this.tag = tag;
	}


	private T data;
	private Node <  T  &rt;    lt=null;
	private Node <  T  &rt;    rt=null;
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public Node <  T  &rt;    getLt() {
		return lt;
	}
	public void setLt(Node <  T  &rt;    lt) {
		this.lt = lt;
	}
	public Node <  T  &rt;    getRt() {
		return rt;
	}
	public void setRt(Node <  T  &rt;    rt) {
		this.rt = rt;
	}
	
	public Node(T data) {
		this.data=data;
		this.tag=0;
		// TODO Auto-generated constructor stub
	}
	/**
	 * @param data
	 * @param lt
	 * @param rt
	 */
	public Node(T data, Node <  T  &rt;    lt, Node <  T  &rt;    rt) {
		super();
		this.data = data;
		this.lt = lt;
		this.rt = rt;
		this.tag=0;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((data == null) ? 0 : data.hashCode());
		result = prime * result + ((lt == null) ? 0 : lt.hashCode());
		result = prime * result + ((rt == null) ? 0 : rt.hashCode());
		return result;
	}
	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Node <  T  &rt;    other = (Node <  T  &rt;   ) obj;
		if (data == null) {
			if (other.data != null)
				return false;
		} else if (!data.equals(other.data))
			return false;
		if (lt == null) {
			if (other.lt != null)
				return false;
		} else if (!lt.equals(other.lt))
			return false;
		if (rt == null) {
			if (other.rt != null)
				return false;
		} else if (!rt.equals(other.rt))
			return false;
		return true;
	}
	@Override
	public String toString()
	{
		StringBuilder sbr=new StringBuilder();
		sbr.append("[");
		sbr.append("{data:").append(data.toString());
//		sbr.append(" tag:").append(tag);
		sbr.append("}");
		if(lt!=null)
			sbr.append(",{left:").append(lt.getData()).append("}");
		else
			sbr.append(",{left:").append(lt).append("}");
		if(rt!=null)
			sbr.append(",{right:").append(rt.getData()).append("}");
		else
			sbr.append(",{right:").append(rt).append("}");
		/*
		if(lt!=null)
			sbr.append(",{left:").append(lt.toString()).append("}");
		else
			sbr.append(",{left:").append(lt).append("}");
		if(rt!=null)
			sbr.append(",{right:").append(rt.toString()).append("}");
		else
			sbr.append(",{right:").append(rt).append("}");
		*/
		sbr.append("]");
		return sbr.toString();
	}

	
	public static void main(String args[])
	{
		System.out.println(new Node <  Integer  &rt;   (new Integer(10)).toString());
	}	
}

package edu.temple.pt.assignment;


import java.util.Stack;
/**
 * 
 * @author aakash
 *
 * @param  <  T  &rt;   
 */
public abstract class BinaryTree <  T  &rt;    {

	protected Node <  T  &rt;    root=null;
	public Node <  T  &rt;    getRoot() {
		return root;
	}

	public void setRoot(Node <  T  &rt;    root) {
		this.root = root;
	}

	protected abstract Node <  T  &rt;    createTree();
	
	public void postOrderTraversal()
	{
		
	}
	public void inOrderTraversal()
	{
		
	}
	public void preOrderTraversal()
	{
		System.out.println("\n=============================");
		System.out.println("Preorder traversal::");
		System.out.println("===============================");
		Node <  T  &rt;    p=root;
		Stack <  Node <  T  &rt;     &rt;    stack=new Stack <  Node <  T  &rt;     &rt;   ();
		while(p!=null||!stack.isEmpty())
		{
			Node <  T  &rt;    rtptr=null;
			if(p!=null)
			{
				visit(p);
				stack.push(p);
				if(p.getLt()!=null)
					p=p.getLt();
				else
					p=p.getRt();
			}
			else
			{
				Node <  T  &rt;   q=null;
				do
				{
					q=stack.pop();				
					if(!stack.isEmpty())
						rtptr=stack.peek().getRt();
					else
						rtptr=null;
				}
				while(!stack.isEmpty()&&q==rtptr);
				p=rtptr;
			}
		}
	}
	public void visit(Node <  T  &rt;    data)
	{
		System.out.println(data.toString());
	}
	
	public void visit(Node <  T  &rt;    data,Node <  T  &rt;    stack,Node <  T  &rt;    top)
	{
		System.out.println("node visiting currently::"+data.toString());
		if(stack!=null)
		{
			Node <  T  &rt;    p=stack;
			System.out.println("Stack::");
			while(p!=null)
			{
				System.out.print("\tdata:"+p.getData());
				if(p.getLt()!=null)
					System.out.print("\t left:"+p.getLt().getData());
				else
					System.out.print("\t left:"+p.getLt());
				if(p.getRt()!=null)
					System.out.println("\t right:"+p.getRt().getData());
				else
					System.out.println("\t right:"+p.getRt());
				
				p=p.getLt();
			}
		}
		else
			System.out.println("stack::"+stack);
		
		
		if(top!=null)
			System.out.println("top::"+top.getData());
		else
			System.out.println("top::"+top);
	}
	public void linkInvrsionTraversal()
	{
		Node <  T  &rt;    cur=root;
		Node <  T  &rt;    		empty=root;
		Node <  T  &rt;    		prev=null;
		Node <  T  &rt;    		next=null;
		System.out.println("link inversal traversal::");
		do
		{
			while(cur!=null)
			{
				visit(cur);
				cur.setTag(0);
				next= cur.getLt();
//						cur-  &rt;   left=prev;
				cur.setLt(prev);
				prev=cur;
				cur=next;
			}
			next=cur;
			cur=prev;
			if(prev.getTag()==0)
				prev=prev.getLt();
			else
				prev=prev.getRt();
			cur.setLt(next);
			boolean up=true;
			do
			{
				if(cur.getRt()!=null && cur.getTag()==0)
				{
					cur.setTag(1);
					next=cur.getRt();
					cur.setRt(prev);
					prev=cur;
					cur=next;

					up=false;
				}
				if(cur.equals(empty))
				{
					cur.setTag(0);
					return;
				}
				if(up==true&&prev!=null)
				{
					Node <  T  &rt;    next_prev=null;
					
					
					if(prev.getTag()==0)
						next_prev=prev.getLt();
					else 
						next_prev=prev.getRt();
					
					if(prev!=null&&prev.getTag()==0)
					{
						cur.setTag(0);
						prev.setLt(cur);
						cur=prev;
						prev=next_prev;
					}
					else
					{
						
						cur.setTag(0);
						prev.setRt(cur);
						cur=prev;
						prev=next_prev;
					}

				}
				
			}while(cur!=null&&up);
			
		}while(cur!=null);
	}
	
	
	
	/**
	Write and test a "modified" Robson Traversal program that uses the linked representation of the trees. This modified version differs from the Robson only in that        the pointers to a node's predecessor should now be as in the Linked Inversion Traversal. That is, when a node's left(right) subtree is being traversed, its left(	  right) pointer will point to its predecessor. During the traversal, when a node is visited, output for each "stack' entry, its info value and the info value of i	   ts left and right successors. This means, first, output the number of the node Top points to, and then the number that that node's left and right pointers point 	    to. Then, output the number of the node that Stack points to,and then the numbers that that node's left and right pointers point to. Do this for each node on the	      "stack". Also, output similar information for each node along the path from the predecessor of the node being visited to the root. That is, for each of these no	      des output its info value and the numbers that the nodes left left and right pointers point to.
	*/
	public void modifiedRobsonTreeTraversal()
	{
		Node <  T  &rt;    cur=root;
		boolean rootHasOnlyRightChild=false;
		if(root!=null)
			rootHasOnlyRightChild=(root.getLt()==null)?(root.getRt()!=null?true:false):false;
		Node <  T  &rt;    		empty=root;
		Node <  T  &rt;    		prev=null;
		Node <  T  &rt;    		next=null;
		Node <  T  &rt;   			avail=null;
		Node <  T  &rt;            stack=null;
		Node <  T  &rt;            top=null;
		boolean done=false;
		System.out.println("\n===============================");
		System.out.println("Modified Robson traversal::");
		System.out.println("===============================");
		if(cur==null)
		{
			System.out.println("Sorry! it is a null tree");
			return;
		}
		do
		{
			while(cur!=null)
			{
				visit(cur,stack,top);
				next= cur.getLt();
				
				if(cur.getLt()==null&&cur.getRt()==null)
				{
					if(avail==null)
						avail=cur;
					else
					{
						cur.setRt(avail);
						avail=cur;
					}
				}
				cur.setLt(prev);
				prev=cur;
				cur=next;
			}
			next=cur;
			cur=prev;
			prev=prev.getLt();
			cur.setLt(next);
			boolean up=true;
			boolean leftTraversingFinished=true;
			do
			{
				if(cur.getRt()!=null&&cur!=avail&&leftTraversingFinished)
				{
					next=cur.getRt();
					cur.setRt(prev);
					if(cur.getLt()!=null)
					{

						if(top!=null)
						{
							Node <  T  &rt;    temp=avail;
							avail=avail.getRt();
							temp.setLt(null);
							temp.setRt(null);
							temp.setRt(top);
							if(stack==null)
								stack=temp;
							else
							{
								temp.setLt(stack);
								stack=temp;
							}
							
						}
						top=cur;
						
					}
					prev=cur;
					cur=next;
					up=false;
				}
				if(cur.equals(empty))
				{
					done=true;
				}
				if(up==true&&prev!=null)
				{
					Node <  T  &rt;    next_prev=null;
					boolean curIsPrevLeftChild=true;
					if(prev==empty)
					{
						next_prev=null;
						if(rootHasOnlyRightChild)
							curIsPrevLeftChild=false;
					}
					else if(prev.getLt()!=null&&top!=prev)
						next_prev=prev.getLt();
						
					else
					{
						next_prev=prev.getRt();
						curIsPrevLeftChild=false;
					}

					
					if(top==prev)
					{
						prev.setRt(cur);
						leftTraversingFinished=false;
						if(top!=null)
						{
							
							if(stack!=null)
							{
								top=stack.getRt();
								Node <  T  &rt;    currentStack=stack;
								stack=stack.getLt();
								currentStack.setLt(null);
								currentStack.setRt(null);
							}
							else 
								top=null;
						
						}
					
					}
						
					else if(curIsPrevLeftChild)
					{
						prev.setLt(cur);
						leftTraversingFinished=true;
					}
					else 
					{
						prev.setRt(cur);
						prev.setLt(null);
						leftTraversingFinished=false;
						
						
					}
					cur=prev;
					prev=next_prev;
				}
				
			}while(cur!=null&&up&&!done);
			
		}while(cur!=null&&!done);
		while(avail!=null)
		{
			Node <  T  &rt;    temp=avail;
			avail=avail.getRt();
			temp.setLt(null);
			temp.setRt(null);
		}
	}
}


Advertisements

Discussion for Integer Overflow in Java

Standard

Today, I was reading an article for overflow and cannot stop myself from sharing the gist of it.

Let us discuss this solution of binary search tree.

public int binarySearchTree(int a[],int item)
{
  int start=0;
  int end=a.length-1;
  while(start<=end)
  {
      int mid=(start+end)/2;
      if(a[mid]==item)
               return mid;
      else if(a[mid]<item)
               start=mid+1;
      else
                end=mid-1;
}
return -1;
}

 

if we carefully check these lines of code, we will realize that the line mid=(start+end)/2 may result in overflow, what if start+end > Integer.MAX_VALUE then mid will have a -ve value.  To overcome this sort of potential programming bug (this bug is still existing in Java’s Arrays Binary search and it has been earlier reported by Joshua Bloch) , it will be better to write this line of code as this,

1)  mid= start + (end-start)/2;
or
2) mid = (start+high) >>>1; //since in java “>>>” is unsigned right shift operator.
 
Now let us try to write a utility class to check whether different arithmetic operation is overflow safe or not.

For this let us start to discuss which all arithmetic operation can result in overflow, here I will like to point out that any logical operation like &, | and ^ cannot result in overflow thats why we are only discussing arithmetic operation.

1) Addition:  a+ b can result in overflow as we have seen in above discussion. So, we need to have a utility method for this.

2)  Subtraction: same like for addition it can also result in overflow.

3)  Multiplication: a*b, can easily result in overflow.

4) Division: consider this case Integer.MIN_VALUE/-1,  this will result in overflow.

5) Negation: – (Integer.MIN_VALUE) can result in overflow again.

 

Here is the Utility class has methods to check the safety for each of the above discussed arithemetic operations:-

package org.aakash.pratice;

public class OverflowCheckUtility {

public static boolean isSafeToSubtract(int i,int j)
{
    if(!isSafeToNegate(j))
          return (i&Integer.MIN_VALUE)<0;
    return isSafeToAdd(i, -j);
}
public static boolean isSafeToNegate(int i)
{
    return i!=Integer.MIN_VALUE;
}
public static boolean isSafeToAdd(int i,int j)
{
    int k=i+j;
    if(((i^k)&(j^k))<0)
         return false;
    return true;
}
public static boolean isSafeToDivide(int i,int j)
{
   if(i<=Integer.MAX_VALUE&j==-1)
      return false;
   return true;
}
public static boolean isSafeToMultiply(int i,int j)
{
   int k=i*j;
   boolean expectedSign=((i&Integer.MIN_VALUE)^(j&Integer.MIN_VALUE))>=0?true:false;
   boolean actualsign=(k&Integer.MIN_VALUE)>=0?true:false;
   return expectedSign==actualsign;
}
/**
   Testing utility class for different boundry values
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
     int count=0;
     int n=Integer.MIN_VALUE;
//        int n=-1;
    while(n!=0)
    {
        n=(n-1)&n;
        count++;
     }
     System.out.println(count);
     System.out.println(Integer.MAX_VALUE*2);
     System.out.println(Integer.MAX_VALUE*-2);
     System.out.println(Integer.MIN_VALUE*2);
     System.out.println(Integer.MIN_VALUE*-1);
     System.out.println("::checking for multiply");
     int i=-1;
     System.out.println(isSafeToMultiply(i, Integer.MIN_VALUE));
     System.out.println(isSafeToMultiply(i, Integer.MAX_VALUE));
     System.out.println(isSafeToMultiply(2, Integer.MAX_VALUE/3));
     System.out.println(isSafeToMultiply(2, Integer.MAX_VALUE/2));
     System.out.println(isSafeToMultiply(2, Integer.MAX_VALUE));
     System.out.println(isSafeToMultiply(-2, Integer.MIN_VALUE/2));


     System.out.println("checking subtraction");
     System.out.println(isSafeToSubtract(-1, Integer.MAX_VALUE));
     System.out.println(isSafeToSubtract(-2, Integer.MAX_VALUE));
     System.out.println(isSafeToSubtract(-5, Integer.MAX_VALUE));
     System.out.println(isSafeToSubtract(1, Integer.MIN_VALUE));
     System.out.println(isSafeToSubtract(-12, Integer.MIN_VALUE));
     System.out.println(isSafeToSubtract(-1, Integer.MIN_VALUE));

     System.out.println("checking addition");
     System.out.println(isSafeToAdd(-1, Integer.MIN_VALUE));
//        System.out.println(isSafeToSubtract(-2, Integer.MAX_VALUE));
//        System.out.println(isSafeToSubtract(-5, Integer.MAX_VALUE));
//        System.out.println(isSafeToSubtract(1, Integer.MAX_VALUE));

}

How to generate all possible binary tree in reverse lexicographic order

Standard

Suppose we are representing binary as L and R array, for example
The tree would be represented then
1            L[1]=2, R[1]=4
/  \           L[2]=0, R[2]=3
2   4        L[3]=0, R[3]=0
\             L[4]=0, R[4]=0
3

Now, if we read tree in preorder, in such a way that if node present it print 1 otherwise 0 then this tree will also be represented as 110100100. Now the problem we are going to solve here is: “ if we have to generate all possible binary tree for a give number of node in such a way that if we traverse the tree in preorder just described then it should be in reverse lexicographic order.

Now, first we will develop the algo then will code it.

Our first step will be to observe the pattern and see what will be the next generated tree for a given tree. Suppose we have this tree

                       1

                  /

             2

           /

        3

tree in 1,0 format : 1110000

then the next generated tree in the reverse lexicographic order will be this

                         1

                     /

                  2

                     \

                       3

tree in 1,0 format : 1101000

After looking at then newly generated tree, we can find that node 3 has moved and its moved from left of 2 to right of 3.  Let us generate one more tree from the last generated tree and the new tree will look like this:

                1

            /       \

          2            3

tree in 1,0 format : 1100100 (Note: observe this no for all the generated tree, you will find its decreasing)

and next  tree generated from this tree is:

               1

             /   \

        null      2

                   /      \

              3          null

tree in 1,0 format : 1011000 (Note: observe this no for all the generated tree, you will find its decreasing)

If we compare with the last generated one then we will find that this time node 2 has moved and now node 3 is left child of node 2.  If we generate  some more trees then we will find that this algo:

1. take the nth node and try to find the best position for it i.e. the next possible position for it in preorder.

2. if we are not able to find any position for nth node then try to find for n-1 th node and moved the node there

and then we make all node greater then this left child of its parent.

For example :-

1 we take the first generated tree as an example here:

1. let us take the nth node , 3 in this case, now try to find the next possible position for it in the tree after traversing it in preorder then we will find that it it is the right of 2nd node.

2. move the 3 rd node to right of 2nd node and since it has no child node, hence this is the answer.

2. Now try, the last generated tree:

1. let us take the nth node first here i.e. 3rd node in this case, now if we try to next possible position. We see there is no possible next position for so

2. try the node higher then it i.e. 2nd node. We find that next possible position for it is right of 1. Now shift 2nd to this new position and move 3 rd as its left child, if we have 4 th node then we would have made it left of 3rd.

The most important this, we have to do now, is to find the way to find the next possible position for the given node. After,  we do pre-order traversal of the tree then we will find that the 3 rd node traversed after traversing the selected node is the possible position for it. For example,

take the example of first generated tree:-

                            1

                       /       \

                      2           null 4

                  /    \

              3         null 3

             /    \

     null  1   null 2

now if we try to find , the possible position for 3 we see that after traversing node 3 in pre-order, we traverse null 1 first then null 2 and then null3 which is the possible position and which is exactly third node traversed after traversing node 3. Hence our logic is correct in this example.

Now, we consider the last generated tree, the parent tree is

                                      1

                                  /      \

                              2             3

                         /     \             /   \

                 null1  null2     null3  null4

Now, we try to find,  best  position for nth node, i.e. 3 rd node, now if we do pre-order traversal, then we find that after traversing node 3rd we can only 2 nodes which are its child and hence there is no possible shift for node 3 rd hence we will do the same for node 2 nd and we will find that 3 rd node traversed after traversing node 2 is the node where currently node 3 is, hence we shift 2 to this new position and make node 3 its left child and if we would has 4 th node then it would have been the left child of node 3rd and so on.

Before we start coding, one more point left to discuss is: from where to start, what actually I mean is what will be the first base tree?

For, generating the reverse lexicographic order  tree, the firs tree should be the left linear tree i.e.

                                                   1

                                            /

                                    2

                                 /

                            3

                        .

                      .

                  n

and the final tree will be right linear tree i.e.

1

   \

      2

        \

           3

               .

                  .

                   n


package edu.temple.pt.assignment;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* This program is written as part of the assignment 5 question 1. It provides APIs to generate the next binary tree for a given binary tree in
* reverse lexicographic order.
*
* Assignment:
* Assume the nodes of any n node binary tree are numbered from 1 to n in preorder. Arrays L and R will be used to represent the tree in memory as in the following example.
* Consider the tree with nodes 1, 2, 3 and 4 with 2 and 4 as 1’s left and right successors and 3 as 2’s right successor. Then L and R would be
* L[1]=2, R[1]=4
L[2]=0, R[2]=3
L[3]=0, R[3]=0
L[4]=0, R[4]=0

Write a program to generate all the binary trees with n nodes so that their representations, using n 0’s and n 1’s (i.e. n right and n left parentheses)
as discussed in class, would appear in reverse lexicographic order. The tree is represented in memory only by the L and R arrays. To do this, you should determine
what transformation is required on a tree that corresponds to the transformation on its parentheses representation to produce the successor tree in lexicographic
order, and then carry out this transformation on the tree in memory. Your program should output the L and R arrays for each tree and its corresponding n 0’s and n 1’s
representation. You should provide a number of samples for small n that are actual outputs of the program. In addition you should output the #of binary trees with n nodes.

<code>

* @author aakash
*
*/
public class ReversedLexicoGraphic {
/**
*
*/
public ReversedLexicoGraphic() {
super();
}
private int preOrderElementVisited[]=null;
private int elementVisitedPosition[]=null;

/**
* this method will do preorder traversal to the tree represented in LR array and will decide what is the next best position for
* the given element.
* @param binaryTreeInLRArray binary tree represented in LR array
* @param element element for which the best next position to be found
* @return -1 if it there is no suitable next position for it otherwise it will return positive number( if is is less than n(number of nodes in tree)
* then it means the index in left array otherwise return_value-n is the index for right array)
*/
private int findNextPositionForGivenElement(BinaryTreeInLRArray binaryTreeInLRArray,int element)
{
preOrderElementVisited=new int[binaryTreeInLRArray.getLeftArray().length*2+1];
elementVisitedPosition=new int[binaryTreeInLRArray.getLeftArray().length-1];
for(int i=0;i<elementVisitedPosition.length;i++)
elementVisitedPosition[i]=-1;
for(int i=0;i<preOrderElementVisited.length;i++)
preOrderElementVisited[i]=-1;
//int freeEntry=0;
int p=1;
Stack stack=new Stack();
int i=0;
int lastFreeEntry=-1;
int j=0;
boolean foundLastNode=false;
int countOfLeaveNodeAfterLastNode=0;
while(p!=0||!stack.isEmpty())
{

if(p!=0)
{
stack.push(p);

preOrderElementVisited[i++]=p-1;
if(binaryTreeInLRArray.getLeftArray()[p-1]==0&&foundLastNode)
{
//System.out.println(“calculating prev=”+lastFreeEntry+” when p=”+(p-1));
countOfLeaveNodeAfterLastNode++;
if(countOfLeaveNodeAfterLastNode>=3)
{
lastFreeEntry=p-1;
break;
}
}
if(binaryTreeInLRArray.getLeftArray()[p-1]!=0)
{
if(j=3)
{
lastFreeEntry=p-1+binaryTreeInLRArray.getLeftArray().length;
break;
}

}
else if(binaryTreeInLRArray.getRightArray()[p-1]!=0&&j<elementVisitedPosition.length-1)
elementVisitedPosition[j++]=p-1+binaryTreeInLRArray.getLeftArray().length;
p=binaryTreeInLRArray.getRightArray()[p-1];

}

}
else
{
int q=0;
int r=0;
do
{
q=stack.pop();
if(!stack.isEmpty())
{
preOrderElementVisited[i]=stack.peek()-1+binaryTreeInLRArray.getLeftArray().length;
r=binaryTreeInLRArray.getRightArray()[stack.peek()-1];
if(r==0&&foundLastNode)
{
//System.out.println(“calculating prev=”+lastFreeEntry+” when p=”+(p-1));
countOfLeaveNodeAfterLastNode++;
if(countOfLeaveNodeAfterLastNode==3)
{
lastFreeEntry=stack.peek()-1+binaryTreeInLRArray.getRightArray().length;
stack.removeAllElements();
return lastFreeEntry;
}

}
else if(r!=0&&j=2;element–)
{
BinaryTreeInLRArray tempBinaryTreeInLRArray=new BinaryTreeInLRArray(baseBinaryTreeInLRArray.getLeftArray().length);
for(int i=2;i<=element;i++)
{
int tempPosition=elementVisitedPosition[i-2];
modifyArrayAtPosition(tempBinaryTreeInLRArray, tempPosition, i);
}
position=findNextPositionForGivenElement(tempBinaryTreeInLRArray, element);
tempBinaryTreeInLRArray=null;
if(position!=-1)
break;
}

if(element==1||element==-1)
{
binaryTreeInLRArray=null;
return binaryTreeInLRArray;
}
// findNextPositionForGivenElement(baseBinaryTreeInLRArray, baseBinaryTreeInLRArray.getLeftArray().length);
// System.out.println(“position:”+position+” for element:”+element);
for(int i=2;i<element;i++)
{
int tempPosition=elementVisitedPosition[i-2];
modifyArrayAtPosition(binaryTreeInLRArray, tempPosition, i);
}

modifyArrayAtPosition(binaryTreeInLRArray, position, element);
for(int i=element+1;i<=binaryTreeInLRArray.getLeftArray().length;i++)
{
int tempPosition=i-2;
modifyArrayAtPosition(binaryTreeInLRArray, tempPosition, i);
}

}
else
{
for(int i=2;i=binaryTreeInLRArray.getLeftArray().length)
{
position-=binaryTreeInLRArray.getLeftArray().length;
binaryTreeInLRArray.getRightArray()[position]=element;
}
else
binaryTreeInLRArray.getLeftArray()[position]=element;
}
/**
* This method can be used to get the next binary tree in reverse lexicographic order.
*
* @param base the base tree from which the next binary tree will be generated
* @return null if there is no possible binary tree otherwise the new generated binary tree.
*/
public BinaryTreeInLRArray getNextBinaryTree(BinaryTreeInLRArray base)
{
if(base.getLeftArray().length<=1)
return null;
// BinaryTreeInLRArray nextLRBinaryTree=new BinaryTreeInLRArray(base.getLeftArray().length);

int prev=findNextPositionForGivenElement(base,base.getLeftArray().length);
// for(int i=0;i<preOrderElementVisited.length;i++)
// System.out.println(“\npreorder visit::”+preOrderElementVisited[i]);
// for(int i=0;i<elementVisitedPosition.length;i++)
// System.out.println(“element visited position::”+elementVisitedPosition[i]);
// System.out.println(“prev=”+prev);
// if(prev==-1)
// return null;

BinaryTreeInLRArray nextLRBinaryTree=modifiedPosition(base, prev);
return nextLRBinaryTree;
}
public List getAllReveresedLexicoGraphicBinaryTree(int n)
{
ArrayList binaryTreeInLRArrayList=new ArrayList();
if(n0″);
int L[]=new int[n];
int R[]=new int[n];
int j=2;
for(int i=0;i<n;i++)
{
R[i]=0;
if(j<=n)
L[i]=j++;
else
L[i]=0;
}
BinaryTreeInLRArray baseBinaryTreeInLRArray=new BinaryTreeInLRArray(L, R);
binaryTreeInLRArrayList.add(baseBinaryTreeInLRArray);
while((baseBinaryTreeInLRArray=getNextBinaryTree(baseBinaryTreeInLRArray))!=null)
binaryTreeInLRArrayList.add(baseBinaryTreeInLRArray);
return binaryTreeInLRArrayList;
}
public static void main(String args[])
{
// int R[]={5,0,4,0,0};
// int L[]={2,3,0,0,0};

int R[]={0,0,0,0};
int L[]={2,3,4,0};
BinaryTreeInLRArray binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.preOrderTraversal();

//binaryTree=new ReversedLexicoGraphic().getNextBinaryTree(binaryTree);
int i=1;
while((binaryTree=new ReversedLexicoGraphic().getNextBinaryTree(binaryTree))!=null)
{
binaryTree.preOrderTraversal();
i++;
System.out.println(“total generated count::”+i);
}
System.out.println(“total generated count::”+i);

/*
binaryTree=new ReversedLexicoGraphic().getNextBinaryTree(binaryTree);
//int i=1;
//while((binaryTree=new ReversedLexicoGraphic().getNextBinaryTree(binaryTree))!=null)
{
binaryTree.preOrderTraversal();
i++;
System.out.println(“total generated count::”+i);
}
System.out.println(“total generated count::”+i);

*/

}
}


package edu.temple.pt.assignment;

import java.util.Stack;

public class BinaryTreeInLRArray {

int L[]=null;
int R[]=null;

public void setLeftArray(int L[])
{
this.L=L;
return;
}
public void setRightArray(int R[])
{
this.R=R;
return;
}
public int[] getLeftArray()
{
return L;
}
public int[] getRightArray()
{
return R;
}

int visitedArray[]={-1};
public BinaryTreeInLRArray(int numberOfNodes) {
// TODO Auto-generated constructor stub
L=new int[numberOfNodes];
R=new int[numberOfNodes];
}
public BinaryTreeInLRArray(int L[],int R[]) {
// TODO Auto-generated constructor stub
if(L.length!=R.length)
throw new IllegalArgumentException(“Sorry! L and R array should be of equal length”);
this.L=L;
this.R=R;
}
private void visit(int i)
{
if(visitedArray[0]!=-1)
System.out.print(visitedArray[0]!=0?1:0);
visitedArray[0]=i;
}
private void cleanUpVisit(){
visitedArray[0]=-1;
}
private void printLRArray()
{
System.out.println(“\t[L]\t[R]”);
System.out.println(“\t—\t—“);
for(int i=0;i<L.length;i++)
System.out.println(“\t|”+L[i]+”|\t|”+R[i]+”|”);
}
public void preOrderTraversal()
{
printLRArray();
int p=1;
Stack stack=new Stack();
while(p!=0||!stack.isEmpty())
{

if(p!=0)
{
visit(p);
stack.push(p);
if(L[p-1]!=0)
p=L[p-1];
else
{
visit(L[p-1]);
p=R[p-1];
if(p==0)
visit(p);
}
}
else
{
int q=0;
int r=0;
do
{
q=stack.pop();
if(!stack.isEmpty())
{

r=R[stack.peek()-1];
if(r==0)
visit(r);
}
else
r=0;

}while(!stack.isEmpty()&& q==r);
p=r;

}

}
cleanUpVisit();
}

public void reversePreOrderTraversal()
{
printLRArray();
int p=1;
Stack stack=new Stack();
while(p!=0||!stack.isEmpty())
{

if(p!=0)
{
visit(p);
stack.push(p);
if(R[p-1]!=0)
p=R[p-1];
else
{
visit(R[p-1]);
p=L[p-1];
if(p==0)
visit(p);
}
}
else
{
int q=0;
int r=0;
do
{
q=stack.pop();
if(!stack.isEmpty())
{

r=L[stack.peek()-1];
if(r==0)
visit(r);
}
else
r=0;

}while(!stack.isEmpty()&& q==r);
p=r;

}

}
cleanUpVisit();
}
private Integer getIntegerFromBinaryString(String str,int i)
{
if(str.charAt(i)==’0’||str.charAt(i)==’)’)
return new Integer(0);
return new Integer(1);
}
/**
* Extra Credit
Write and test a program “create”, that reads the sequence of n 0’s and n 1’s (i.e. the n right and n left parentheses) representing an n node
binary tree as we discussed in class and creates a linked representation of the tree in memory using L and R. This program should echo the
input. After the tree is created, preorder traverse the tree, and as each node is visited, output its number and that of its left and right
successors.
Provide the actual output for a number of sample trees.
* @param binaryString
*/
public void createTree(String binaryString)
{
L=new int[binaryString.length()/2];
R=new int[binaryString.length()/2];
for(int i=0;i<L.length;i++)
{
L[i]=R[i]=-1;
}
int p=1;
Stack stack=new Stack();
int i=0;
if(getIntegerFromBinaryString(binaryString, i).equals(new Integer(0)))
{
p=0;
for(int j=0;j<L.length;j++)
{
L[j]=R[j]=0;
}
}
int elementCount=2;
while(p!=0||!stack.isEmpty())
{

if(p!=0)
{
//visit(p);
i++;
stack.push(p);
if(getIntegerFromBinaryString(binaryString, i).equals(new Integer(1)))
L[p-1]=elementCount++;
else
L[p-1]=0;

if(L[p-1]!=0)
p=L[p-1];
else
{
i++;
if(getIntegerFromBinaryString(binaryString, i).equals(new Integer(1)))
R[p-1]=elementCount++;
else
R[p-1]=0;
// visit(L[p-1]);
p=R[p-1];
// if(p==0)
// visit(p);
}
}
else
{
int q=0;
int r=0;
do
{
q=stack.pop();
if(!stack.isEmpty())
{
r=R[stack.peek()-1];
if(r==-1)
{
i++;
if(getIntegerFromBinaryString(binaryString, i).equals(new Integer(1)))
R[stack.peek()-1]=elementCount++;
else
R[stack.peek()-1]=0;
r=R[stack.peek()-1];
}

}
else
r=0;
}while(!stack.isEmpty()&& q==r);
p=r;

}

}
}

public static void main(String args[])
{
int L[]={2,0,0,0};
int R[]={4,3,0,0};

BinaryTreeInLRArray binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“110100100”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“111000100”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“101100100”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“101010100”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“101010100”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“111100000”);
System.out.println();
binaryTree.preOrderTraversal();

binaryTree=new BinaryTreeInLRArray(L,R);
binaryTree.createTree(“011100000”);
System.out.println();
binaryTree.preOrderTraversal();
}
}