激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 圆产精品久久久久久久久久久 | www.99久 | 性少妇freeseⅹbbwhd | 日韩黄色免费观看 | 久久99国产精品久久99 | 中文字幕精品一区久久久久 | 久久精品1区2区 | 国产精品成人亚洲一区二区 | 久久99免费观看 | 国产午夜探花 | 九九热视频免费观看 | chinesexxxx刘婷hd 国产91在线播放九色 | 极品大长腿啪啪高潮露脸 | 成年人黄色免费网站 | 久久精品一区二区三区四区五区 | 亚洲精品成人18久久久久 | 精品一区二区中文字幕 | 欧美成人一区二区三区电影 | 99国产精品国产免费观看 | 蜜桃久久一区二区三区 | 福利在线播放 | 中文字幕 亚洲一区 | 欧美色爱综合 | 黄色片网站在线看 | www.guochan| 又黄又爽免费无遮挡在线观看 | 午夜视频你懂的 | 污片视频在线观看 | 成人免费入口 | 国产精品久久久久久久不卡 | 斗破苍穹在线免费 | 男女羞羞视频在线观看免费 | 暴力强行进如hdxxx | 国产一区二区午夜 | 久久精品无码一区二区三区 | 国产免费大片视频 | 国产精品久久久久久久久久三级 | 青青国产在线视频 | 日本欧美国产 | 久久精品首页 | 欧美又黄又嫩大片a级 |