Open In App

Boundary Traversal of Binary Tree using JavaScript

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Boundary Traversal of the Binary Tree can be done by traversing the left, right, and leaf parts of the Binary Tree. We will break the Boundary Traversal of Binary Tree using JavaScript in three parts. First, we will traverse the left part, right part, and leaf parts of the Binary Tree and print all of them in the same order. This way we can traverse boundary elements of binary tree.

Example:

binary-tree

Boundary Traversal of Binary Tree

Recursive Approach

In recursive approach we traverse the boundary element of binary tree using depth-first search (recursion). Depth-first search is implemented using recursive approach. We will first traverse the left part of binary tree , then we will traverse leaf nodes of binary tree and in last we will traverse right part of binary tree. This way we can traverse the boundary elements of binary tree.

Example: The below code uses recursive approach to traverse the binary tree in JavaScript.

Javascript




class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
function buildTree(str) {
    if (str.length === 0 ||
        str[0] === 'N')
        return null;
    const ip = str.split(' ');
    const root =
        new Node(parseInt(ip[0]));
    const queue = [];
    queue.push(root);
    let i = 1;
    while (queue.length > 0 && i < ip.length)
    {
        const currNode = queue.shift();
        let currVal = ip[i];
        if (currVal !== 'N') {
            currNode.left =
                new Node(parseInt(currVal));
            queue.push(currNode.left);
        }
        i++;
        if (i >= ip.length) break;
        currVal = ip[i];
        if (currVal !== 'N') {
            currNode.right =
                new Node(parseInt(currVal));
            queue.push(currNode.right);
        }
        i++;
    }
    return root;
}
 
class Solution {
    traverseLeft(root, ans) {
        if (!root ||
            (!root.left && !root.right))
            return;
        ans.push(root.data);
        if (root.left)
            this.traverseLeft(root.left, ans);
        else
            this.traverseLeft(root.right, ans);
    }
 
    traverseLeaf(root, ans) {
        if (!root) return;
        if (!root.left && !root.right) {
            ans.push(root.data);
            return;
        }
        this.traverseLeaf(root.left, ans);
        this.traverseLeaf(root.right, ans);
    }
 
    traverseRight(root, ans) {
        if (!root ||
            (!root.left && !root.right))
            return;
        if (root.right)
            this.traverseRight(root.right, ans);
        else
            this.traverseRight(root.left, ans);
        ans.push(root.data);
    }
 
    boundary(root) {
        const ans = [];
        if (!root) return ans;
        ans.push(root.data);
        this.traverseLeft(root.left, ans);
        this.traverseLeaf(root.left, ans);
        this.traverseLeaf(root.right, ans);
        this.traverseRight(root.right, ans);
        return ans;
    }
}
 
const solution = new Solution();
let input = "20 8 22 4 12 N N N N 10 14";
let root = buildTree(input);
console.log(solution.boundary(root));


Output

[ 20, 8, 4, 10, 14, 22 ]

Iterative Approach

In iterative approach , we will traverse the left part and right part using loop and the leaf nodes can be traversed by using breadth-first search. Breadth first search can be implemented by using queue data structure. In this approach again we will traverse the left part of binary tree, then leaf nodes and in last we will traverse the right part of binary tree.

Example: The below code traverse the boundaries of a binary tree using the iterative method.

Javascript




class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
function boundaryTraversal(root) {
    if (root) {
        if (!root.left && !root.right) {
            console.log(root.data);
            return;
        }
        let list = [];
        list.push(root);
        let L = root.left;
        while (L.left) {
            list.push(L);
            L = L.left;
        }
        let queue = [];
        queue.push(root);
        while (queue.length) {
            let temp = queue.shift();
            if (!temp.left && !temp.right) {
                list.push(temp);
            }
            if (temp.left) {
                queue.push(temp.left);
            }
            if (temp.right) {
                queue.push(temp.right);
            }
        }
        let list_r = [];
        let R = root.right;
        while (R.right) {
            list_r.push(R);
            R = R.right;
        }
        list_r.reverse();
        list.push(...list_r);
        for (let i of list) {
            console.log(i.data + " ");
        }
        console.log();
        return;
    }
}
 
function main() {
    let root = new Node(20);
    root.left = new Node(8);
    root.right = new Node(22);
    root.left.left = new Node(4);
    root.left.right = new Node(12);
    root.right.left = new Node(10);
    root.right.right = new Node(25);
    boundaryTraversal(root);
}
 
main();


Output

20 
8 
4 
12 
10 
25 
22 




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads