在PHP中的二进制搜索树

时间:2015-02-08 00:03:11

标签: php tree binary-tree binary-search-tree

所以我是php的新手(昨天开始学习) 我在实现二叉搜索树时遇到了一些麻烦。 我希望能够从文件中读取行(我已经好了)并将它们转换为二叉树中的节点。 我需要能够向树中添加节点并在其中搜索值,但是,它没有做任何事情。

到目前为止,这是我的代码,有人能看到这个问题吗?

<?php
    session_start();
?>

<html>
<head>
</head>

<body>


    <?php 
        echo $_SESSION[$datefull];
        $query = $_SESSION[$datefull];

        echo '</br></br>';

        readDates("dates_list.txt");
        createNodes();
        insertNodes();

    ?>

</p>

    <?php
        function readDates($file) {
            $datesList = fopen($file, "a+") or die("Unable to open file");

            $i = 0;
            $lines = array();
            while(!feof($datesList)){

                $lines[$i] = fgets($datesList);
                echo $lines[$i] . "</br>";
            }
            fclose($datesList);
        }
    ?>


<?php 
    class Node 
    {
        public $level = 1;
        public $data = NULL;
        public $left = NULL;
        public $right = NULL;

        public function __construct($data) 
        {
            $this->data = $data;
        }
    }

    class BinarySearchTree 
    {
        private $_topNode = NULL;
        private $_count = 0;
        private $_height = 1;

        /**
        * Insert a value into the tree.
        */
        public function insert($data) 
        {
            $newNode = new Node($data);

            if ( $this->_topNode === NULL ) 
            {
                $this->_topNode = &$newNode;
            } 
            else 
            {
                $current = $this->_topNode;

                    while ( $current !== NULL ) 
                    {
                        if ( $data < $current->data ) 
                        {
                            $newNode->level++;

                            if ( $current->left !== NULL ) 
                            {
                                $current = $current->left;
                            } 
                            else 
                            {
                                $current->left = $newNode;
                                break;
                            }
                        } 
                        else if ( $data > $current->data ) 
                        {
                            $newNode->level++;

                            if ( $current->right !== NULL ) 
                            {
                                $current = $current->right;
                            } 
                            else 
                            {
                                $current->right = $newNode;
                                break;
                            }
                        } 
                        else 
                        {
                            return;
                        }
                    }
                }

            $this->_count++;
        }


        /**
        * Search the tree for a given value.
         */
        public function search($data) 
        {
            $current = $this->_topNode;

            while ( $current !== NULL ) 
            {
                if ( $data < $current->data ) 
                {
                    if ( $current->left !== NULL ) 
                    {
                        $current = $current->left;
                    } 
                    else 
                    {
                        return FALSE;
                    }
                } 
                else if ( $data > $current->data ) 
                {
                    if ( $current->right !== NULL ) 
                    {
                        $current = $current->right;
                    } 
                    else 
                    {
                        return FALSE;
                    }
                } 
                else 
                {
                    return $current;
                }
            }
        }

        /**
         * Return the size of the tree.
         */
        public function size() 
        {
            return $this->_count;
        }
?>


    <?php
        function createNodes()
        {
            $tree = new BinarySearchTree();
            $nodes = array();
            for($j = 0; $j < count($lines); $j++)
            {
                $temp = $lines[$j];
                $nodes[$j] = new Node($temp);
            }
        }

        function insertNodes()
        {
            for($k = 0; $k < count($nodes); $k++)
            {
                $temp = $nodes[$k];
                $tree->insert($temp);
            }
        }
    ?>
</body> 


</html>

0 个答案:

没有答案