最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年06月25日 09:00:21   作者:  
本篇文章是對PHP中的自排序二叉樹進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在節(jié)點之間再應(yīng)用一些排序邏輯,二叉樹就能提供出色的組織方式。對于每個節(jié)點,都讓滿足所有特定條件的元素都位于左節(jié)點及其子節(jié)點。在插入新元素時,我們需要從樹的第一個節(jié) 點(根節(jié)點)開始,判斷它屬于哪一側(cè)的節(jié)點,然后沿著這一側(cè)找到恰當(dāng)?shù)奈恢?,類似地,在讀取數(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>';
?>

相關(guān)文章

  • PHP管理依賴(dependency)關(guān)系工具 Composer 安裝與使用

    PHP管理依賴(dependency)關(guān)系工具 Composer 安裝與使用

    Composer 是PHP中用來管理依賴(dependency)關(guān)系的工具。你可以在自己的項目中聲明所依賴的外部工具庫(libraries),Composer會幫你安裝這些依賴的庫文件。
    2014-08-08
  • PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程

    PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程

    在本篇文章中我們給大家總結(jié)了一篇關(guān)于PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。
    2019-01-01
  • php讀取csc文件并輸出

    php讀取csc文件并輸出

    本文給大家分享的是php讀取csc文件并輸出的方法,方法一用到的是fgetcsv函數(shù),方法二用到是fopen函數(shù),有需要的小伙伴可以參考下。
    2015-05-05
  • php從字符串創(chuàng)建函數(shù)的方法

    php從字符串創(chuàng)建函數(shù)的方法

    這篇文章主要介紹了php從字符串創(chuàng)建函數(shù)的方法,涉及php中字符串與create_function函數(shù)的使用技巧,需要的朋友可以參考下
    2015-03-03
  • PHP實現(xiàn)json_decode不轉(zhuǎn)義中文的方法

    PHP實現(xiàn)json_decode不轉(zhuǎn)義中文的方法

    這篇文章主要介紹了PHP實現(xiàn)json_decode不轉(zhuǎn)義中文的方法,結(jié)合實例形式具體分析了php5.4+及5.3版本針對json_decode實現(xiàn)不轉(zhuǎn)義中文的具體操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2017-05-05
  • PHP判斷文件是否被引入的方法get_included_files用法示例

    PHP判斷文件是否被引入的方法get_included_files用法示例

    這篇文章主要介紹了PHP判斷文件是否被引入的方法get_included_files用法,結(jié)合實例形式分析了get_included_files函數(shù)獲取引入文件及遍歷輸出的操作技巧,需要的朋友可以參考下
    2016-11-11
  • PHP快速排序quicksort實例詳解

    PHP快速排序quicksort實例詳解

    這篇文章主要介紹了PHP快速排序quicksort實現(xiàn)方法,結(jié)合實例形式分析了快速排序的原理及php實現(xiàn)快速排序的相關(guān)操作技巧,需要的朋友可以參考下
    2016-09-09
  • thinkphp框架實現(xiàn)數(shù)據(jù)添加和顯示功能

    thinkphp框架實現(xiàn)數(shù)據(jù)添加和顯示功能

    這篇文章主要為大家詳細(xì)介紹了thinkphp框架實現(xiàn)數(shù)據(jù)添加和顯示功能的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • php7.3報preg_match()?JIT?compilation?failed?no?more?memory解決辦法

    php7.3報preg_match()?JIT?compilation?failed?no?more?mem

    PHP?JIT編譯失敗,內(nèi)存不足的解決方法!你是否遇到過這個問題?不用擔(dān)心,我們將為你提供簡單易懂的解決方案,讓你擺脫這一困擾,立即閱讀我們的指南,輕松解決PHP?JIT編譯失敗的煩惱!
    2023-12-12
  • php生成圖形驗證碼幾種方法小結(jié)

    php生成圖形驗證碼幾種方法小結(jié)

    生成圖形驗證碼需要使用php GD庫來生成,如果你沒開戶GD庫我們需要在php.ini文件找到extension=php_gd2.dll 去掉前面的;就行了,然后重啟apache 或iis環(huán)境即可
    2013-08-08

最新評論

正蓝旗| 三门峡市| 长垣县| 元江| 瓦房店市| 宾阳县| 铜鼓县| 崇阳县| 江西省| 灯塔市| 广宗县| 乐清市| 合作市| 胶南市| 金昌市| 永安市| 潞西市| 阳谷县| 凤台县| 桃园县| 内乡县| 临湘市| 志丹县| 浦城县| 仪陇县| 砚山县| 贺兰县| 万年县| 河东区| 通化县| 新竹县| 山东省| 辽源市| 汕头市| 孝昌县| 静安区| 芷江| 三都| 长垣县| 和田县| 黑山县|