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
Sample output:-
12 23 11 56
12 23 11 78
12 23 43
12 18 12 98
12 18 99
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
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.
ReplyDeleteBusiness Analytics Course in Bangalore
Đặt vé tại phòng vé Aivivu, tham khảo
ReplyDeletegiá vé máy bay đi Mỹ tháng nào rẻ nhất
đã có chuyến bay từ mỹ về việt nam chưa
vé máy bay pleiku đi sài gòn
bay đi hà nội
giá vé máy bay đi Huế
You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.
ReplyDeleteData Science Training in Bangalore
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.
ReplyDeleteDigital Marketing Training in Bangalore
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.
ReplyDeleteArtificial Intelligence Training in Bangalore
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.
ReplyDeleteMachine Learning Course in Bangalore
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.
ReplyDeleteDigital Marketing Training in Bangalore
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.
ReplyDeleteArtificial Intelligence Training in Bangalore
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.
ReplyDeleteMachine Learning Course in Bangalore
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.
ReplyDeleteData Science Training in Bangalore
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.
ReplyDeleteData Scientist Training in Bangalore
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.
ReplyDeleteData Science Course in Patna
This was lovely thanks for sharing
ReplyDelete