binary search tree print child nodes

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.

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.

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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe