Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ankitraturi
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitraturi/b680ade46e229f962cda to your computer and use it in GitHub Desktop.
Save ankitraturi/b680ade46e229f962cda to your computer and use it in GitHub Desktop.
Breadth First Search in PHP for binary tree
<?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