2010-03-09 3 views
0

J'ai toutes mes données dans ma base de données. Il a 4 colonnes suivantesProblème de structure d'arbre Java ou php

id source_clust target_clust result_clust 

1 7  72  649 
2 9  572  650 
3 649  454  651 
4 32  650  435 

Ces données sont comme une structure arborescente. source_clust et target_clust génèrent target_clust. target_clust peut être source_clust ou target_clust pour créer un nouveau target_clust. Y at-il une fonction php ou d'une classe que je peux utiliser pour générer une structure arborescente pour mes données ????

Je vois ce site MySql ils font exactement ce dont j'ai besoin, mais je ne pouvais pas trouver comment mettre en œuvre cette requête dans mes données.

Merci!

Edité


Est-il possible en Java pour le faire? si nous avons les mêmes données dans le tableau ??

+0

Voulez-vous ajouter plus information? Je ne suis pas. Vous parlez des clusters source et cible, mais où result_cluster entre-t-il en jeu? –

+0

Merci pour votre contribution. C'est la sortie du cluster Agnes :). Il combine donc la source et la cible sur la base de la distance pour générer le cluster de résultats. Si ce n'est toujours pas clair, svp faites le moi savoir – user238384

+0

ou vous pouvez dire que la source et la cible sont des enfants de résultat mais le résultat sera également enfant d'une autre colonne de résultat (MLM :)) – user238384

Répondre

2

vous pouvez télécharger l'arborescence PHP (ou même binary tree dans votre cas) des classes de phpclasses.org, alors vous pouvez utiliser un script qui remplit l'arbre avec les données de votre base de données. Vous aurez plus de parties indépendantes de l'arbre, car vous avez plus de racines. Dans le cas que vous avez écrit, vous avez 2 racines: 651 et 435.

[...] 
$tree = new Tree(); 
$strsql = "SELECT source_clust, target_clust, result_clust FROM mytable"; 
$ressql = @mysql_query($strsql); 
while(list($v1, $v2, $v3)[email protected]_fetch_array($ressql)) 
{ 
    if (!$tree->hasNode($v1)) 
     $tree->newNode($v1);   
    if (!$tree->hasNode($v2)) 
     $tree->newNode($v2);   
    if (!$tree->hasNode($v3)) 
     $tree->newNode($v3); 

    $tree->Node($v1)->parent = $tree->Node($v3); 
    $tree->Node($v2)->parent = $tree->Node($v3); 
} 
@mysql_free_result($ressql); 
0

Ceci est le code complet je l'ai utilisé pour construire une structure de données d'arbre binaire et ses opérations correspondantes:

