Skip to content

Commit

Permalink
studying the code and adding more comments to understand
Browse files Browse the repository at this point in the history
it better 🤨🤔
  • Loading branch information
ideaguy3d committed Jan 1, 2020
1 parent 01d6c50 commit af11fa3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
72 changes: 61 additions & 11 deletions @cs/BinaryTree.php → @cs/BinarySearchTree.php
@@ -1,17 +1,25 @@
<?php
namespace julius;

/**
* Created by PhpStorm.
* User: Julius Alvarado
* Date: 8/17/2019
* Time: 1:17 AM
*/

namespace julius;

class BinaryTree
class BinarySearchTree
{
private $root = null;


/**
* Primary Public Function
*
* Makes an indirect recursive call
*
* @param $key
*/
public function insert($key) {
$node = new class ($key) {
public $key;
Expand All @@ -32,8 +40,20 @@ public function __construct($key) {
$this->insertNode($this->root, $node);
}
} // END OF: insert()

private function insertNode($node, $newNode) {

/**
* Private Recursive Helper Function
*
* We don't operate on the result of recursive call so we don't
* return the recursive call.
*
* The recursive call will just do internal work so nothing has
* to be returned
*
* @param $node
* @param $newNode
*/
private function insertNode($node, $newNode): void {
if($newNode->key < $node->key) {
if($node->left === null) {
$node->left = $newNode;
Expand All @@ -51,11 +71,20 @@ private function insertNode($node, $newNode) {
}
}
} // END OF: insertNode()


/**
* @return mixed root - can be null or a Node
*/
public function getRoot() {
return $this->root;
}


/**
* Primary Public function
*
* @param $key
* @return bool
*/
public function search($key) {
return $this->searchNode($this->root, $key);
}
Expand All @@ -74,19 +103,40 @@ private function searchNode($node, $key) {
return true;
}
}


/**
* Primary Public Function
*
* @param \Closure $callback
*/
public function inOrderTraverse(\Closure $callback) {
$this->inOrderTraverseNode($this->root, $callback);
}


/**
* Private Recursive Helper Function
*
* @param $node
* @param $callback
*/
private function inOrderTraverseNode($node, $callback) {
$key = $node->key ?? 'null';
echo "\ncurrent key: $key";

if($node !== null) {
$this->inOrderTraverseNode($node->left, $callback);
$callback($node->key);
$recurse1 = "point1";
$callback($key);
$this->inOrderTraverseNode($node->right, $callback);
$recurse2 = 'point2';
}
}


/**
* Primary Public Function
*
* @param \Closure $callback
*/
public function preOrderTraverse(\Closure $callback) {

}
Expand Down
17 changes: 11 additions & 6 deletions @cs/index.php
Expand Up @@ -6,27 +6,32 @@
* Time: 1:46 AM
*/

require('BinaryTree.php');
require('BinarySearchTree.php');

use julius\BinaryTree;
use julius\BinarySearchTree;

$set1 = [11, 7, 15, 5, 3, 9, 8, 10, 13, 12, 14, 20, 18, 25, 6];

$binaryTree = new BinaryTree();
$binaryTree = new BinarySearchTree();

foreach($set1 as $v) {
$binaryTree->insert($v);
}

echo "\n----------------------------------------------------------------------\n";
echo "\n----------------------------------------------------------------------------------\n";

$callback = function($value){
$treeSorted = [];
$callback = function($value) use (&$treeSorted) {
$treeSorted[] = "n<$value>";
echo " [$value] ";
};

$binaryTree->inOrderTraverse($callback);

echo "\n----------------------------------------------------------------------\n";
echo "\n\n";
var_export($treeSorted);

echo "\n----------------------------------------------------------------------------------\n";



Expand Down

0 comments on commit af11fa3

Please sign in to comment.