Reverse Clockwise spiral traversal of a binary tree

Clockwise spiral traversal from bottom of binary tree

Here given code implementation process.

// C program for 
// Reverse Clockwise spiral traversal of a binary tree
#include <stdio.h>

#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *new_tree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// This is creates and returns the new binary tree node
struct TreeNode *get_node(int data)
{
	// Create dynamic node
	struct TreeNode *new_node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL;
		new_node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return new_node;
}
// Find height of given binary tree
int treeHeight(struct TreeNode *root)
{
	if (root != NULL)
	{
		int l = treeHeight(root->left);
		int r = treeHeight(root->right);
		if (l > r)
		{
			return l + 1;
		}
		else
		{
			return r + 1;
		}
	}
	else
	{
		return 0;
	}
}
// Display given level from left to right
void printLeftToRight(struct TreeNode *node, int level)
{
	if (node == NULL)
	{
		return;
	}
	if (level == 1)
	{
		// print level node
		printf(" %d", node->data);
	}
	else
	{
		// First visit left child
		printLeftToRight(node->left, level - 1);
		printLeftToRight(node->right, level - 1);
	}
}
// Display given level from right to left
void printRightToLeft(struct TreeNode *node, int level)
{
	if (node == NULL)
	{
		return;
	}
	if (level == 1)
	{
		// print level node
		printf(" %d", node->data);
	}
	else
	{
		// First visit right child
		printRightToLeft(node->right, level - 1);
		printRightToLeft(node->left, level - 1);
	}
}
// Handles the request of printing reversal clockwise spiral of binary tree
void reverseClockWiseSpiral(struct TreeNode *node)
{
	if (node == NULL)
	{
		printf("\n Empty Tree \n");
	}
	else
	{
		// Start level
		int top = 1;
		// Last level
		int down = treeHeight(node);
		// This is level indicator (top to down selector)
		int selection = 1;
		// When the top level less than or equal to down level
		while (top <= down)
		{
			if (selection == 0)
			{
				// left to right 
				printLeftToRight(node, top);
				// Increase level
				top++;
				// Next is bottom level
				selection = 1;
			}
			else
			{
				// right to left
				printRightToLeft(node, down);
				// Decrease the level
				down--;
				// Next is top level
				selection = 0;
			}
		}
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = new_tree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1

	    -----------------
	    Binary tree

	*/
	tree->root = get_node(6);
	tree->root->left = get_node(4);
	tree->root->left->right = get_node(3);
	tree->root->left->right->left = get_node(10);
	tree->root->left->right->left->left = get_node(9);
	tree->root->left->right->right = get_node(8);
	tree->root->left->right->right->right = get_node(-1);
	tree->root->left->left = get_node(2);
	tree->root->right = get_node(7);
	tree->root->right->right = get_node(12);
	tree->root->right->right->left = get_node(5);
	reverseClockWiseSpiral(tree->root);
	return 0;
}

input

 -1 9 6 5 8 10 4 7 12 3 2
// Java program for
// Reverse Clockwise spiral traversal of a binary tree

// Binary Tree node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree 
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Find height of given binary tree
	public int treeHeight(TreeNode root)
	{
		if (root != null)
		{
			int l = treeHeight(root.left);
			int r = treeHeight(root.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	public void printLeftToRight(TreeNode node, int level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			System.out.print("  " + node.data);
		}
		else
		{
			// First visit left child
			printLeftToRight(node.left, level - 1);
			printLeftToRight(node.right, level - 1);
		}
	}
	// Display given level from right to left
	public void printRightToLeft(TreeNode node, int level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			System.out.print("  " + node.data);
		}
		else
		{
			// First visit right child
			printRightToLeft(node.right, level - 1);
			printRightToLeft(node.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	public void reverseClockWiseSpiral()
	{
		if (this.root == null)
		{
			System.out.println("\n Empty Tree ");
		}
		else
		{
			// Start level
			int top = 1;
			// Last level
			int down = treeHeight(this.root);
			// This is level indicator (top to down selector)
			boolean selection = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right 
					printLeftToRight(this.root, top);
					// Increase level
					top++;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					printRightToLeft(this.root, down);
					// Decrease the level
					down--;
					// Next is top level
					selection = false;
				}
			}
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		         6                            
		       /   \    
		      4     7    
		     / \     \               
		    2   3     12
		       / \    / 
		      10  8  5
		      /    \
		     9     -1

		    -----------------
		    Binary tree

		*/
		tree.root = new TreeNode(6);
		tree.root.left = new TreeNode(4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.right.right.right = new TreeNode(-1);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.reverseClockWiseSpiral();
	}
}

input

  -1  9  6  5  8  10  4  7  12  3  2
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Reverse Clockwise spiral traversal of a binary tree

// Binary Tree node
class TreeNode
{
	public: 
    int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
// Define Binary Tree
class BinaryTree
{
	public: 
    TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Find height of given binary tree
	int treeHeight(TreeNode *root)
	{
		if (root != NULL)
		{
			int l = this->treeHeight(root->left);
			int r = this->treeHeight(root->right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	void printLeftToRight(TreeNode *node, int level)
	{
		if (node == NULL)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			cout << "  " << node->data;
		}
		else
		{
			// First visit left child
			this->printLeftToRight(node->left, level - 1);
			this->printLeftToRight(node->right, level - 1);
		}
	}
	// Display given level from right to left
	void printRightToLeft(TreeNode *node, int level)
	{
		if (node == NULL)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			cout << "  " << node->data;
		}
		else
		{
			// First visit right child
			this->printRightToLeft(node->right, level - 1);
			this->printRightToLeft(node->left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	void reverseClockWiseSpiral()
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree " << endl;
		}
		else
		{
			// Start level
			int top = 1;
			// Last level
			int down = this->treeHeight(this->root);
			// This is level indicator (top to down selector)
			bool selection = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					this->printLeftToRight(this->root, top);
					// Increase level
					top++;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					this->printRightToLeft(this->root, down);
					// Decrease the level
					down--;
					// Next is top level
					selection = false;
				}
			}
		}
	}
};
int main()
{
	BinaryTree *tree = new BinaryTree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1
	    -----------------
	    Binary tree
	*/
	tree->root = new TreeNode(6);
	tree->root->left = new TreeNode(4);
	tree->root->left->right = new TreeNode(3);
	tree->root->left->right->left = new TreeNode(10);
	tree->root->left->right->left->left = new TreeNode(9);
	tree->root->left->right->right = new TreeNode(8);
	tree->root->left->right->right->right = new TreeNode(-1);
	tree->root->left->left = new TreeNode(2);
	tree->root->right = new TreeNode(7);
	tree->root->right->right = new TreeNode(12);
	tree->root->right->right->left = new TreeNode(5);
	tree->reverseClockWiseSpiral();
	return 0;
}

input

  -1  9  6  5  8  10  4  7  12  3  2
// Include namespace system
using System;
// Csharp program for
// Reverse Clockwise spiral traversal of a binary tree

// Binary Tree node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Find height of given binary tree
	public int treeHeight(TreeNode root)
	{
		if (root != null)
		{
			int l = this.treeHeight(root.left);
			int r = this.treeHeight(root.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	public void printLeftToRight(TreeNode node, int level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			Console.Write("  " + node.data);
		}
		else
		{
			// First visit left child
			this.printLeftToRight(node.left, level - 1);
			this.printLeftToRight(node.right, level - 1);
		}
	}
	// Display given level from right to left
	public void printRightToLeft(TreeNode node, int level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			Console.Write("  " + node.data);
		}
		else
		{
			// First visit right child
			this.printRightToLeft(node.right, level - 1);
			this.printRightToLeft(node.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	public void reverseClockWiseSpiral()
	{
		if (this.root == null)
		{
			Console.WriteLine("\n Empty Tree ");
		}
		else
		{
			// Start level
			int top = 1;
			// Last level
			int down = this.treeHeight(this.root);
			// This is level indicator (top to down selector)
			Boolean selection = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					this.printLeftToRight(this.root, top);
					// Increase level
					top++;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					this.printRightToLeft(this.root, down);
					// Decrease the level
					down--;
					// Next is top level
					selection = false;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		         6                            
		       /   \    
		      4     7    
		     / \     \               
		    2   3     12
		       / \    / 
		      10  8  5
		      /    \
		     9     -1
		    -----------------
		    Binary tree
		*/
		tree.root = new TreeNode(6);
		tree.root.left = new TreeNode(4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.right.right.right = new TreeNode(-1);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.reverseClockWiseSpiral();
	}
}

input

  -1  9  6  5  8  10  4  7  12  3  2
<?php
// Php program for
// Reverse Clockwise spiral traversal of a binary tree

// 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;
	}
}
// Define Binary Tree
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	// Find height of given binary tree
	public	function treeHeight($root)
	{
		if ($root != NULL)
		{
			$l = $this->treeHeight($root->left);
			$r = $this->treeHeight($root->right);
			if ($l > $r)
			{
				return $l + 1;
			}
			else
			{
				return $r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	public	function printLeftToRight($node, $level)
	{
		if ($node == NULL)
		{
			return;
		}
		if ($level == 1)
		{
			// print level node
			echo("  ".$node->data);
		}
		else
		{
			// First visit left child
			$this->printLeftToRight($node->left, $level - 1);
			$this->printLeftToRight($node->right, $level - 1);
		}
	}
	// Display given level from right to left
	public	function printRightToLeft($node, $level)
	{
		if ($node == NULL)
		{
			return;
		}
		if ($level == 1)
		{
			// print level node
			echo("  ".$node->data);
		}
		else
		{
			// First visit right child
			$this->printRightToLeft($node->right, $level - 1);
			$this->printRightToLeft($node->left, $level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	public	function reverseClockWiseSpiral()
	{
		if ($this->root == NULL)
		{
			echo("\n Empty Tree ".
				"\n");
		}
		else
		{
			// Start level
			$top = 1;
			// Last level
			$down = $this->treeHeight($this->root);
			// This is level indicator (top to down selector)
			$selection = true;
			// When the top level less than or equal to down level
			while ($top <= $down)
			{
				if ($selection == false)
				{
					// left to right
					$this->printLeftToRight($this->root, $top);
					// Increase level
					$top++;
					// Next is bottom level
					$selection = true;
				}
				else
				{
					// right to left
					$this->printRightToLeft($this->root, $down);
					// Decrease the level
					$down--;
					// Next is top level
					$selection = false;
				}
			}
		}
	}
}

function main()
{
	$tree = new BinaryTree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1
	    -----------------
	    Binary tree
	*/
	$tree->root = new TreeNode(6);
	$tree->root->left = new TreeNode(4);
	$tree->root->left->right = new TreeNode(3);
	$tree->root->left->right->left = new TreeNode(10);
	$tree->root->left->right->left->left = new TreeNode(9);
	$tree->root->left->right->right = new TreeNode(8);
	$tree->root->left->right->right->right = new TreeNode(-1);
	$tree->root->left->left = new TreeNode(2);
	$tree->root->right = new TreeNode(7);
	$tree->root->right->right = new TreeNode(12);
	$tree->root->right->right->left = new TreeNode(5);
	$tree->reverseClockWiseSpiral();
}
main();

input

  -1  9  6  5  8  10  4  7  12  3  2
// Node JS program for
// Reverse Clockwise spiral traversal of a binary tree

// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Find height of given binary tree
	treeHeight(root)
	{
		if (root != null)
		{
			var l = this.treeHeight(root.left);
			var r = this.treeHeight(root.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	printLeftToRight(node, level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			process.stdout.write("  " + node.data);
		}
		else
		{
			// First visit left child
			this.printLeftToRight(node.left, level - 1);
			this.printLeftToRight(node.right, level - 1);
		}
	}
	// Display given level from right to left
	printRightToLeft(node, level)
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			process.stdout.write("  " + node.data);
		}
		else
		{
			// First visit right child
			this.printRightToLeft(node.right, level - 1);
			this.printRightToLeft(node.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	reverseClockWiseSpiral()
	{
		if (this.root == null)
		{
			console.log("\n Empty Tree ");
		}
		else
		{
			// Start level
			var top = 1;
			// Last level
			var down = this.treeHeight(this.root);
			// This is level indicator (top to down selector)
			var selection = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					this.printLeftToRight(this.root, top);
					// Increase level
					top++;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					this.printRightToLeft(this.root, down);
					// Decrease the level
					down--;
					// Next is top level
					selection = false;
				}
			}
		}
	}
}

function main()
{
	var tree = new BinaryTree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1
	    -----------------
	    Binary tree
	*/
	tree.root = new TreeNode(6);
	tree.root.left = new TreeNode(4);
	tree.root.left.right = new TreeNode(3);
	tree.root.left.right.left = new TreeNode(10);
	tree.root.left.right.left.left = new TreeNode(9);
	tree.root.left.right.right = new TreeNode(8);
	tree.root.left.right.right.right = new TreeNode(-1);
	tree.root.left.left = new TreeNode(2);
	tree.root.right = new TreeNode(7);
	tree.root.right.right = new TreeNode(12);
	tree.root.right.right.left = new TreeNode(5);
	tree.reverseClockWiseSpiral();
}
main();

input

  -1  9  6  5  8  10  4  7  12  3  2
#  Python 3 program for
#  Reverse Clockwise spiral traversal of a binary tree

#  Binary Tree node
class TreeNode :
	def __init__(self, data) :
		#  Set node value
		self.data = data
		self.left = None
		self.right = None
	

#  Define Binary Tree
class BinaryTree :
	def __init__(self) :
		self.root = None
	
	#  Find height of given binary tree
	def treeHeight(self, root) :
		if (root != None) :
			l = self.treeHeight(root.left)
			r = self.treeHeight(root.right)
			if (l > r) :
				return l + 1
			else :
				return r + 1
			
		else :
			return 0
		
	
	#  Display given level from left to right
	def printLeftToRight(self, node, level) :
		if (node == None) :
			return
		
		if (level == 1) :
			#  print level node
			print(" ", node.data, end = "")
		else :
			#  First visit left child
			self.printLeftToRight(node.left, level - 1)
			self.printLeftToRight(node.right, level - 1)
		
	
	#  Display given level from right to left
	def printRightToLeft(self, node, level) :
		if (node == None) :
			return
		
		if (level == 1) :
			#  print level node
			print(" ", node.data, end = "")
		else :
			#  First visit right child
			self.printRightToLeft(node.right, level - 1)
			self.printRightToLeft(node.left, level - 1)
		
	
	#  Handles the request of printing reverse Clockwise spiral of binary tree
	def reverseClockWiseSpiral(self) :
		if (self.root == None) :
			print("\n Empty Tree ")
		else :
			top = 1
			down = self.treeHeight(self.root)
			selection = True
			#  When the top level less than or equal to down level
			while (top <= down) :
				if (selection == False) :
					#  left to right
					self.printLeftToRight(self.root, top)
					#  Increase level
					top += 1
					#  Next is bottom level
					selection = True
				else :
					#  right to left
					self.printRightToLeft(self.root, down)
					#  Decrease the level
					down -= 1
					#  Next is top level
					selection = False
				
			
		
	

def main() :
	tree = BinaryTree()
	#         6                            
	#       /   \    
	#      4     7    
	#     / \     \               
	#    2   3     12
	#       / \    / 
	#      10  8  5
	#      /    \
	#     9     -1
	#    -----------------
	#    Binary tree
	tree.root = TreeNode(6)
	tree.root.left = TreeNode(4)
	tree.root.left.right = TreeNode(3)
	tree.root.left.right.left = TreeNode(10)
	tree.root.left.right.left.left = TreeNode(9)
	tree.root.left.right.right = TreeNode(8)
	tree.root.left.right.right.right = TreeNode(-1)
	tree.root.left.left = TreeNode(2)
	tree.root.right = TreeNode(7)
	tree.root.right.right = TreeNode(12)
	tree.root.right.right.left = TreeNode(5)
	tree.reverseClockWiseSpiral()

if __name__ == "__main__": main()

input

  -1  9  6  5  8  10  4  7  12  3  2
#  Ruby program for
#  Reverse Clockwise spiral traversal of a binary tree

#  Binary Tree node
class TreeNode 
	# Define the accessor and reader of class TreeNode
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(data) 
		#  Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end

end

#  Define Binary Tree
class BinaryTree 
	# Define the accessor and reader of class BinaryTree
	attr_reader :root
	attr_accessor :root
	def initialize() 
		self.root = nil
	end

	#  Find height of given binary tree
	def treeHeight(root) 
		if (root != nil) 
			l = self.treeHeight(root.left)
			r = self.treeHeight(root.right)
			if (l > r) 
				return l + 1
			else 
				return r + 1
			end

		else 
			return 0
		end

	end

	#  Display given level from left to right
	def printLeftToRight(node, level) 
		if (node == nil) 
			return
		end

		if (level == 1) 
			#  print level node
			print("  ", node.data)
		else 
			#  First visit left child
			self.printLeftToRight(node.left, level - 1)
			self.printLeftToRight(node.right, level - 1)
		end

	end

	#  Display given level from right to left
	def printRightToLeft(node, level) 
		if (node == nil) 
			return
		end

		if (level == 1) 
			#  print level node
			print("  ", node.data)
		else 
			#  First visit right child
			self.printRightToLeft(node.right, level - 1)
			self.printRightToLeft(node.left, level - 1)
		end

	end

	#  Handles the request of printing reverse Clockwise spiral of binary tree
	def reverseClockWiseSpiral() 
		if (self.root == nil) 
			print("\n Empty Tree ", "\n")
		else 
			#  Start level
			top = 1
			#  Last level
			down = self.treeHeight(self.root)
			#  This is level indicator (top to down selector)
			selection = true
			#  When the top level less than or equal to down level
			while (top <= down) 
				if (selection == false) 
					#  left to right
					self.printLeftToRight(self.root, top)
					#  Increase level
					top += 1
					#  Next is bottom level
					selection = true
				else 
					#  right to left
					self.printRightToLeft(self.root, down)
					#  Decrease the level
					down -= 1
					#  Next is top level
					selection = false
				end

			end

		end

	end

end

def main() 
	tree = BinaryTree.new()
	#         6                            
	#       /   \    
	#      4     7    
	#     / \     \               
	#    2   3     12
	#       / \    / 
	#      10  8  5
	#      /    \
	#     9     -1
	#    -----------------
	#    Binary tree
	tree.root = TreeNode.new(6)
	tree.root.left = TreeNode.new(4)
	tree.root.left.right = TreeNode.new(3)
	tree.root.left.right.left = TreeNode.new(10)
	tree.root.left.right.left.left = TreeNode.new(9)
	tree.root.left.right.right = TreeNode.new(8)
	tree.root.left.right.right.right = TreeNode.new(-1)
	tree.root.left.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(7)
	tree.root.right.right = TreeNode.new(12)
	tree.root.right.right.left = TreeNode.new(5)
	tree.reverseClockWiseSpiral()
end

main()

input

  -1  9  6  5  8  10  4  7  12  3  2
// Scala program for
// Reverse Clockwise spiral traversal of a 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);
	}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Find height of given binary tree
	def treeHeight(root: TreeNode): Int = {
		if (root != null)
		{
			var l: Int = treeHeight(root.left);
			var r: Int = treeHeight(root.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	def printLeftToRight(node: TreeNode, level: Int): Unit = {
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print("  " + node.data);
		}
		else
		{
			// First visit left child
			printLeftToRight(node.left, level - 1);
			printLeftToRight(node.right, level - 1);
		}
	}
	// Display given level from right to left
	def printRightToLeft(node: TreeNode, level: Int): Unit = {
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print("  " + node.data);
		}
		else
		{
			// First visit right child
			printRightToLeft(node.right, level - 1);
			printRightToLeft(node.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	def reverseClockWiseSpiral(): Unit = {
		if (this.root == null)
		{
			println("\n Empty Tree ");
		}
		else
		{
			// Start level
			var top: Int = 1;
			// Last level
			var down: Int = treeHeight(this.root);
			// This is level indicator (top to down selector)
			var selection: Boolean = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					printLeftToRight(this.root, top);
					// Increase level
					top += 1;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					printRightToLeft(this.root, down);
					// Decrease the level
					down -= 1;
					// Next is top level
					selection = false;
				}
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree: BinaryTree = new BinaryTree();
		/*
		         6                            
		       /   \    
		      4     7    
		     / \     \               
		    2   3     12
		       / \    / 
		      10  8  5
		      /    \
		     9     -1
		    -----------------
		    Binary tree
		*/
		tree.root = new TreeNode(6);
		tree.root.left = new TreeNode(4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.right.right.right = new TreeNode(-1);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.reverseClockWiseSpiral();
	}
}

input

  -1  9  6  5  8  10  4  7  12  3  2
// Swift 4 program for
// Reverse Clockwise spiral traversal of a 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;
	}
}
// Define Binary Tree
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Find height of given binary tree
	func treeHeight(_ root: TreeNode? )->Int
	{
		if (root  != nil)
		{
			let l: Int = self.treeHeight(root!.left);
			let r: Int = self.treeHeight(root!.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	func printLeftToRight(_ node: TreeNode? , _ level : Int)
	{
		if (node == nil)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print("  ", node!.data, terminator: "");
		}
		else
		{
			// First visit left child
			self.printLeftToRight(node!.left, level - 1);
			self.printLeftToRight(node!.right, level - 1);
		}
	}
	// Display given level from right to left
	func printRightToLeft(_ node: TreeNode? , _ level : Int)
	{
		if (node == nil)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print(" ", node!.data, terminator: "");
		}
		else
		{
			// First visit right child
			self.printRightToLeft(node!.right, level - 1);
			self.printRightToLeft(node!.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	func reverseClockWiseSpiral()
	{
		if (self.root == nil)
		{
			print("\n Empty Tree ");
		}
		else
		{
			// Start level
			var top: Int = 1;
			// Last level
			var down: Int = self.treeHeight(self.root);
			// This is level indicator (top to down selector)
			var selection: Bool = true;
			// When the top level less than or equal to down level
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					self.printLeftToRight(self.root, top);
					// Increase level
					top += 1;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					self.printRightToLeft(self.root, down);
					// Decrease the level
					down -= 1;
					// Next is top level
					selection = false;
				}
			}
		}
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1
	    -----------------
	    Binary tree
	*/
	tree.root = TreeNode(6);
	tree.root!.left = TreeNode(4);
	tree.root!.left!.right = TreeNode(3);
	tree.root!.left!.right!.left = TreeNode(10);
	tree.root!.left!.right!.left!.left = TreeNode(9);
	tree.root!.left!.right!.right = TreeNode(8);
	tree.root!.left!.right!.right!.right = TreeNode(-1);
	tree.root!.left!.left = TreeNode(2);
	tree.root!.right = TreeNode(7);
	tree.root!.right!.right = TreeNode(12);
	tree.root!.right!.right!.left = TreeNode(5);
	tree.reverseClockWiseSpiral();
}
main();

input

  -1  9   6  5  8  10   4   7  12  3  2
// Kotlin program for
// Reverse Clockwise spiral traversal of a binary tree

// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Find height of given binary tree
	fun treeHeight(root: TreeNode ? ): Int
	{
		if (root != null)
		{
			val l: Int = this.treeHeight(root.left);
			val r: Int = this.treeHeight(root.right);
			if (l > r)
			{
				return l + 1;
			}
			else
			{
				return r + 1;
			}
		}
		else
		{
			return 0;
		}
	}
	// Display given level from left to right
	fun printLeftToRight(node: TreeNode ? , level : Int): Unit
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print("  " + node.data);
		}
		else
		{
			// First visit left child
			this.printLeftToRight(node.left, level - 1);
			this.printLeftToRight(node.right, level - 1);
		}
	}
	// Display given level from right to left
	fun printRightToLeft(node: TreeNode ? , level : Int): Unit
	{
		if (node == null)
		{
			return;
		}
		if (level == 1)
		{
			// print level node
			print("  " + node.data);
		}
		else
		{
			// First visit right child
			this.printRightToLeft(node.right, level - 1);
			this.printRightToLeft(node.left, level - 1);
		}
	}
	// Handles the request of printing reverse Clockwise spiral of binary tree
	fun reverseClockWiseSpiral(): Unit
	{
		if (this.root == null)
		{
			println("\n Empty Tree ");
		}
		else
		{
			// Start level
			var top: Int = 1;
			// Last level
			var down: Int = this.treeHeight(this.root);
			// This is level indicator (top to down selector)
			var selection: Boolean = true;
			while (top <= down)
			{
				if (selection == false)
				{
					// left to right
					this.printLeftToRight(this.root, top);
					// Increase level
					top += 1;
					// Next is bottom level
					selection = true;
				}
				else
				{
					// right to left
					this.printRightToLeft(this.root, down);
					// Decrease the level
					down -= 1;
					// Next is top level
					selection = false;
				}
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val tree: BinaryTree = BinaryTree();
	/*
	         6                            
	       /   \    
	      4     7    
	     / \     \               
	    2   3     12
	       / \    / 
	      10  8  5
	      /    \
	     9     -1
	    -----------------
	    Binary tree
	*/
	tree.root = TreeNode(6);
	tree.root?.left = TreeNode(4);
	tree.root?.left?.right = TreeNode(3);
	tree.root?.left?.right?.left = TreeNode(10);
	tree.root?.left?.right?.left?.left = TreeNode(9);
	tree.root?.left?.right?.right = TreeNode(8);
	tree.root?.left?.right?.right?.right = TreeNode(-1);
	tree.root?.left?.left = TreeNode(2);
	tree.root?.right = TreeNode(7);
	tree.root?.right?.right = TreeNode(12);
	tree.root?.right?.right?.left = TreeNode(5);
	tree.reverseClockWiseSpiral();
}

input

  -1  9  6  5  8  10  4  7  12  3  2


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







© 2021, kalkicode.com, All rights reserved