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

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

在節(jié)點之間再應用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個節(jié)點,都讓滿足所有特定條件的元素都位于左節(jié)點及其子節(jié)點。在插入新元素時,我們需要從樹的第一個節(jié) 點(根節(jié)點)開始,判斷它屬于哪一側(cè)的節(jié)點,然后沿著這一側(cè)找到恰當?shù)奈恢茫愃频兀谧x取數(shù)據(jù)時,只需要使用按序遍歷方法來遍歷二叉樹。
復制代碼 代碼如下:
<?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技術(shù)PHP Class&amp;amp;Object -- PHP 自排序二叉樹的深入解析,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 亚洲免费无码中文在线亚洲在 | 国产亚洲国际精品福利 | 国产国拍亚洲精品永久软件 | 我把寡妇日出水好爽 | 伦理片92伦理午夜 | 草莓视频免费在线观看 | 国产精品视频yy9099 | 久久精品国产欧美 | 麻豆蜜桃国语精品无码视频 | 亚洲日韩天堂在线中文字幕 | 草莓西瓜樱桃香蕉直播视频 | 国产亚洲精品品视频在线 | 国产av免费观看日本 | 绝逼会被锁 | 国产色婷婷精品人妻蜜桃成熟 | 最新无码国产在线视频9299 | 国产三级在线精品男人的天堂 | 亚洲成人日韩 | 国内2018年午夜福利5678 | A级超碰视频在线观看 | 动漫H片在线播放免费高清 动漫AV纯肉无码AV电影网 | 久久99亚洲热最新地址获取 | 97国产揄拍国产精品人妻 | 久久久精品日本一区二区三区 | 国产无遮挡色视频免费观看性色 | 国产午夜精品一区二区理论影院 | 亚洲无AV在线中文字幕 | 嫩草视频在线观看免费 | 伊人色综合久久天天 | 成人永久免费视频 | 国产精品久久久久久久久久免费 | 91精品国产色综合久久不 | 日韩一区二区三区射精 | 日本久久不射 | 在线视频 亚洲 | 国产人妻人伦精品836700 | 熟妇久久无码人妻AV蜜桃 | 国产乱子影视频上线免费观看 | CHINA篮球体育飞机2023 | 泷泽萝拉首部av | 国内精品久久久久久久试看 |