# Reverse anti clockwise spiral traversal of a binary tree in typescript

Ts program for Reverse anti clockwise spiral traversal of a binary tree. Here mentioned other language solution.

``````/*
TypeScript program for
Reversal anti clockwise spiral
Traversal of a binary tree
*/
// Binary Tree node
class TreeNode
{
public data: number;
public left: TreeNode;
public right: TreeNode;
constructor(data: number)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
public root: TreeNode;
constructor()
{
this.root = null;
}
// Find height of given binary tree
public number treeHeight(node: TreeNode)
{
if (node != null)
{
var l = this.treeHeight(node.left) + 1;
var r = this.treeHeight(node.right) + 1;
if (l > r)
{
return l;
}
else
{
return r;
}
}
else
{
return 0;
}
}
// Display given level from left to right
public printLeftToRight(node: TreeNode, level: number)
{
if (node == null)
{
return;
}
if (level == 1)
{
// print level node
console.log("  " + 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 printRightToLeft(node: TreeNode, level: number)
{
if (node == null)
{
return;
}
if (level == 1)
{
// print level node
console.log("  " + 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
// anti clockwise spiral of binary tree
public reversalAntiClockwise()
{
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)
{
// right to left
this.printRightToLeft(this.root, top);
// Increase level
top++;
// Next is bottom level
selection = true;
}
else
{
// left to right
this.printLeftToRight(this.root, down);
// Decrease the level
down--;
// Next is top level
selection = false;
}
}
}
}
public static 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);
// Test
tree.reversalAntiClockwise();
}
}
BinaryTree.main();
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/``````

Output

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

## 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.