I was asked to traverse a binary tree by level , but in Spiral order
A
/ \
B C
/ \ / \
D E F G
should be traversed as
A
CB
DEFG
And the interviewer asked me to do it using a stack and a queue. here is the solution.
class Program
{
static char[] inputArray = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', };
static void Main(string[] args)
{
TreeTraversalBFS treeTraversal = new TreeTraversalBFS();
Console.WriteLine("*****************With Queue and Stack *******************");
Node root = treeTraversal.BuildBinaryTree(inputArray);
treeTraversal.DisplayNodesByCrisCross(root);
Console.WriteLine("***********************************************");
Console.ReadLine();
}
}
public class Node
{
public Node left { get; set; }
public Node right { get; set; }
public char data;
public Node(char data)
{
this.data = data;
}
}
class TreeTraversalBFS
{
/// <summary>
/// Build the binary tree
/// </summary>
/// <param name="inputArray"></param>
/// <returns></returns>
public Node BuildBinaryTree(char[] inputArray)
{
//to hold the nodes
Queue<Node> queue = new Queue<Node>();
Node root = new Node(inputArray[0]);
queue.Enqueue(root);
for (int i = 1; i < inputArray.Length;)
{
Node node = queue.Dequeue();
Node left = new Node(inputArray[i++]);
node.left = left;
queue.Enqueue(left);
if (i < inputArray.Length)
{
Node right = new Node(inputArray[i++]);
node.right = right;
queue.Enqueue(right);
}
}
return root;
}
/// <summary>
/// breadth-first using a queue and stack
/// </summary>
/// <param name="root"></param>
public void DisplayNodesByCrisCross(Node root)
{
if (root == null)
return;
Queue<Node> queue = new Queue<Node>();
Stack<Node> stack = new Stack<Node>();
queue.Enqueue(root);
int level = 0;
while (true)
{
if (level % 2 == 1)
{
int queuNodeCount = queue.Count;
if (queuNodeCount == 0)
break;
while (queuNodeCount > 0)
{
Node queueNode = queue.Dequeue();
Console.Write(queueNode.data);
if (queueNode.left != null)
{
stack.Push(queueNode.left);
//insert into queue as well to display next level left to right
queue.Enqueue(queueNode.left);
}
if (queueNode.right != null)
{
stack.Push(queueNode.right);
//insert into queue as well to display next level left to right
queue.Enqueue(queueNode.right);
}
queuNodeCount--;
}
}
else
{
int stackNodeCount = stack.Count;
while (stackNodeCount > 0)
{
Node stackNode = stack.Pop();
Node queueNode = queue.Dequeue();
//display data from stack
Console.Write(stackNode.data);
//add nodes from Queue and not from Stack to display next level nodes left to right
if (queueNode.left != null)
queue.Enqueue(queueNode.left);
if (queueNode.right != null)
queue.Enqueue(queueNode.right);
stackNodeCount--;
}
}
Console.WriteLine();
level++;
}
}
}
A
/ \
B C
/ \ / \
D E F G
should be traversed as
A
CB
DEFG
And the interviewer asked me to do it using a stack and a queue. here is the solution.
class Program
{
static char[] inputArray = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', };
static void Main(string[] args)
{
TreeTraversalBFS treeTraversal = new TreeTraversalBFS();
Console.WriteLine("*****************With Queue and Stack *******************");
Node root = treeTraversal.BuildBinaryTree(inputArray);
treeTraversal.DisplayNodesByCrisCross(root);
Console.WriteLine("***********************************************");
Console.ReadLine();
}
}
public class Node
{
public Node left { get; set; }
public Node right { get; set; }
public char data;
public Node(char data)
{
this.data = data;
}
}
class TreeTraversalBFS
{
/// <summary>
/// Build the binary tree
/// </summary>
/// <param name="inputArray"></param>
/// <returns></returns>
public Node BuildBinaryTree(char[] inputArray)
{
//to hold the nodes
Queue<Node> queue = new Queue<Node>();
Node root = new Node(inputArray[0]);
queue.Enqueue(root);
for (int i = 1; i < inputArray.Length;)
{
Node node = queue.Dequeue();
Node left = new Node(inputArray[i++]);
node.left = left;
queue.Enqueue(left);
if (i < inputArray.Length)
{
Node right = new Node(inputArray[i++]);
node.right = right;
queue.Enqueue(right);
}
}
return root;
}
/// <summary>
/// breadth-first using a queue and stack
/// </summary>
/// <param name="root"></param>
public void DisplayNodesByCrisCross(Node root)
{
if (root == null)
return;
Queue<Node> queue = new Queue<Node>();
Stack<Node> stack = new Stack<Node>();
queue.Enqueue(root);
int level = 0;
while (true)
{
if (level % 2 == 1)
{
int queuNodeCount = queue.Count;
if (queuNodeCount == 0)
break;
while (queuNodeCount > 0)
{
Node queueNode = queue.Dequeue();
Console.Write(queueNode.data);
if (queueNode.left != null)
{
stack.Push(queueNode.left);
//insert into queue as well to display next level left to right
queue.Enqueue(queueNode.left);
}
if (queueNode.right != null)
{
stack.Push(queueNode.right);
//insert into queue as well to display next level left to right
queue.Enqueue(queueNode.right);
}
queuNodeCount--;
}
}
else
{
int stackNodeCount = stack.Count;
while (stackNodeCount > 0)
{
Node stackNode = stack.Pop();
Node queueNode = queue.Dequeue();
//display data from stack
Console.Write(stackNode.data);
//add nodes from Queue and not from Stack to display next level nodes left to right
if (queueNode.left != null)
queue.Enqueue(queueNode.left);
if (queueNode.right != null)
queue.Enqueue(queueNode.right);
stackNodeCount--;
}
}
Console.WriteLine();
level++;
}
}
}