Reverse level order traversal of binary tree in php
Php program for Reverse level order traversal of binary tree. Here problem description and other solutions.
<?php
/*
Php program for
Reverse level order traversal of binary tree
By using stack and queue
*/
// Binary Tree Node
class TreeNode
{
public $data;
public $left;
public $right;
public function __construct($data)
{
// Set node value
$this->data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
// Create node of (Queue,Stack)
class Node
{
public $element;
public $next;
public function __construct($node)
{
$this->element = $node;
$this->next = NULL;
}
}
class MyQueue
{
public $head;
public $tail;
public $count;
public function __construct()
{
$this->head = NULL;
$this->tail = NULL;
$this->count = 0;
}
public function size()
{
return $this->count;
}
public function isEmpty()
{
return $this->count == 0;
}
// Add new node of queue
public function enqueue($value)
{
// Create a new node
$x = new Node($value);
if ($this->head == NULL)
{
// Add first element into queue
$this->head = $x;
}
else
{
// Add node at the end using tail
$this->tail->next = $x;
}
$this->count++;
$this->tail = $x;
}
// Delete a element into queue
function dequeue()
{
if ($this->head == NULL)
{
// Empty Queue
return;
}
// Visit next node
$this->head = $this->head->next;
$this->count--;
if ($this->head == NULL)
{
// When deleting a last node of linked list
$this->tail = NULL;
}
}
// Get front node
public function peek()
{
if ($this->head == NULL)
{
return NULL;
}
return $this->head->element;
}
}
// Define a custom stack
class MyStack
{
public $top;
public $count;
public function __construct()
{
$this->top = NULL;
$this->count = 0;
}
public function size()
{
return $this->count;
}
// Add node at the top of stack
public function push($element)
{
// Create new node
$x = new Node($element);
$x->next = $this->top;
// Make new top
$this->top = $x;
$this->count++;
}
public function isEmpty()
{
if ($this->size() > 0)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
public function pop()
{
if ($this->size() > 0)
{
// Change top element of stack
$this->top = $this->top->next;
$this->count--;
}
}
// Return top element of stack
public function peek()
{
if ($this->size() == 0)
{
return NULL;
}
return $this->top->element;
}
}
class BinaryTree
{
public $root;
public function __construct()
{
$this->root = NULL;
}
public function reverseLevelOrder()
{
if ($this->root != NULL)
{
$q = new MyQueue();
$s = new MyStack();
// Add first node
$q->enqueue($this->root);
$node = $this->root;
while ($q->isEmpty() == false && $node != NULL)
{
if ($node->right != NULL)
{
// Add right child node
$q->enqueue($node->right);
}
if ($node->left != NULL)
{
// Add left child node
$q->enqueue($node->left);
}
// Add the resultant node
$s->push($node);
// Remove current node
$q->dequeue();
// Get new head
$node = $q->peek();
}
// Display result
while ($s->isEmpty() == false)
{
// Get top element
$node = $s->peek();
// Display level node
echo " $node->data";
// Remove top
$s->pop();
}
}
else
{
echo "Empty Tree\n";
}
}
public static
function main($args)
{
// Create new tree
$tree = new BinaryTree();
/*
Make A Binary Tree
-----------------------
1
/ \
/ \
2 3
/ / \
4 5 6
/ / \ \
7 8 9 10
*/
// Add node
$tree->root = new TreeNode(1);
$tree->root->left = new TreeNode(2);
$tree->root->right = new TreeNode(3);
$tree->root->right->right = new TreeNode(6);
$tree->root->right->right->right = new TreeNode(10);
$tree->root->right->left = new TreeNode(5);
$tree->root->left->left = new TreeNode(4);
$tree->root->left->left->left = new TreeNode(7);
$tree->root->right->left->left = new TreeNode(8);
$tree->root->right->left->right = new TreeNode(9);
// Display the reverse level order elements
$tree->reverseLevelOrder();
}
}
BinaryTree::main(array());
Output
7 8 9 10 4 5 6 2 3 1
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment