/*
 * Node.java
 *
 * Created on January 1, 2002, 1:25 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package medusa.georgios.ClusteringAlgorithms;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author Georgios
 */
public class Node {
    public int id;
    public String Label;
    public boolean is_leaf;
    public Node left_child;
    public Node right_child;
    public Node parent;
    public Node higher_parent;
    public ArrayList labels;
    public int id_counter=0;
    Hashtable Index_nodes_map = new Hashtable();
    StringBuffer NHX = new StringBuffer("");
    
    public Node(ArrayList Labels) {  this.labels=Labels;}
    
  
public Node create_node()
{
  Node leaf_node=null;
  leaf_node=new Node(this.labels);
  leaf_node.id=id_counter;
  leaf_node.is_leaf=true;
  leaf_node.left_child=null;
  leaf_node.right_child=null;
  leaf_node.parent=null;

  Index_nodes_map.put(leaf_node.id,leaf_node);
  id_counter++;
  return leaf_node;
}  
    
private void calculate_tree_NewickFormat(Node cluster) {

	if(cluster!=null)
	{
               if(cluster.is_leaf==false){
                    NHX.append("(");
                    calculate_tree_NewickFormat(cluster.left_child);}

               if(cluster.is_leaf==false)
                           NHX.append(",");
                else
                        {
                   if(cluster.id<=labels.size())
                                NHX.append(labels.get(cluster.id));
                        }

               if(cluster.is_leaf==false){
                    calculate_tree_NewickFormat(cluster.right_child);
                NHX.append(")");
                }
	 }
       
  }

public String get_tree_NewickFormat(Node cluster)
{
    this.calculate_tree_NewickFormat(cluster);
    NHX.append(";");
    return NHX.toString();
}

public String get_tree_NewickFormat()
{  NHX.delete(0,NHX.length());
    search_higher_ancestor_of(Index_nodes_map.get(0));
    this.calculate_tree_NewickFormat(higher_parent);
    NHX.append(";");
    return NHX.toString();
}


public void  search_higher_ancestor_of(Node cluster) 
{
 	if(cluster!=null)
	{
		if(cluster.parent!=null){
            higher_parent=cluster.parent;
		search_higher_ancestor_of(cluster.parent);}
	   else
		   higher_parent = cluster;
	}
}


public boolean connection_exists(Node parent, Node child)
{
    boolean connection_exists=false;
    int id_n1,id_n2;
	if(parent==null || child ==null) return false; 
	if(parent.id==child.id) return true;

	this.search_higher_ancestor_of(parent);
	id_n1=higher_parent.id;
	this.search_higher_ancestor_of(child);
	id_n2=higher_parent.id;
	if(id_n1==id_n2)
       connection_exists=true;
return connection_exists;

}


void connect(Node parent, Node child)
{
	if(parent.left_child!=null && parent.right_child!=null) 
        {System.out.println("Has already 2 children");}

	if(parent.left_child!=null)
		parent.right_child=child;  

	else 
	  parent.left_child=child; 
	

        child.parent=parent;

	if(parent.left_child!=null || parent.right_child!=null )parent.is_leaf=false;
	//System.out.println("\t========== "+parent.id +"-"+child.id);
    
}


    
    
    
}

 

Ads help maintain this website.