天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP Class&Object -- PHP 自排序二叉樹的深入解析

在節點之間再應用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個節點,都讓滿足所有特定條件的元素都位于左節點及其子節點。在插入新元素時,我們需要從樹的第一個節 點(根節點)開始,判斷它屬于哪一側的節點,然后沿著這一側找到恰當的位置,類似地,在讀取數據時,只需要使用按序遍歷方法來遍歷二叉樹。
復制代碼 代碼如下:
<?php
ob_start();
// Here we need to include the binary tree class
Class Binary_Tree_Node() {
   // You can find the details at
}
ob_end_clean();
// Define a class to implement self sorting binary tree
class Sorting_Tree {
    // Define the variable to hold our tree:
    public $tree;
    // We need a method that will allow for inserts that automatically place
    // themselves in the proper order in the tree
    public function insert($val) {
        // Handle the first case:
        if (!(isset($this->tree))) {
            $this->tree = new Binary_Tree_Node($val);
        } else {
            // In all other cases:
            // Start a pointer that looks at the current tree top:
            $pointer = $this->tree;
            // Iteratively search the tree for the right place:
            for(;;) {
                // If this value is less than, or equal to the current data:
                if ($val <= $pointer->data) {
                    // We are looking to the left ... If the child exists:
                    if ($pointer->left) {
                        // Traverse deeper:
                        $pointer = $pointer->left;
                    } else {
                        // Found the new spot: insert the new element here:
                        $pointer->left = new Binary_Tree_Node($val);
                        break;
                    }
                } else {
                    // We are looking to the right ... If the child exists:
                    if ($pointer->right) {
                        // Traverse deeper:
                        $pointer = $pointer->right;
                    } else {
                        // Found the new spot: insert the new element here:
                        $pointer->right = new Binary_Tree_Node($val);
                        break;
                    }
                }
            }
        }
    }

    // Now create a method to return the sorted values of this tree.
    // All it entails is using the in-order traversal, which will now
    // give us the proper sorted order.
    public function returnSorted() {
        return $this->tree->traverseInorder();
    }
}
// Declare a new sorting tree:
$sort_as_you_go = new Sorting_Tree();
// Let's randomly create 20 numbers, inserting them as we go:
for ($i = 0; $i < 20; $i++) {
    $sort_as_you_go->insert(rand(1,100));
}
// Now echo the tree out, using in-order traversal, and it will be sorted:
// Example: 1, 2, 11, 18, 22, 26, 32, 32, 34, 43, 46, 47, 47, 53, 60, 71,
//   75, 84, 86, 90
echo '<p>', implode(', ', $sort_as_you_go->returnSorted()), '</p>';
?>

php技術PHP Class&amp;amp;Object -- PHP 自排序二叉樹的深入解析,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 收集最新中文国产中文字幕 | 久久久精品3d动漫一区二区三区 | 亚洲第一成年人网站 | 亚洲AV国产福利精品在现观看 | 暖暖 免费 高清 日本在线 | 国产欧美一区二区三区免费 | WWW夜片内射视频在观看视频 | 青青久在线视频免费观看 | 精品无码久久久久久国产百度 | 特大巨黑人吊性xxxxgay | 久久怡红院国产精品 | 消息称老熟妇乱视频一区二区 | 久久中文字幕乱码免费 | 综合人妻久久一区二区精品 | 青草久久精品亚洲综合专区 | 国产在线伊人 | 粉色视频午夜网站入口 | 野花高清在线观看免费3中文 | 国产成人拍精品视频网 | 国产中的精品AV一区二区 | 欧美成人3d动漫专区 | 久久国产精品福利影集 | 国产av在在免费线观看美女 | 四房播播开心色播 | a视频在线观看免费 | 毛片手机在线看 | 恋夜影院支持安卓视频美女 | 色影音先锋av资源网 | 乱亲女H秽乱长久久久 | 全彩黄漫火影忍者纲手无遮挡 | 富婆找黑人老外泻火在线播放 | 91久久99久91天天拍拍 | 国产亚洲福利在线视频 | 午夜AV亚洲一码二中文字幕青青 | 4hu四虎免费影院www | 影音先锋亚洲AV少妇熟女 | 久久国内精品视频 | 黑色丝袜美腿美女被躁翻了 | 世界第一黄暴漫画家 | 精品无码国产污污污免费网站2 | 亚洲日韩欧美国产专区 |