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

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

主站蜘蛛池模板: 久久久久久亚洲精品 | 日韩三级一区二区三区 | 色国产精品 | 九一国产精品视频 | 综合久久网 | 97国产精品视频观看一 | 欧美丝袜xxxxx在线播放 | 巨大巨粗巨长 黑人长吊视频 | 91久久久久久久 | 亚洲女人性视频 | 牛牛热在线视频 | 4hu四虎| 日本一区二区三区在线 视频 | 日本国产最新一区二区三区 | 6080yy午夜不卡一二三区久久 | 中文字幕在线有码高清视频 | 免费国产在线观看不卡 | 亚洲福利视频一区二区三区 | 欧美一区二区精品 | 欧美日本在线一区二区三区 | 日本高清一区二区三区不卡免费 | 伊人久久精品久久亚洲一区 | 日本一道本中文字幕 | 国内精品久久久久影院不卡 | 91亚洲精品色午夜麻豆 | 男女激情视频国产免费观看 | 久久久99精品免费观看精品 | 国产九九精品视频 | 看全色黄大色黄大片免责看 | 四虎影视国产永久免费 | 精品无人区乱码麻豆1区2区 | 久久中文字幕一区二区三区 | 日韩黄色在线观看 | 一区二区三区在线视频观看 | 韩国美女丝袜一区二区 | 日本视频www色变态 日本视频网站在线www色 | 六月色播| 国产夫妻小视频 | 中文激情| 欧美色欧美亚洲高清在线观看 | chinese在线播放91国内 |