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

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

在節(jié)點之間再應(yīng)用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個節(jié)點,都讓滿足所有特定條件的元素都位于左節(jié)點及其子節(jié)點。在插入新元素時,我們需要從樹的第一個節(jié) 點(根節(jié)點)開始,判斷它屬于哪一側(cè)的節(jié)點,然后沿著這一側(cè)找到恰當(dāng)?shù)奈恢茫愃频兀谧x取數(shù)據(jù)時,只需要使用按序遍歷方法來遍歷二叉樹。
復(fù)制代碼 代碼如下:
<?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)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 天天看片视频免费观看 | 美女内射少妇一区二区四区 | 亚洲 在线 日韩 欧美 | 日本欧美高清一区二区视频 | 7723手机游戏破解版下载 | 欧美尤物射精集锦 | 男人的天堂MV在线视频免费观看 | 网友自拍区视频精品 | 国产精品成人网 | 日韩AV无码一区二区三区不卡毛片 | 双性精跪趴灌满h室友4p | 灌饱娇嫩H将军公主最新章节 | 思思久99久女女精品 | 亚洲不卡视频在线观看 | 乌克兰黄色录像 | 最近在线视频观看2018免费 | av老司机色爱区综合 | 午夜视频在线瓜伦 | 女教师杨雪的性荡生活 | 撕烂衣服扒开胸罩揉爆胸 | 久久精品综合网中文字幕 | 在线观看免费精品国产 | 果冻传媒视频在线观看完整版免费 | 暖暖 日本 视频 在线观看免费 | 国产精品免费久久久久影院 | 2020精品国产视 | 暖暖 免费 高清 日本视频大全 | 日韩在线av免费视久久 | 亚洲无AV在线中文字幕 | 午夜久久影院 | 国产精品1卡二卡三卡四卡乱码 | 亚洲一卡久久4卡5卡6卡7卡 | 成人精品视频在线观看播放 | 国产精人妻无码一区麻豆 | 国产精品免费大片一区二区 | 花蝴蝶在线观看中字 | 日韩一区精品视频一区二区 | 国产伦精品一区二区免费 | 欧美亚洲精品一区二三区8V | 九九热综合| 男人扒开添女人下部口述 |