您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用php mysql在二叉树中查找插入位置和子节点数

使用php mysql在二叉树中查找插入位置和子节点数

function getNodeInsertPostionByParentId($parentId){
        $position = array('status'=>false);
        $parents = array();
        foreach($parentId as $parent){              
                $qry = "select customer_id,node_direction from mlm_nodes where parent_id =".$parent." order by node_direction";
                $rst = MysqL_query($qry);
                $count = MysqL_num_rows($rst);
                if($count==2){
                    while($row = MysqL_fetch_assoc($rst)){
                        $parents[$parent][] = $row['customer_id'];
                    }   
                }elseif($count==1){
                    $position['status'] = true;
                    $position['parentId'] = $parent;
                    $position['node'] = 'R';
                    //echo '<pre>1';print_r($position);echo '</pre>';
                    return $position;
                }else{
                    $position['status'] = true;
                    $position['parentId'] = $parent;
                    $position['node'] = 'L';
                    //echo '<pre>2';print_r($position);echo '</pre>';
                    return $position;
                }
            }

        return $this->searchByParents($parents);
    }

    function searchByParents($parents){

        foreach($parents as $parent){
            return $this->getNodeInsertPostionByParentId($parent);  
        }
    } 


echo '<pre>';print_r($this->getNodeInsertPostionByParentId(array('4')));die;
php 2022/1/1 18:50:54 有362人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