PHP Class&Object -- 解析PHP实现二叉树

yipeiwu_com5年前PHP代码库
二叉树及其变体是数据结构家族里的重要组成部分。最为链表的一种变体,二叉树最适合处理需要一特定次序快速组织和检索的数据。
复制代码 代码如下:

<?php
// Define a class to implement a binary tree
class Binary_Tree_Node {
    // Define the variable to hold our data:
    public $data;
    // And a variable to hold the left and right objects:
    public $left;
    public $right;

    // A constructor method that allows for data to be passed in
    public function __construct($d = NULL) {
        $this->data = $d;
    }

    // Traverse the tree, left to right, in pre-order, returning an array
    // Preorder means that each node's value preceeds its children.
    public function traversePreorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traversePreorder(); }
        if ($this->right) { $r = $this->right->traversePreorder(); }

        // Return a merged array of the current value, left, and right:
        return array_merge(array($this->data), $l, $r);
    }
    // Traverse the tree, left to right, in postorder, returning an array
    // Postorder means that each node's value follows its children.
    public function traversePostorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traversePostorder(); }
        if ($this->right) { $r = $this->right->traversePostorder(); }

        // Return a merged array of the current value, left, and right:
        return array_merge($l, $r, array($this->data));
    }
    // Traverse the tree, left to right, in-order, returning an array.
    // In-order means that values are ordered as left children, then the
    //  node value, then the right children.
    public function traverseInorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traverseInorder(); }
        if ($this->right) { $r = $this->right->traverseInorder(); }

        // Return a merged array of the current value, left, and right:
        return array_merge($l, array($this->data), $r);
    }
}
// Let's create a binary tree that will equal the following:    3
//                                                             / /     
//                                                            h   9     
//                                                               / /    
// Create the tree:                                             6   a   
$tree = new Binary_Tree_Node(3);
$tree->left = new Binary_Tree_Node('h');
$tree->right = new Binary_Tree_Node(9);
$tree->right->left = new Binary_Tree_Node(6);
$tree->right->right = new Binary_Tree_Node('a');
// Now traverse this tree in all possible orders and display the results:
// Pre-order: 3, h, 9, 6, a
echo '<p>', implode(', ', $tree->traversePreorder()), '</p>';
// Post-order: h, 9, 6, a, 3
echo '<p>', implode(', ', $tree->traversePostorder()), '</p>';
// In-order: h, 3, 6, 9, a
echo '<p>', implode(', ', $tree->traverseInorder()), '</p>';
?>

相关文章

php文件读取方法实例分析

本文实例讲述了php文件读取方法。分享给大家供大家参考。具体如下: <?php $file = fopen("Test//file.txt", "r"); //打开文...

php简单压缩css样式示例

本文实例讲述了php简单压缩css样式的方法。分享给大家供大家参考,具体如下: $css = ''; //找css目录 $root = $_SERVER['DOCUMENT_ROOT...

PHP curl 并发最佳实践代码分享

本文将探讨两种具体的实现方法, 并对不同的方法做简单的性能对比. 1. 经典cURL并发机制及其存在的问题 经典的cURL实现机制在网上很容易找到, 比如参考PHP在线手册的如下实现方式...

PHP fclose函数用法总结

php fclose()函数 语法 作用:关闭一个打开文件 语法: fclose(file) 参数: file 必需。规定要关闭的文件。 说明:如果成功则返回 true,否则返回...

用PHP实现递归循环每一个目录

函数的原理很简单,主要就是用了一下递归调用。 复制代码 代码如下: function file_list($path){ if ($handle = opendir($path)) {...