Last active
August 29, 2015 14:17
-
-
Save ankitraturi/b680ade46e229f962cda to your computer and use it in GitHub Desktop.
Breadth First Search in PHP for binary tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Class Test extends SplQueue | |
{ | |
} | |
$queue = []; | |
//SplQueue new object | |
$dummyQueue = new test(); | |
//id is the root node you have to pass | |
$this->traverseTree($id, $dummyQueue); | |
function traverseTree($rootNode, $dummyQueue) | |
{ | |
if ($rootNode->lft != 0) { | |
$dummyQueue->enqueue($rootNode->lft); | |
} | |
if ($rootNode->rgt != 0) { | |
$dummyQueue->enqueue($rootNode->rgt); | |
} | |
if (!($dummyQueue->isEmpty())) { | |
$nextId = $dummyQueue->dequeue(); | |
$nextNode = //get next node information using $nextId | |
array_push($this->queue, $nextNode); | |
$this->traverseTree($nextNode, $dummyQueue); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment