Skip to content

Instantly share code, notes, and snippets.

@psaitu
Created February 7, 2014 10:36
Show Gist options
  • Save psaitu/8860426 to your computer and use it in GitHub Desktop.
Save psaitu/8860426 to your computer and use it in GitHub Desktop.
6lowpan hilo
//import java.util.HashMap;
public class Graph {
Node root;
public Graph()
{
this.root = null;
}
public Graph(int num)
{
Node pNode = new Node(num);
this.root = pNode;
}
public void add_children(int[] num)
{
int i;
for(i = 0;i < num.length ; i++)
{
// System.out.println(this.root.child);
if(this.root.child == null)
{
Node new_node = new Node(num[i]);
System.out.println(new_node);
System.out.println(this.root.child[i]);
this.root.child[i] = new_node;
this.root.child[i].parent = this.root ;
}
}
}
public void add_ctc(int source, int[] num )
{
int i;
if( sget_Node(source)!= null )
{
Node Source = sget_Node(source);
if(Source == null)
{
System.out.println("Node not present");
}
for( i=0 ;i<num.length ; i++)
{
Source.child[i] = new Node(num[i]);
Source.child[i].parent = Source ;
}
}
}
public void set_neighbors(int coordinator , int[] num)
{
int i;
if(sget_Node(coordinator)!= null)
{
Node Coordinator = sget_Node(coordinator);
if(Coordinator == null)
{
System.out.println("node not present");
}
for(i=0 ; i<num.length ; i++)
{
Coordinator.child[i] = sget_Node(num[i]);
}
}
}
public Node labelTonode(int label)
{
Node p = sget_Node(label);
return p;
}
public int[] lsibling_set(int label)
{
int node[] = new int[15];
Node p = sget_Node(label);
Node q = return_Parent_Node(p);
int i = 0;
while (q.child[i].label != label )
{
node[i] = q.child[i].label;
i++;
}
return node;
}
public Node[] sibling_set(int label)
{
Node node[] = new Node[15];
Node p = sget_Node(label);
Node q = return_Parent_Node(p);
int i = 0;
while (q.child[i].label != label )
{
node[i] = q.child[i];
i++;
}
return node;
}
public int[] sub_intersection(int[] s1, int[] s2 )
{
int[] a= new int[15];
int i=0,j=0;
int k=0;
for( i=0;i<a.length;i++)
{
a[i]=0;
}
while(s1.length != 0)
{
while(s2.length!=0)
{
if(s1[i]==s2[j])
{
a[k] =s1[i];
k++;
}
j++;
}
i++;
}
return a;
}
public void intersection(int[] s1, int[] s2 )
{
int[] b;
b = sub_intersection(s1,s2);
if( b == null)
{
System.out.println("no common nodes");
//return 0;
}
else
{
System.out.println("nodes are:");
for(int i=0;i<b.length;i++)
{
while(b[i]!=0)
{
System.out.println(b[i]+",");
}
}
}
}
public Node return_Parent_Node(Node n)
{
return n.parent;
}
public int return_Parent( Node n)
{
return n.parent.label;
}
public Node sget_Node(int num)
{
// searches and returns node .
Node pNode = this.root;
while(pNode!=null)
{
if(pNode.label == num)
{
//System.out.println("("+num+") DATA FOUND\n" );
return pNode ;
}
else
{
while (pNode!=null)
{
for(int i = 0 ; i < pNode.n ; i++)
{
if(pNode.child[i].label == num)
{
//System.out.println("("+num+") DATA FOUND\n");
return pNode.child[i];
}
else
{
pNode = pNode.child[i];
}
}
}
}
//System.out.println("Node not found");
//return null;
}
return null;
}
public boolean bsget_Node(int num)
{
// searches and returns node .
Node pNode = this.root;
while(pNode!=null)
{
if(pNode.label == num)
{
//System.out.println("("+num+") DATA FOUND\n" );
return true ;
}
else
{
while (pNode!=null)
{
for(int i = 0 ; i < pNode.n ; i++)
{
if(pNode.child[i].label == num)
{
//System.out.println("("+num+") DATA FOUND\n");
return true;
}
else
{
pNode = pNode.child[i];
}
}
}
}
//System.out.println("Node not found");
//return null;
}
return false;
}
public void node_Search (int num)
{
Node pNode = this.root;
while(pNode!=null)
{
if(pNode.label == num)
{
System.out.println("("+num+") DATA FOUND\n" );
return;
}
else
{
while (pNode!=null)
{
for(int i = 0 ; i < pNode.n ; i++)
{
if(pNode.child[i].label == num)
{
System.out.println("("+num+") DATA FOUND\n");
}
else
{
pNode = pNode.child[i];
}
}
}
}
System.out.println("("+num+") ELMENT NOT FOUND!! \n");
}
}
public int depth_Node(Node p)
{
Node[] q = new Node[15];
int i=0;
int count = 0;
while(!(p.equals(this.root)))
{
q[i] = p.parent;
p = p.parent;
i++;
count++;
}
return count;
}
public Node[] set_of_ascendant_nodes (Node p)
{
Node[] q = new Node[15];
int i=0;
while(!p.equals(this.root))
{
q[i] = p.parent;
p = p.parent;
i++;
}
return q;
}
public Node[] set_of_descendant_nodes(Node p,Node d)
{
//descendant node with respect to destination.
Node[] q = new Node[15];
Node[] s = new Node[15];
int i=0;
while(!(d.child[i].equals(null)))
{
Node[] r = new Node[15];
for(i=0;i<d.child.length;i++)
{
r[i]=d.child[i];
for(i=0;i<r.length;i++)
{
int a = depth_Node(r[i]); // depth of child node
int b = depth_Node(p); // depth of source node
boolean c = (a==(b-1))| (a==b) | (a==(b+1)) ;
if( r[i] == d )
{
q[i] = r[i];
}
else if((r[i] == p.neighbors[i]) && c)
{
//it is directly connected to the source pos
d.child = r[i].child;
q[i] = r[i];
}
else
{
// node is one of its parent's
if((r[i] == p.parent)||(r[i] == p.parent.neighbors[i]))
{
d.child = r[i].child;
q[i] = r[i];
}
else
{
//node is one of its ascendants.
Node t = r[i];
s = set_of_ascendant_nodes(p);
for(i=0;i<s.length;i++)
{
if ((t.equals(s[i])) || (t.equals(s[i].neighbors[i])))
{
d.child[i] = t.child[i];
}
}
}
}
}
}
}
return q;
}
public int hMd( Node md , Node d )
{
int dmd = depth_Node(md);
int ddn = depth_Node(d);
int hMd = dmd - ddn + 1 ;
return hMd;
}
public int hPc( Node p )
{
int ddc = depth_Node(p.parent);
int ddn = depth_Node(p);
int hpc = ddc + ddn;
return hpc;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Graph hl = new Graph(1);
System.out.println(hl);
//
hl.add_children(new int[]{2,3,4,5});
hl.add_ctc(2,new int[]{6,7,8});
hl.add_ctc(3, new int[]{10,11,12});
hl.add_ctc(4,new int[]{14,15});
hl.add_ctc(5,new int[]{18,19,20});
hl.add_ctc(6, new int[]{22});
hl.add_ctc(22 ,new int[]{86});
hl.add_ctc(11, new int[]{41});
hl.add_ctc(12,new int[]{46});
hl.add_ctc(18,new int[]{70,71});
hl.add_ctc(20,new int[]{77,78});
//
// hl.set_neighbors(70 , new int[]{22,6,18,71});
// hl.set_neighbors(18, new int[]{22,70,71,19,5,6,22});
// hl.set_neighbors(22 , new int[]{86,7,6,18,70});
// hl.set_neighbors(5 , new int[]{18,19,70,71,6,22});
// hl.set_neighbors(20 , new int[]{15,79,78,19,5});
// hl.set_neighbors(4 , new int[]{14,12,1,15,5,2});
// hl.set_neighbors(3 , new int[]{46,12,1,2,10,11});
// hl.set_neighbors(2 , new int[]{1,3,10,8,7,6});
// hl.set_neighbors(18 , new int[]{22,70,71,19,5,6,22});
}
}
public class Node {
Node[] child;
Node neighbors[];
Node parent;
int n; //maximum number of children.
int label;
Position p;
//Ipv6 addr;
String data;
public Node(int num)
{
this.label = num;
this.n = 16;
this.child = new Node[this.n];
System.out.println(this.label);
for(int i=0 ; i< this.n ;i++ )
{
System.out.println(this.child[i]);
this.child[i] = null;
this.p = new Position();
}
//ipassign
}
public Node(String msg)
{
this.data = msg;
}
public void Node_setPosition( int x, int y)
{
this.p = new Position(x,y);
}
public void Node_getPosition( Node n)
{
int x = n.p.x;
int y = n.p.y;
System.out.println("( "+x+","+y+")");
}
// public void return_Node(int num)
// {
//
// }
public void Node_deleteNode(Node n)
{
for(int i = 0; i< n.n ; i++)
{
while (n.child[i] != null)
{
this.child[i] = null;
}
}
n = null;
}
}
1
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Graph@1aa8c488
Exception in thread "main" java.lang.NullPointerException
at Graph.sget_Node(Graph.java:178)
at Graph.add_ctc(Graph.java:36)
at Main.main(Main.java:13)
public class Position {
int x;
int y;
public Position()
{
this.x = 0;
this.y = 0;
}
public Position(int x1 , int y1)
{
this.x = x1;
this.y = y1;
}
public double get_distance( Position p1 ,Position p2)
{
double distance;
double abs1=Math.abs(this.x-p1.x);
double sqr1=(Math.pow(abs1, 2));
double abs2=Math.abs(this.y-p2.y);
double sqr2=(Math.pow(abs2, 2));
double answer= sqr1+sqr2;
distance=Math.sqrt(answer);
return distance;
}
public void myprint(Position p)
{
System.out.println("("+p.x+","+p.y+")");
}
public Position midpoint(Position p)
{
Position q=new Position((this.x+p.x)/2,(this.y+p.y)/2);
return q;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment