Aug 7, 2016

Textual description of firstImageUrl

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

Location: Hyderabad, Telangana, India

9 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