镇坪县 黄平县 越西县 板桥市 禹城市 无为县 华阴市 揭阳市 瑞金市 沾益县 临湘市 嘉义县 锦州市 信丰县 长沙市 宜州市
2018年最新新闻网

您当前的位置:主页 > 2018年最新新闻网国内 >

作者:公辛 来源:原创 发布日期:05-29

巴哥犬_二叉树结构 统计每一个叶子节点下的节点的数量

标签:埃默森 斗地主说活跃度不够

类如下:

<?php
class getUserLeftRightNumber
{
    public $arr_all;
    public $arr_init;
    public $arr_leaf;
    public $arr_res;
    public function __counstruct()
    {
    
    }

    public function getUserNum情牵两世_2018年最新新闻网()
    {
        // ini_set("memory_limit","6144M");
        // set_time_limit(0);

        // header("content-type:text/html;charset=utf-8"); 
        // define("DB_HOST","127.0.0.1"); 
        // define("DB_USER","root"); 
        // define("DB_PASS","root"); 
        // define("DB_NAME","test"); 
        // define("DB_PORT",3306); 
        // define("DB_CHAR","utf8"); 
        // define("APPNAME",""); 
        // $mysqli = new mysqli( DB_HOST, DB_USER, DB_PASS, DB_NAME);
        济宁3.6级地震_2018年最新新闻网// $rst = $mysqli->query( "set names " . DB_CHAR );
        // $sql = "select * from ub_user_expand";
        // $result=$mysqli->query($sql);
        // $array=array(); 
        // while( $rows=$result->fetch_assoc() )
        // {
        //     $r["uid"] = $rows["uid"];
        //     $r["name"] = $rows["username"];
        //     $r["_referee"] = $rows["_referee"];
        //     $r["_left"] = $rows["_left"];
        //     $r["_right"] = $rows["_right"];
        //     $r["position"] = $rows["position"];

        //     $this->arr_all[] = $r;
        // }
        // print_r($this->arr_all);die();

        $this->arr_all[0]  = array("uid" => "10000000", "_referee" => "","name" => "101001", "_left" => "10000001", "_right" => "10000002", "position" => "" );
        $this->arr_all[1]  = array("uid" => "10000001", "_referee" => "101001", "name" => "102001", "_left" => "10000003", "_right" => "10000004", "position" => 1 );
        $this->arr_all[2]  = array("uid" => "10000002", "_referee" => "101001", "name" => "102002", "_left" => "10000005", "_right" => "10000006", "position" => 2 );
        $this->arr_all[3]  = array("uid" => "10000003", "_referee" => "102001", "name" => "103001", "_left" => "10000007", "_right" => "10000008", "position" => 1 );
        $this->arr_all[4]  = array("uid" => "10000004", "_referee" => "102001", "name" => "103002", "_left" => "10000009", "_right" => "1000001英国军人策划恐袭_2018年最新新闻网     $this->arr_all[18] = array("uid" => "10000018", "_referee" => "104002", "name" => "105004", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[19] = array("uid" => "10000019", "_referee" => "104003", "name" => "105005", "_left" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[20] = array("uid" => "10000020", "_referee" => "104003", "name" => "105006", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[21] = array("uid" => "10000021", "_referee" => "104004", "name" => "105007", "_left" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[22] = array("uid" => "10000022", "_referee" => "104004", "name" => "105008", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[23] = array("uid" => "10000023", "_referee" => "104005", "name" => "105009", "_l崂山_2018年最新新闻网eft" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[24] = array("uid" => "10000024", "_referee" => "104005", "name" => "105010", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[25] = array("uid" => "10000025", "_referee" => "104006", "name" => "105011", "_left" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[26] = array("uid" => "10000026", "_referee" => "104006", "name" => "105012", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[27] = array("uid" => "10000027", "_referee" => "104007", "name" => "105013", "_left" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[28] = array("uid" => "10000028", "_referee" => "104007", "name" => "105014", "_left" => "",  "_right" => "", "position" => 2 );
        $this->arr_all[29] = array("uid" => "10000029", "_referee" => "104008", "name" => "105015", "_left" => "",  "_right" => "", "position" => 1 );
        $this->arr_all[30] = array("uid" => "10000030", "_referee" => "104008", "name" => "105016", "_left" => "",  "_right" => "", "position" => 2 );


        // 循环处理数据 获取 叶子节点
        foreach ( $this->arr_all as $key => $value ) 
        {
            $this->arr_init[$value["name"]] = $value;
            if ( empty( $value["_left"] ) && empty( $value["_right"] ) ) 
            {
                $this-&陆龟_2018年最新新闻网gt;arr_leaf[] = $value;
            }
        }

        foreach ( $this->arr_leaf as $key => $value ) 
        {
            $this->getUserNumInit( $this->arr_init, $value, 0 );
        }

        print_r( $this->arr_res );
    
    }

    public function getUserNumInit( $all, $leaf, $count_last )
    {
        if ( !empty( $leaf ) && is_array( $leaf ) && !empty($leaf["_referee"]) ) 
        {
            // echo "<br/>".$leaf["name"]."<br/>";
            $count      = 0;
            $count_self = 1;
            $count_all  = $count_self + $count_last;

            $res_key    = $leaf["_referee"];

            $this->arr_res[$res_key]["uid"] = $this->arr_init[$leaf["_referee"]]["uid"];
            $this->arr_res[$res_key]["uname"] = $res_key;

            if ( $leaf["position"] == "left" ) 
            {
                $this->arr_res[$res_key]["numLeft"] = $count_all;
                $count = $count_all;

                if ( isset( $this->arr_res[$res_key]["numRight"]) ) 
                {
                    $count = $this->arr_res[$res_key]["numLeft"] + $this->arr_res[$res_key]["numRight"];
                }
            }
            elseif ( $leaf["position"] == "right" ) 
            {
                $this->arr_res[$res_key]["numRight"] = $count_all;
                $count = $count_all;
                if ( isset( $this->arr_res[$res_key]["numLeft"]) ) 
                {
                    $count = $this->arr_res[$res_key]["numLeft"] + $this->arr_res[$res_key]["numRight"];
                }
            }
            else 
            {
                return false;
            }

            // 获取上级、然后递归
            $referee = isset($all[$leaf["_referee"]])&&!empty($leaf["_referee"]) ? $all[$leaf["_referee"]] : "";
            if ( !empty( $referee ) ) 
            {
                $this->getUserNumInit( $all, $referee, $count );
            }

        }
    }
}

使用

$obj = new getUserLeftRightNumber();
$obj->getUserNum();

当前文章:http://www-cijilie-com.kunenergy.cn/qa6s/8659d_151357.html

发布时间:2019-07-23 07:34:09

银河娱乐首页yh163am.com  919银河优越会  2949.com澳门银河娱乐  银河优越会下载  www.7163.com澳门银河  银河娱乐平台yh163am.com  澳门银河yh7788.bet  澳门银河官网真人娱乐  2949.com澳门银河娱乐  世界杯投注  

Copyright @ 2016-2018 2018年最新新闻网 版权所有