Skip to main content

Print vertical order of binary tree in scala

Scala program for Print vertical order of binary tree. Here problem description and explanation.

import scala.collection.mutable._;
/* 
  Scala program for
  Print vertical traversal of binary tree
*/
// 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);
	}
}
// Create Q node
class QNode(var data: TreeNode,
	var next: QNode,
		var distance: Int)
{
	def this(node: TreeNode)
	{
		this(node, null, 0);
	}
}
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, d: Int): 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;
			// Set node distance
			node.distance = d;
		}
		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)
	}
	// Find vertical elements
	def getVerticalNode(
      record: HashMap[Int, ArrayBuffer[Int]]): Unit = {
		if (this.root != null)
		{
			var q: MyQueue = new MyQueue();
			// Add first node
			q.enqueue(this.root, 0);
			var node: TreeNode = this.root;
			var d: Int = 0;
			while (q.isEmpty() == false)
			{
				// Get distance
				d = q.head.distance;
				if (node.left != null)
				{
					// Add left child node
					q.enqueue(node.left, d - 1);
				}
				if (node.right != null)
				{
					// Add right child node
					q.enqueue(node.right, d + 1);
				}
				if (!record.contains(d))
				{
					// Add new distance
					record.addOne(d, new ArrayBuffer[Int]());
				}
				record.get(d).get += node.data;
				// Remove current node
				q.dequeue();
				// Get new head
				node = q.peek();
			}
		}
	}
	def verticalView(): Unit = {
		// This is store result
		var record: HashMap[Int, ArrayBuffer[Int]] = 
          new HashMap[Int, ArrayBuffer[Int]]();
		getVerticalNode(record);
		var distance: Int = 0;
		// Find first leftmost element
		while (record.contains(distance - 1))
		{
			distance -= 1;
		}
		// Display result
		while (record.contains(distance))
		{
			// This loop display vertical element
			for (v <- record.get(distance).get)
			{
				print("  " + v);
			}
			println();
			distance += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new tree
		var tree: BinaryTree = new BinaryTree();
		/*
		  Binary Tree
		  -------------
		      10
		     /  \
		    2    4
		   /    / \
		  3    6   5
		   \    \
		    9    7
		     \    \
		      1    11 
		*/
		tree.root = new TreeNode(10);
		tree.root.left = new TreeNode(2);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.left.right = new TreeNode(9);
		tree.root.right = new TreeNode(4);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.left.right = new TreeNode(7);
		tree.root.right.left.right.right = new TreeNode(11);
		tree.root.left.left.right.right = new TreeNode(1);
		// Test
		tree.verticalView();
	}
}

Output

  3
  2  9
  10  6  1
  4  7
  5  11




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