Home  >  Article  >  Backend Development  >  求将一段递归代码改成非递归形式

求将一段递归代码改成非递归形式

WBOY
WBOYOriginal
2016-06-13 11:59:25772browse

求将一段递归代码改为非递归形式

<br />function delRecursive($id,$class_arr)<br />{<br />	if($id=="") $id=0;<br />	for($i=0;$i<count($class_arr);$i++){<br />		if($class_arr[$i][3]==$id){<br />		    $subid=$class_arr[$i][0];<br />		    $this->db->delete('tree', array('id' => $id));<br />		    $this->db->delete('tree', array('id' => $subid));		<br />			delRecursive($subid,$class_arr);<br />		}	<br />	}<br />}<br />

------解决方案--------------------
怎么就不知道与人方便,与己方便的道理呢?

消除递归就是用自己的堆栈,代替系统的堆栈
$ar = array(<br />  array(  "1", "0", "顶级分类1",     "0", "1"),<br />  array("713", "0", "顶级分类2",     "0", "1"),<br />  array("716", "0", "一级子分类1", "713", "1"),<br />  array("718", "0", "顶级分类3",     "0", "1"),<br />  array("721", "0", "二级子分类1", "716", "1"),<br />);<br /><br />$id = 713;<br />delRecursive($id, $ar);<br />func($id, $ar);<br /><br />function func($id, $class_arr)<br />{<br />    if($id=="") $id=0;<br />    $st = array($id);<br />    do {<br />      $cnt = count($st);<br />      for($i=0;$i<count($class_arr);$i++){<br />        if(in_array($id = $class_arr[$i][3], $st)){<br />            $subid=$class_arr[$i][0];<br />            if(! in_array($subid, $st)) {<br />              $st[] = $subid;<br />echo "$id,$subid\n";   <br />//            $this->db->delete('tree', array('id' => $id));<br />//            $this->db->delete('tree', array('id' => $subid));    <br />//            delRecursive($subid,$class_arr);<br />            }<br />        }   <br />      }<br />    }while($cnt < count($st));<br />}<br /><br />function delRecursive($id, $class_arr)<br />{<br />    if($id=="") $id=0;<br />    for($i=0;$i<count($class_arr);$i++){<br />        if($class_arr[$i][3]==$id){<br />            $subid=$class_arr[$i][0];<br />//            $this->db->delete('tree', array('id' => $id));<br />//            $this->db->delete('tree', array('id' => $subid));    <br />echo "$id,$subid\n";   <br />            delRecursive($subid,$class_arr);<br />        }   <br />    }<br />}

------解决方案--------------------

<br>$st = array($id);<br>do {<br>                $cnt = count($st);<br>                 for($i=0;$i<count></count>                    if(in_array($class_arr[$i][3], $st)){<br>                        $subid=$class_arr[$i][0];<br>                            if(!in_array($subid, $st)) {<br>                                $st[] = $subid;<br>                                $this->DB_MT->delete('tree', array('id' => $id));<br>                                $this->DB_MT->delete('tree', array('id' => $subid));<br>                            }<br>                        }   <br>                    }<br>}while($cnt 
                 
              
              
        
            
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn