Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

BOUNDARY

TRAVERSAL
Boundary Traversal Method

EXPLANATION

Boundary traversal refers to the process of visiting and processing the

nodes of a tree or a graph that lie on the boundary or perimeter of the

structure. In the context of binary trees, which are a common type of tree

data structure, boundary traversal typically involves visiting the nodes in

a specific order: the left boundary, the leaf nodes, and the right boundary.
Boundary Traversal Method

Steps involved in boundary traversal of a binary tree:

1. Left Boundary Traversal


2. Leaf Node Traversal
3. Right Boundary Traversal

The order of these steps ensures that each node is visited exactly once, and
the traversal covers the entire boundary of the binary tree
Boundary Traversal Method

Example: 1

/ \

2 3

/ \ / \

4 5 6 7

Boundary Traversal:

Left Boundary: 1 → 2 → 4 Leaf Nodes: 4 → 5 → 6 → 7


Right Boundary: 7 → 3 → 1

Complete Order: 1 → 2 → 4 → 5 → 6 → 7 → 3
Boundary Traversal Method

Example: 10
/ \
2 13
\ / \
8 12 15
/ \
4 9
Boundary Traversal:

Left Boundary: 10 → 2 → 8 → 4 Leaf Nodes: 4 → 9 → 12 → 15


Right Boundary: 15 → 13 → 10

Complete Order: 10 → 2 → 8 → 4 → 9 → 12 → 15 → 13
Boundary Traversal Method

Example: 8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13

Boundary Traversal:

Left Boundary: 8 → 3 → 1 → 6 → 4 Leaf Nodes: 4 → 7 → 13 → 14


Right Boundary: 14 → 10 → 8

Complete Order: 8 → 3 → 1 → 6 → 4 → 7 → 13 → 14 → 10
Boundary Traversal Method

Algorithm:

1. Input:

The root of the binary tree.

2. Output:

Print the boundary nodes of the binary tree in an anticlockwise

direction.
Boundary Traversal Method

Algorithm:

3. Procedure:

If the root is not null, do the following:

1. Print the value of the root node.

2. Call `printLeftBoundary` with the left child of the root, excluding the

leftmost leaf.

3. Call `printLeaves` with the root's left and right children to print the

leaves in a lefttoright order.

4. Call `printRightBoundary` with the right child of the root, excluding the

rightmost leaf.
Boundary Traversal Method

Algorithm:

4. printLeftBoundary(node):

If the node is not null and is not a leaf:

1. Print the value of the node.

2. Recursively call `printLeftBoundary` with the left child of the node.

3. If the left child is null, recursively call `printLeftBoundary` with the

right child of the node.


Boundary Traversal Method

Algorithm:

5. printRightBoundary(node):

If the node is not null and is not a leaf:

1. If the right child is null, recursively call `printRightBoundary` with the

left child of the node.

2. Recursively call `printRightBoundary` with the right child of the node.

3. Print the value of the node.


Boundary Traversal Method

Algorithm:

6. printLeaves(node):

If the node is not null:

1. Recursively call `printLeaves` with the left child of the node.

2. If the node is a leaf, print its value.

3. Recursively call `printLeaves` with the right child of the node.


Boundary Traversal Method

Pseudocode:

procedure boundaryTraversal(root):

if root is not null:

print root.val

printLeftBoundary(root.left)

printLeaves(root.left)

printLeaves(root.right)

printRightBoundary(root.right)
Boundary Traversal Method

Pseudocode:

procedure printLeftBoundary(node):

if node is not null and not isLeaf(node):

print node.val

printLeftBoundary(node.left)

if node.left is null:

printLeftBoundary(node.right)
Boundary Traversal Method

Pseudocode:

procedure printRightBoundary(node):

if node is not null and not isLeaf(node):

if node.right is null:

printRightBoundary(node.left)

printRightBoundary(node.right)

print node.val
Boundary Traversal Method

Pseudocode:

procedure printLeaves(node):

if node is not null:

printLeaves(node.left)

if isLeaf(node):

print node.val

printLeaves(node.right)

function isLeaf(node):

return node is not null and node.left is null and node.right is null
Boundary Traversal Method

class TreeNode { if (root != null) {


int val; System.out.print(root.val + " ");
TreeNode left, right; printLeaves(root.left);
printLeaves(root.right);
public TreeNode(int value) { }
val = value; }
left = right = null;
} public static void main(String[] args) {
} // Constructing the example tree
TreeNode root = new TreeNode(8);
public class BinaryTreeBoundaryTraversal { root.left = new TreeNode(3);
root.right = new TreeNode(10);
private static void printLeaves(TreeNode root) { root.left.left = new TreeNode(1);
if (root != null) { root.left.right = new TreeNode(6);
printLeaves(root.left); root.left.right.left = new TreeNode(4);
if (root.left == null && root.right == root.left.right.right = new TreeNode(7);
null) System.out.print(root.val + " "); root.right.right = new TreeNode(14);
printLeaves(root.right); root.right.right.left = new TreeNode(13);
}
} // Calling the boundaryTraversal function
public static void boundaryTraversal(TreeNode root) { boundaryTraversal(root);
}
}
Boundary Traversal

EXPLANATION

 This Java code defines a binary tree node class and a program to perform

boundary traversal of a binary tree.

 The boundaryTraversal method prints the root, left leaves, and right leaves,

while the printLeaves method prints the leaf nodes.

 The main function constructs an example tree and calls the boundary

traversal method.
Boundary Traversal Method

TIME AND SPACE COMPLEXITY

Time Complexity:

O(n) where n is the number of nodes.

Space Complexity:

O(h) where h is the height of the tree (O(n) in the worst case,

O(log n) in a balanced tree).


INTERVIEW QUESTION

1. Explain the process of binary tree boundary traversal.

Answer: Binary tree boundary traversal involves three main steps. First,

traverse and print the left boundary of the tree (excluding the leftmost

leaf). Second, print all leaf nodes from left to right. Lastly, traverse

and print the right boundary of the tree (excluding the rightmost leaf).
INTERVIEW QUESTION

2. What is the time complexity of binary tree boundary traversal?

Answer:

The time complexity of binary tree boundary traversal is O(n), where n is

the number of nodes in the tree. This is because each node is visited

exactly once during the traversal, resulting in a linear time complexity.


INTERVIEW QUESTION

3. How would you modify the boundary traversal algorithm to include only
distinct values in the output?

Answer: To ensure only distinct values are printed, we can use a set or

hash table to keep track of the values encountered during traversal.

Before printing a value, we check whether it has been seen before. If

not, we print it and add it to the set.


INTERVIEW QUESTION

4. Can you implement the binary tree boundary traversal iteratively?

Answer: Yes, an iterative solution can be implemented using a queue or

stack data structure. The idea is to simulate the recursive process by

maintaining a collection of nodes to be processed. We start with the root

and iteratively enqueue or push the nodes related to the left and right

boundaries and leaf nodes.

You might also like