Level order traversal using queue in scala

Scala program for Level order traversal using queue. Here more information.

/*
  Scala program for
  Level order tree traversal using queue
*/
// Create Q node
class QNode(var data: TreeNode,
	var next: QNode)
{
	def this(node: TreeNode)
	{
		this(node, null);
	}
}
// Binary Tree Node
class TreeNode(var data: Int,
	var left: TreeNode,
		var right: TreeNode)
{
	def this(data: Int)
	{
		// Set node value
		this(data, null, null);
	}
}
class MyQueue(var head: QNode,
	var tail: QNode,
		var count: Int)
{
	def this()
	{
		this(null, null, 0)
	}
	def size(): Int = {
		return this.count;
	}
	def isEmpty(): Boolean = {
		return this.count == 0;
	}
	// Add new node of queue
	def enqueue(value: TreeNode): Unit = {
		// Create a new node
		var node: QNode = new QNode(value);
		if (this.head == null)
		{
			// Add first element into queue
			this.head = node;
		}
		else
		{
			// Add node at the end using tail
			this.tail.next = node;
		}
		this.count += 1;
		this.tail = node;
	}
	// Delete a element into queue
	def dequeue(): Unit = {
		if (this.head == null)
		{
			// Empty Queue
			return;
		}
		// Visit next node
		this.head = head.next;
		this.count -= 1;
		if (this.head == null)
		{
			// When deleting a last node of linked list
			this.tail = null;
		}
	}
	// Get front node
	def peek(): TreeNode = {
		if (this.head == null)
		{
			return null;
		}
		return this.head.data;
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null)
	}
	def levelOrder(): Unit = {
		if (this.root != null)
		{
			var q: MyQueue = new MyQueue();
			// Add first node
			q.enqueue(this.root);
			var node: TreeNode = this.root;
			while (q.isEmpty() == false && node != null)
			{
				if (node.left != null)
				{
					// Add left child node
					q.enqueue(node.left);
				}
				if (node.right != null)
				{
					// Add right child node
					q.enqueue(node.right);
				}
				// Display level node
				print("  " + node.data);
				// Remove current node
				q.dequeue();
				// Get new head
				node = q.peek();
			}
		}
		else
		{
			println("Empty Tree");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new tree
		var tree: BinaryTree = new BinaryTree();
		/*
		  Make A Binary Tree
		  -----------------------
		         1
		       /   \
		      2     3
		     /     / \
		    4     5   6
		   /     / \
		  7     8   9
		*/
		// Adding tree nodes
		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.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 level order  element
		tree.levelOrder();
	}
}

Output

  1  2  3  4  5  6  7  8  9



Comment

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







© 2022, kalkicode.com, All rights reserved