⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 07c11-2.php

📁 介绍PHP5的给类型函数应用
💻 PHP
字号:
<?phpob_start();require '07c11-1.php';ob_end_clean();// Define a class to implement self sorting binary treeclass 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, 90echo '<p>', implode(', ', $sort_as_you_go->returnSorted()), '</p>';?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -