中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

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

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

主站蜘蛛池模板: 久久久久久国产精品三区 | 激情视频一区 | 国产精品久久国产精品久久 | 亚洲精品综合一区二区 | 亚洲综合久久网 | 香蕉一区| 日韩精品一区二区三区免费观看 | 亚洲色综合| 国产 日韩 欧美 在线 | 天天干天天操天天爽 | 精品国产一区二区三区在线观看 | 亚洲高清在线观看 | 一区二区三区在线免费观看 | 中文字幕一区二区三区精彩视频 | 欧美日韩久久 | 免费中文字幕日韩欧美 | 国产精品成人在线播放 | 欧美激情国产精品 | 久久国产精品网站 | 青春草91| 国产一区欧美 | 国产麻豆乱码精品一区二区三区 | jdav视频在线观看免费 | 久国产视频 | 在线播放一区二区三区 | 国产精品久久久久久久久久 | 亚洲精品视频免费观看 | 亚洲人成人网 | 国产视频福利 | 成人1区2区| 欧美日韩精选 | 亚洲精品综合 | 在线成人免费av | 日本aⅴ中文字幕 | 亚洲电影在线播放 | 成人日韩| 日本 欧美 国产 | 亚洲视频二区 | 亚洲精品视频免费观看 | 爱爱视频日本 | 精品久久一区二区三区 |