Деревья бинарного поиска в переполнении стека

Так что я действительно новичок в 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>

2

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector