Posted on by Kalkicode
Code Binary Tree

boundary traversal of binary tree in swift

Swift program for boundary traversal of binary tree. Here problem description and other solutions.

import Foundation
/* 
  Swift 4 program for
  Boundary Traversal of binary tree
*/
// Binary Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	func isLeaf(_ node: TreeNode? ) -> Bool
	{
		if (node  != nil && 
            node!.left == nil && 
            node!.right == nil)
		{
			return true;
		}
		return false;
	}
	// Print all leftmost node
	func printLeftMost(_ node: TreeNode? )
	{
		if (node  != nil && node!.left  != nil)
		{
			print("",node!.data, terminator: " ");
			// Recursively visit left subtree
			self.printLeftMost(node!.left);
		}
	}
	// Print rightmost node in reverse order
	func printRightMost(_ node: TreeNode? )
	{
		if (node  != nil && node!.right  != nil)
		{
			self.printRightMost(node!.right);
			print("",node!.data, terminator: " ");
		}
	}
	// Display all leaf nodes
	func printLeafNode(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			if (self.isLeaf(node))
			{
				// Display left node
				print("",node!.data, terminator: " ");
			}
			self.printLeafNode(node!.left);
			self.printLeafNode(node!.right);
		}
	}
	func boundaryView()
	{
		if (self.root == nil)
		{
			return;
		}
		// Step A
		// print root value
		print(self.root!.data, terminator: " ");
		if (self.isLeaf(self.root))
		{
			// When only single node
			return;
		}
		// Step B
		// Print leftmost nodes
		self.printLeftMost(self.root!.left);
		// Step C
		// Print all leaf nodes
		self.printLeafNode(self.root);
		// Step D
		// Print rightmost nodes
		self.printRightMost(self.root!.right);
	}
	static func main(_ args: [String])
	{
		// Create new tree
		let tree: BinaryTree? = BinaryTree();
		/*
		 Make A Binary Tree
		  -----------------------
		             1
		           /   \
		          2     3
		         /  \   / \
		        4    5 6   7
		       /    /   \    \
		      12   8     9    10
		            \
		             11
		*/
		// Add tree nodes
		tree!.root = TreeNode(1);
		tree!.root!.left = TreeNode(2);
		tree!.root!.right = TreeNode(3);
		tree!.root!.right!.right = TreeNode(7);
		tree!.root!.right!.left = TreeNode(6);
		tree!.root!.left!.left = TreeNode(4);
		tree!.root!.left!.right = TreeNode(5);
		tree!.root!.left!.right!.left = TreeNode(8);
		tree!.root!.left!.right!.left!.right = TreeNode(11);
		tree!.root!.left!.left!.left = TreeNode(12);
		tree!.root!.right!.left!.right = TreeNode(9);
		tree!.root!.right!.right!.right = TreeNode(10);
		// Display boundary view
		tree!.boundaryView();
	}
}
BinaryTree.main([String]());

Output

1  2  4  12  11  9  10  7  3

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