Skip to content
the poor coder

binary search tree print child nodes

Poor Man
Published
Reading time
2 min read
On this page

Printing Child Nodes in Binary Search Tree#

Printing child nodes of a binary search tree can be done in multiple ways. A binary search tree is a tree data structure where each node has at most two child nodes, and the left child node is always smaller than the parent node, while the right child node is always larger. To print child nodes, we need to traverse the tree and look for the child nodes of each parent node.

Method 1: Inorder Traversal#

Inorder traversal is a common traversal method used in binary search trees. In this method, we first traverse the left subtree, then the root node, and then the right subtree. To print the child nodes using inorder traversal, we can modify the traversal function to print the left and right child nodes of each parent node along with the root node.

Text
function inorder(node) {
  if (node) {
    inorder(node.left);
    console.log(node.value);
    if (node.left) {
      console.log(node.left.value);
    }
    if (node.right) {
      console.log(node.right.value);
    }
    inorder(node.right);
  }
}

In the above code, we first traverse the left subtree recursively using the inorder function. Then we print the root node's value using console.log. Finally, we check if the root node has a left or right child node and print their values using console.log. Then we recursively traverse the right subtree using the inorder function.

Method 2: Breadth-First Traversal#

Breadth-first traversal is another traversal method used in trees. In this method, we traverse the tree level by level, starting from the root node. To print the child nodes using breadth-first traversal, we can modify the traversal function to print the child nodes of each parent node along with the root node.

Text
function breadthFirst(node) {
  var queue = [node];
  while (queue.length) {
    var current = queue.shift();
    console.log(current.value);
    if (current.left) {
      console.log(current.left.value);
    }
    if (current.right) {
      console.log(current.right.value);
    }
    if (current.left) {
      queue.push(current.left);
    }
    if (current.right) {
      queue.push(current.right);
    }
  }
}

In the above code, we initialize a queue with the root node and then loop through the queue until it is empty. In each iteration, we dequeue the first node from the queue and print its value using console.log. Then we check if the node has left and right child nodes and print their values using console.log. Finally, we enqueue the left and right child nodes of the current node to the queue if they exist.

Conclusion#

Printing child nodes in a binary search tree requires traversing the tree and looking for the child nodes of each parent node. We can use various traversal methods, such as inorder traversal or breadth-first traversal, to print the child nodes. Both methods can be modified to print the child nodes of each parent node along with the root node.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC