Javatpoint Logo
Javatpoint Logo

Boundary traversal of binary tree in C++

The process of visiting the boundary nodes of a binary tree in a particular order is referred to as boundary traversal. The left boundary, which does not include the left leaf nodes, leaf nodes, and the right boundary, which does not include the right leaf nodes, make up the border nodes. You may access and process the tree's external nodes with the aid of this traversal, which is helpful for a variety of applications.

  1. Left Boundary: The group of nodes from the tree's root to its leftmost node-aside from the left leaf nodes-forms the left boundary. It is possible to implement the left boundary traversal from the top down. Anytime there is a left child, you start at the root and go there. If there isn't a left child, visit the right child and keep going until you find the leftmost node.
  2. Leaf Nodes: Nodes that have no children on either the left or the right are known as leaf nodes. Utilizing an in-order traversal, you can move through these nodes. Each leaf node will be visited during an in-order traverse, which travels clockwise.
  3. Right Boundary: The set of nodes that make up the right boundary of the tree, omitting the right leaf nodes, extends from the rightmost node to the root. You can construct the traversal of the right boundary using a bottom-up method, just like you did for the left boundary. Every time a suitable youngster is present, you start at the source and go there. You visit the left child if there isn't a right child, then carry on this manner until you reach the rightmost node.

Boundary traversal can be implemented in C++ by creating distinct functions for left boundary traversal, right boundary traversal, and leaf node traversal. These three elements will be included in the final boundary traversal in the stated order.

The steps for boundary traversal are listed below in brief:

  1. The root node should be printed.
  2. Use a top-down strategy to print the left boundary (excluding left-leaf nodes).
  3. Using an in-order traverse, print the leaf nodes.
  4. Use a bottom-up strategy to print the right boundary (excluding the right leaf nodes).

Code:

Let's take an example to illustrate the boundary traversal of binary tree in C++:

Output:

1 2 3 5 6 8 10 9 7

Explanation:

1. In this example, the code starts by declaring the TreeNode structure and includes the required header file. The three properties of this structure, which stand in for a node in a binary tree, are left, which points to the node's left child, right, which points to the node's right child, and val, which stores the node's value.

Three recursive functions are defined in the code for various aspects of boundary traversal.

  1. printLeaves(TreeNode* root): The function printLeaves(TreeNode* root) is responsible for publishing the tree's leaf nodes. It moves through the tree from left to right, starting at the root, and prints a node's value if it is a leaf node (that is if it lacks both left and right children).
  2. printLeftBoundary(TreeNode* root): Except for left-leaf nodes, the left boundary of the tree can be printed using the printLeftBoundary(TreeNode* root) Starting at the root and moving to the leftmost node, it employs a top-down strategy. A node prints its left child if it has one; otherwise, the right child is taken into account. The process keeps on until it reaches the leftmost non-leaf node.
  3. printRightBoundary(TreeNode* root): Similar to printLeftBoundary, printRightBoundary prints the right boundary of the tree (excluding right leaf nodes) from the bottom up. The function name is printRightBoundary(TreeNode* root). Printing nodes along the way, start from the root and move to the rightmost non-leaf node.

2. The primary engine behind boundary traversal is the function boundaryTraversal(TreeNode* root). The root node is displayed first. After that, it calls printLeftBoundary to print the left boundary (excluding the leaf nodes on the left), printLeaves to print the leaf nodes, and printRightBoundary to print the right boundary (excluding the leaf nodes on the right).

For purposes of demonstration, a sample binary tree is built in the main function. The structure of the tree is as follows:

3. The boundary traversal is carried out by calling the boundaryTraversal function with the tree root.

The binary tree's nodes are printed in the following order by the code: root, left boundary (apart from left leaf nodes), leaf nodes, and right boundary (aside from right leaf nodes).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA