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

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

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>';
?>


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色综合久久天天综合 | 成年性香蕉漫画在线观看 | 欧美一级片免费 | 清纯漂亮女友初尝性过程 | 日韩美毛片| 啊好爽视频 | 成人国产精品一区二区不卡 | 扒开斗罗美女了的胸罩和内裤漫画 | 亚洲国产精品第一区二区三区 | 草草视频在线免费观看 | 我和岳偷长篇小说 | 夫妻性生活在线 | 国产在线观看a | 国产成人精品1024在线 | 国产九九视频在线观看 | 久久一er精这里有精品 | 国产精亚洲视频 | 久久久这里有精品999 | 亚洲spank男男实践网站 | 911福利视频 | 国产成人手机在线好好热 | 国产婷婷高清在线观看免费 | 西施打开双腿下面好紧 | 高清一级做a爱免费视 | 国产精品免费久久久久影院 | 亚洲 日本 天堂 国产 在线 | 亚洲天堂网站 | 成人综合网站 | 亚洲精品色图 | 国产一区二区三区在线看 | 日韩欧美中文字幕一区二区三区 | 国产亚洲欧美一区二区三区 | 亚洲精品AV无码喷奶水糖心 | 成人精品视频 成人影院 | 欧美精品一区二区三区免费播放 | 天美传媒tm0087 | 欧美亚洲影院 | 猫咪免费人成网站在线观看入口 | 久久噜国产精品拍拍拍拍 | 成年私人影院免费视频网站 | 高黄h文各种play |