国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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>';
?>


延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久久精 | 久久成人精品视频 | 嘿嘿视频在线观看 | 亚洲 自拍 另类 欧美 丝袜 | 欧美成人精品一区二区男人看 | 日韩成人影院 | 亚洲精品短视频 | 国产精品久久久久久久 | 免费大片黄在线观看 | 干干人人 | 欧美精品入口蜜桃 | 国产羞羞视频在线观看 | 九九视频在线 | 视频一区 中文字幕 | 大白屁股一区二区视频 | 国产精品视频导航 | 日韩有码在线播放 | 91社区福利 | 日韩一区二区三区在线 | 可以免费看黄的网站 | 久久精品国产精品青草 | 国产美女精品人人做人人爽 | 天天爽夜夜爽夜夜爽精品视频 | 狠狠综合久久 | 美女视频一区 | 在线国产视频观看 | 最近免费观看高清韩国日本大全 | 特级黄一级播放 | 国产资源免费观看 | 91中文在线 | 久久综合久久综合久久综合 | 国产精品日韩三级 | 国产精品自拍视频 | 午夜精品久久久久久 | 男女精品视频 | 国产精品一区二区三区四区 | 亚洲一区二区三区四区五区午夜 | 欧美精品99 | 国产区视频 | 亚洲三级视频 | 亚洲精品电影在线观看 |