一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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 自排序二叉樹的深入解析,轉載需保留來源!

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

主站蜘蛛池模板: 精品国产91乱码一区二区三区 | 国内精品视频 | 国产精品每日更新 | 欧美亚色 | 亚洲成人手机在线 | 国产精品成人观看视频网站 | 在线观看理论福利片 | 在线亚洲日产一区二区 | 国产美女白丝袜精品_a不卡 | 婷婷亚洲综合五月天小说在线 | 人人干在线视频 | 国产精品福利在线观看入口 | 精品久久久久久久久中文字幕 | 欧美日韩中文一区二区三区 | 国产欧美视频综合二区 | 视频免费1区二区三区 | 天天在线天天综合网色 | 91精品欧美综合在线观看 | 巨臀中文字幕一区二区翘臀 | 天天做天天爱夜夜爽 | www.欧美视频 | 欧洲女人性开放免费网站 | 97久久精品国产精品青草 | 欧美成人高清 | 午夜免费在线观看 | 国产成人精品一区 | 91精品国产免费久久久久久青草 | 亚洲国产lv| 久久天堂视频 | 男人扒开女人腿做爽爽视频网站 | 在线免费看污视频 | 久99久热只有精品国产99 | 狠狠综合久久久综合 | 久久久久久久久中文字幕 | 国产片欧美片亚洲片久久综合 | 亚洲线精品久久一区二区三区 | 成人爽a毛片在线视频 | 国产偷国产偷亚洲高清人乐享 | 香蕉一区二区三区观 | 国产午夜精品一区二区 | 在线亚洲精品国产成人二区 |