Open For Ad

Open for Ad

Latest Posts

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

19 comments:

  1. Great article with valuable information found very resourceful and enjoyed reading it waiting for next blog updated thanks for sharing.
    typeerror nonetype object is not subscriptable

    ReplyDelete
  2. 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
  3. Actually I read it yesterday but I had some ideas about it and today I wanted to read it again because it is so well written.

    Data Analytics Course in Bangalore

    ReplyDelete
  4. I have to search sites with relevant information ,This is a
    wonderful blog,These type of blog keeps the users interest in
    the website, i am impressed. thank you.
    Data Science Training in Bangalore

    ReplyDelete
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. Good blog and absolutely exceptional. You can do a lot better, but I still say it's perfect. Keep doing your best.

    Data Analytics Course in Nagpur

    ReplyDelete
  15. I have read your article, it is very informative and useful to me, I admire the valuable information you offer in your articles. Thanks for posting it ...

    Business Analytics Course in Patna

    ReplyDelete
  16. 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
  17. Good blog and absolutely exceptional. You can do a lot better, but I still say it's perfect. Keep doing your best.

    Data Science Training in Durgapur

    ReplyDelete
  18. Learn to build powerful models to solve business problems by generating useful insights and discover the various scientific processes and methods used to transform the information available in huge datasets into meaningful results. master all the tools and techniques in Data Science and gain domain-specific knowledge which will help you to add more value to your profile. Sign up for the Data Science course in Bangalore with Placements and multiple your chances of working across all industries and job functions.

    Data Science Institute in Bangalore

    ReplyDelete