一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP Class&Object -- PHP 自排序二叉樹的深入解析

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

2020-04-28 11:42PHP教程網 PHP教程

本篇文章是對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>';
?>


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产91高清在线观看 | 亚洲精品动漫免费二区 | 亚洲欧美在线免费观看 | 极品美女aⅴ高清在线观看 极品ts赵恩静和直男激战啪啪 | 天天舔天天干天天操 | 调教女高中生第3部分 | 香蕉久久一区二区三区啪啪 | 欧美一二 | 国产99区 | 精品国产一区二区三区国产馆 | 午夜亚洲一区二区福利 | 校花小雪灌满了男人们的浓浆 | 成人性用品 | 97影院手机版 | 日本在线视频免费看 | 99国产牛牛视频在线网站 | 精品国产自在现线拍400部 | 国产成人一区二区三区影院免费 | 国产精品va在线观看不 | 亚洲国产精品一区二区久久 | 天堂色| 国产全部理论片线观看 | 黑帮大佬与我的365天2标清中文 | 日韩三及片| 久久人妻熟女中文字幕AV蜜芽 | 国产午夜精品久久理论片小说 | 四虎影视永久在线 | 美女口述又粗又大感觉 | 国产美女亚洲精品久久久综合 | 日韩无砖2021特黄 | 亚洲日韩欧美一区二区在线 | 车上小婕子系列辣文小说 | free性泰国娇小videos | 久久无码人妻中文国产 | 女人把私密部位张开让男人桶 | 亚洲国产成人精品激情 | 国模娜娜一区二区三区 | 护士xxxx | 成年男女免费大片在线观看 | 国产三级精品91三级在专区 | 97影院网|