Posted on by Kalkicode
Code Binary Tree

# Reverse anti clockwise spiral traversal of a binary tree in c++ C++ program for Reverse anti clockwise spiral traversal of a binary tree. Here mentioned other language solution.

``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Reversal anti 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 = nullptr;
this->right = nullptr;
}
};
// Define Binary Tree
class BinaryTree
{
public: TreeNode *root;
BinaryTree()
{
this->root = nullptr;
}
// Find height of given binary tree
int treeHeight(TreeNode *node)
{
if (node != nullptr)
{
int l = this->treeHeight(node->left) + 1;
int r = this->treeHeight(node->right) + 1;
if (l > r)
{
return l;
}
else
{
return r;
}
}
else
{
return 0;
}
}
// Display given level from left to right
void printLeftToRight(TreeNode *node, int level)
{
if (node == nullptr)
{
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 == nullptr)
{
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
// anti clockwise spiral of binary tree
void reversalAntiClockwise()
{
if (this->root == nullptr)
{
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)
{
// 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;
}
}
}
}
};
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);
// Test
tree->reversalAntiClockwise();
return 0;
}``````

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.

Categories
Relative Post