<?php 
class Node 
{ 
public $data; 
public $leftChild; 
public $rightChild; 

public function __construct($data) 
    { 
    $this->data=$data; 
    $this->leftChild=null; 
    $this->rightChild=null; 
    } 
public function disp_data() 
    { 
    echo $this->data; 
    } 


}//end class Node 
class BinaryTree 
{ 
public $root; 
//public $s; 
public function __construct() 
    { 
    $this->root=null; 
    //$this->s=file_get_contents('store'); 

    } 
//function to display the tree 
    public function display() 
    { 
    $this->display_tree($this->root); 

    } 
    public function display_tree($local_root) 
    { 

    if($local_root==null) 
    return; 
    $this->display_tree($local_root->leftChild); 
    echo $local_root->data."<br/>"; 
    $this->display_tree($local_root->rightChild); 

    } 
// function to insert a new node 
    public function insert($key) 
    { 
    $newnode=new Node($key); 
     if($this->root==null) 
     { 
     $this->root=$newnode; 
     return; 
     } 
     else 
     { 
     $parent=$this->root; 
     $current=$this->root; 
      while(true) 
      { 
       $parent=$current; 
       //$this->find_order($key,$current->data); 
       if($key==($this->find_order($key,$current->data))) 
        { 
         $current=$current->leftChild; 
         if($current==null) 
         { 
          $parent->leftChild=$newnode; 
          return; 
         }//end if2 
        }//end if1 
       else 
        { 
         $current=$current->rightChild; 
         if($current==null) 
         { 
          $parent->rightChild=$newnode; 
          return; 
         } //end if1      
        } //end else 
      }//end while loop 
     }//end else 

    } //end insert function 

//function to search a particular Node 
public function find($key) 
    { 
    $current=$this->root; 
    while($current->data!=$key) 
      { 
      if($key==$this->find_order($key,$current->data)) 
       { 
       $current=$current->leftChild; 
       } 
      else 
       { 
       $current=$current->rightChild; 
       } 
      if($current==null) 
       return(null); 

      } 
     return($current->data); 
    }// end the function to search 
public function delete1($key) 
    { 
    $current=$this->root; 
    $parent=$this->root; 

    $isLeftChild=true; 
    while($current->data!=$key) 
      { 
      $parent=$current; 
      if($key==($this->find_order($key,$current->data))) 
      { 
       $current=$current->leftChild; 
       $isLeftChild=true; 
      } 
      else 
      { 
       $current=$current->rightChild; 
       $isLeftChild=false; 
      } 
      if($current==null) 
       return(null); 
      }//end while loop 

     echo "<br/><br/>Node to delete:".$current->data; 
    //to delete a leaf node 
    if($current->leftChild==null&&$current->rightChild==null) 
     { 
      if($current==$this->root) 
       $this->root=null; 
      else if($isLeftChild==true) 
      { 
      $parent->leftChild=null; 
      } 
     else 
      { 
      $parent->rightChild=null; 
      } 
     return($current);  
     }//end if1 
    //to delete a node having a leftChild 
    else if($current->rightChild==null) 
     { 
      if($current==$this->root) 
      $this->root=$current->leftChild; 
      else if($isLeftChild==true) 
      { 
      $parent->leftChild=$current->leftChild; 
      } 
      else 
      { 
      $parent->rightChild=$current->leftChild; 
      } 
      return($current); 
     }//end else if1 
    //to delete a node having a rightChild 
    else if($current->leftChild==null) 
     { 
     if($current==$this->root) 
      $this->root=$current->rightChild; 
     else if($isLeftChild==true) 
      { 
      $parent->leftChild=$current->rightChild; 
      } 
     else 
      { 
      $parent->rightChild=$current->rightChild; 
      } 
      return($current); 
     } 
    //to delete a node having both childs 
    else 
     { 
     $successor=$this->get_successor($current); 
     if($current==$this->root) 
      { 
      $this->root=$successor; 

      } 
     else if($isLeftChild==true) 
      { 
      $parent->leftChild=$successor; 
      } 
     else 
      { 
      $parent->rightChild=$successor; 
      }  
     $successor->leftChild=$current->leftChild; 
     return($current); 
     } 


    }//end the function to delete a node 
//Function to find the successor node 
public function get_successor($delNode) 
    { 
    $succParent=$delNode; 
    $successor=$delNode; 
    $temp=$delNode->rightChild; 
    while($temp!=null) 
     { 
      $succParent=$successor; 
      $successor=$temp; 
      $temp=$temp->leftChild; 
     } 
    if($successor!=$delNode->rightChild) 
    { 
     $succParent->leftChild=$successor->rightChild; 
     $successor->rightChild=$delNode->rightChild; 
    } 
    return($successor); 
    } 
//function to find the order of two strings 
public function find_order($str1,$str2) 
    { 
    $str1=strtolower($str1); 
    $str2=strtolower($str2); 
    $i=0; 
    $j=0; 

    $p1=$str1[i]; 
    $p2=$str2[j]; 
    while(true) 
    { 
     if(ord($p1)<ord($p2)||($p1==''&&$p2=='')) 
     { 

      return($str1); 
     } 
     else 
     { 
      if(ord($p1)==ord($p2)) 
      { 
       $p1=$str1[++$i]; 
       $p2=$str2[++$j]; 
       continue; 
      } 
      return($str2); 
     } 
    }//end while 

    } //end function find string order 

public function is_empty() 
    { 
    if($this->root==null) 
     return(true); 
    else 
     return(false); 
    } 
}//end class BinaryTree 
?>