Traverse all possible path from root to leaves and Display all nodes

Problem:- Display all nodes from root to leaves, consider all possible paths.
Output for above Binary tree:- 12-23-11-56, 12-23-11-78, etc

public static void pathFromtRootToleafs(Node root, Vector<Integer> vector,
   int index) {
  if (root == null)
   return;
  vector.insertElementAt(root.getData(), index);
  index++;
  pathFromtRootToleafs(root.getLeftChild(), vector, index);
  pathFromtRootToleafs(root.getRightChild(), vector, index);
  if (root.getLeftChild() == null && root.getRightChild() == null) {
   
   displayNodes(vector, index);System.out.println("");
  }
 }
Here we are doing pre-order traversal and storing nodes values in a vector till we reach leaf (left and right node are null). Once we reach at leaf node, we call display method with index value to display all nodes.

Complete Sample program:- 

package com.devinline.trees;
import java.util.Vector;

public class AllPathsFromRootToLeafs {

 /**
  * To display all nodes from root to leaf - consider all possible paths
  */
 public static void main(String[] args) {
  Vector<Integer> vector = new Vector<>();
  BinaryTree bt = new BinaryTree();
  Node root = bt.createTree();
  pathFromtRootToleafs(root, vector, 0);
 }

 public static void pathFromtRootToleafs(Node root, Vector<Integer> vector,
   int index) {
  if (root == null)
   return;
  vector.insertElementAt(root.getData(), index);
  index++;
  pathFromtRootToleafs(root.getLeftChild(), vector, index);
  pathFromtRootToleafs(root.getRightChild(), vector, index);
  if (root.getLeftChild() == null && root.getRightChild() == null) {
   
   displayNodes(vector, index);System.out.println("");
  }
 }

 private static void displayNodes(Vector<Integer> vector, int index) {
  for (int i = 0; i < index; i++) {
   System.out.print(vector.get(i) + " ");
  }
 }
}
class BinaryTree {
 Node root;

 public BinaryTree() {
  root = null;
 }
 public Node createTree() {
  if (root == null) {
   root = new Node(12);
  }
  root.setLeftChild(new Node(23));
  root.setRightChild(new Node(18));
  root.getLeftChild().setLeftChild(new Node(11));
  root.getLeftChild().setRightChild(new Node(43));
  root.getRightChild().setLeftChild(new Node(12));
  root.getLeftChild().getLeftChild().setLeftChild(new Node(56));
  root.getLeftChild().getLeftChild().setRightChild(new Node(78));
  root.getRightChild().getLeftChild().setRightChild(new Node(98));
  root.getRightChild().setRightChild(new Node(99));
  return root;
 } 
}

class Node {

 private int data;
 private Node leftChild;
 private Node rightChild;

 public Node(int data) {
  this.data = data;
  leftChild = null;
  rightChild = null;
 }

 public int getData() {
  return data;
 }

 public void setData(int data) {
  this.data = data;
 }

 public Node getLeftChild() {
  return leftChild;
 }

 public void setLeftChild(Node leftChild) {
  this.leftChild = leftChild;
 }

 public Node getRightChild() {
  return rightChild;
 }

 public void setRightChild(Node rightChild) {
  this.rightChild = rightChild;
 }

}

Sample output:
-
12 23 11 56
12 23 11 78
12 23 43
12 18 12 98
12 18 99

13 Comments

  1. Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.

    Business Analytics Course in Bangalore

    ReplyDelete
  2. You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.

    Data Science Training in Bangalore

    ReplyDelete
  3. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such amazing content for all the curious readers who are very keen on being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in the future too.

    Digital Marketing Training in Bangalore

    ReplyDelete
  4. I found Habit to be a transparent site, a social hub that is a conglomerate of buyers and sellers willing to offer digital advice online at a decent cost.

    Artificial Intelligence Training in Bangalore

    ReplyDelete
  5. The Extraordinary blog went amazed by the content that they have developed in a very descriptive manner. This type of content surely ensures the participants explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

    Machine Learning Course in Bangalore

    ReplyDelete
  6. Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

    Digital Marketing Training in Bangalore

    ReplyDelete
  7. A good blog always contains new and exciting information, and reading it I feel like this blog really has all of these qualities that make it a blog.

    Artificial Intelligence Training in Bangalore

    ReplyDelete
  8. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.

    Machine Learning Course in Bangalore

    ReplyDelete
  9. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog.

    Data Science Training in Bangalore

    ReplyDelete
  10. Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

    Data Scientist Training in Bangalore

    ReplyDelete
  11. Thank you for posting this information. I just want to let you know that I just visited your site and find it very interesting and informative. I look forward to reading most of your posts.

    Data Science Course in Patna

    ReplyDelete
  12. This was lovely thanks for sharing

    ReplyDelete
Previous Post Next Post