# Boundary Traversal of Binary Number in C++

In this article, you will learn about the boundary traversal of binary numbers in C++ with their example.

A basic procedure in computer science and data structures called boundary traversal of a binary tree entails going to the outermost nodes of a binary tree in a particular order. The tree's border, which is made up of the left boundary, leaf nodes, and right boundary, is used to extract information using this traversal technique. Each level's leftmost nodes, excluding leaf nodes, are included within the left boundary. The terminal nodes of the tree are the leaf nodes, and the right boundary includes the rightmost nodes of each level, removing leaf nodes.

Binary tree: Beginning at the root, print the boundary nodes of the binary tree anticlockwise. The boundary comprises

• The left boundary (not including leaf nodes on the left)
• Leaves (Only the leaf nodes make up leaves)
• Right boundary (excluding leaf nodes from the right nodes)

The route from the root to the node that is furthest to the left is referred to as the left boundary. The route from the root to the node with the most rightward nodes is referred to as the right boundary. If the root lacks a left or right sub-tree, it is either a left boundary or a right boundary. Please take note that no sub-trees are included in this definition; it only pertains to the input binary tree. The leftmost node is defined as a leaf node that you could arrive at if, if one existed, you started at the left sub-tree every time. Go to the relevant sub-tree if not. Proceed until you reach a leaf node.

The definition of the rightmost node follows accordingly, with the left and right switching places.

For example, the following tree's border traversal is "1 2 3 4 5 6 7".

The traversal will take place in this order

1. Root: 1
2. Left-boundary nodes: 7
3. Leaf nodes: 1 2 4 5
4. Right boundary nodes: 3 7 6 3

We divide the issue into three sections:

1. Print the left boundary from top to bottom.

2. Print all leaf nodes in a single column from left to right can be separated into two parts:

• Print every leaf node of the left sub-tree starting from the left.
• From left to right, print every leaf node in the right subtree.

3. Print the right boundary from the bottom up.

The implementation is shown below based on the situations above:

### C++ Code:

`#include <iostream>#include<conio.h>struct TreeNode {    int val;    TreeNode* left;    TreeNode* right;    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}};void printLeaves(TreeNode* root) {    if (root) {        printLeaves(root->left);        if (!root->left && !root->right) {            std::cout << root->val << " ";        }        printLeaves(root->right);    }}void printLeftBoundary(TreeNode* root) {    if (root) {        if (root->left || root->right) {            std::cout << root->val << " ";        }        if (root->left) {            printLeftBoundary(root->left);        } else {            printLeftBoundary(root->right);        }    }}void printRightBoundary(TreeNode* root) {    if (root)       {        if (root->right)        {            printRightBoundary(root->right);        } else {            printRightBoundary(root->left);        }        if (root->left || root->right) {            std::cout << root->val << " ";        }    }}void printBoundaryTraversal(TreeNode* root) {    if (root) {        std::cout << root->val << " ";        printLeftBoundary(root->left);        printLeaves(root->left);        printLeaves(root->right);        printRightBoundary(root->right);        std::cout << std::endl;    }}`

int main() {

`TreeNode* root = new TreeNode(1);    root->left = new TreeNode(2);    root->right = new TreeNode(3);    root->left->left = new TreeNode(4);    root->left->right = new TreeNode(5);    root->right->left = new TreeNode(6);    root->right->right = new TreeNode(7);    std::cout << "Boundary Traversal of Binary Tree: ";    printBoundaryTraversal(root);    return 0;}`

Output:

`Boundary Traversal of binary tree is 1 2 4 5 6 7 3`

Explanation:

This output represents the left boundary (1 2 4 5), leaf nodes (4 5 6 7), and right boundary (3 7 6 3) of the binary tree in a counterclockwise order, starting from the root node (1) and moving along the boundary of the tree.

## Conclusion:

A specialized tree traversal algorithm called boundary traversal of a binary tree traverses the boundary nodes of the tree in a systematic, anticlockwise manner. Three essential phases make up this traversal:

• Going to the left boundary
• Looking through and printing the leaf nodes
• Going over the right boundary

It is a thorough technique for removing all border nodes from a binary tree while keeping the tree's hierarchical structure, including the root, left boundary nodes, leaf nodes, and right boundary nodes. By using this strategy, we ensure that all nodes of the binary tree that are located on its edge are accessed and printed in the desired order. When it's required to look into or deal with a binary tree's outermost nodes, boundary traversal is a helpful technique that can be applied to a variety of tree-related issues.