tosha9
BAN USERI basically compared data given in tree with depth from root and found the difference between them.
Calling code
int data1 = 2; //example
int data2= 5; //example
int depth = TreeNode.distanceBetweenNodes(data1, data2, root);
if(depth > 0 )
{
System.out.println("Distance between data "+data1 +" and "+data2+" is "+depth);
}
else
{
System.out.println("Data1 = "+ data1 + " or Data2 "+data2+" not in tree");
}
My tree structure looks like following
public class TreeNode
{
private int data;
private TreeNode leftchild;
private TreeNode rightchild;
public TreeNode(int data)
{
this.data = data;
leftchild = null;
rightchild = null;
}
public static TreeNode insert(int value, TreeNode node)
{
if(node==null)
{
//System.out.println("Assign value "+value);
node = new TreeNode(value);
// return node;
}
else
{
//System.out.println("Comparing data value with "+node.data +" and "+value);
if(node.data >= value)
{
// System.out.println("moving left");
node.setLeftchild(insert(value,node.getLeftchild()));
}
else
{
// System.out.println("moving right");
node.setRightchild(insert(value,node.getRightchild()));
}
}
return node;
}
public static int distanceBetweenNodes(int data1, int data2, TreeNode root)
{
int distance1 = distanceFromRoot(data1, root);
int distance2 = distanceFromRoot(data2, root);
System.out.println("Distance from root is "+ distance1 );
System.out.println("Distance from root is "+ distance2);
if((distance1 < 0) || (distance2 < 0))
{
System.err.println("The distance is "+distance1);
return -1;
}
else
{
return Math.abs(distanceFromRoot(data1, root) - distanceFromRoot(data2, root));
}
}
public static int distanceFromRoot(int value ,TreeNode node)
{
if(node == null)
{
return -100;
}
else if (node.getData() == value)
{
return 1;
}
return Math.max((1+distanceFromRoot(value, node.getLeftchild())), (1+distanceFromRoot(value, node.getRightchild())));
}
The node structure follows:
public class TreeNode
{
private int data;
private TreeNode leftchild;
private TreeNode rightchild;
public TreeNode(int data)
{
this.data = data;
leftchild = null;
rightchild = null;
} //end of constructor
public static int distanceBetweenNodes(int data1, int data2, TreeNode root)
{
int distance1 = distanceFromRoot(data1, root);
int distance2 = distanceFromRoot(data2, root);
System.out.println("Distance from root is "+ distance1 );
System.out.println("Distance from root is "+ distance2);
if( (distance1 != 0) && (distance2 != 0))
{
return -1;
}
else
{
return Math.abs(distanceFromRoot(data1, root) - distanceFromRoot(data2, root));
}
}//end of function
public static int distanceFromRoot(int value ,TreeNode node)
{
if(node == null)
{
return 0;
}
else if (node.getData() == value)
{
return 1;
}
return Math.max((1+distanceFromRoot(value, node.getLeftchild())), (1+distanceFromRoot(value, node.getRightchild())));
} //end of function
}//end of class
The top two options that are used are sessions and cookies. To access a session, you will need to have session_start() at the top of each page, and then you will use the $_SESSION hash to access and store your session variables. For cookies, you only have to remember one rule. You must use the set_cookie function before any output is started in your PHP script. From then on you can use the $_COOKIE has to access your cookie variables and values.
There are other methods, but they are not as fool proof and most often than not depend on the IP address of the visitor, which is a very dangerous thing to do.
I am assuming m depth of tree can not be more than 99.
- tosha9 June 05, 2014