I am trying to trace the path of a node in a binary tree (not a binary search tree). Given a node, I am trying to print the values of the path from the root.
I have written the following program.
package dsa.tree;
import java.util.Stack;
public class TracePath {
private Node n1;
public static void main(String args[]){
TracePath nodeFinder = new TracePath();
nodeFinder.find();
}
public void find(){
Tree t = getSampleTree();
tracePath(t,n1);
}
private Tree getSampleTree() {
Tree bsTree = new BinarySearchTree();
int randomData[] = {43,887,11,3,8,33,6,0,46,32,78,76,334,45};
for(int i=0;i<randomData.length;i++){
bsTree.add(randomData[i]);
}
n1 = bsTree.search(76);
return bsTree;
}
public void tracePath(Tree t, Node node){
trace(t,node);
}
Stack<Node> mainStack = new Stack<Node>();
public void trace(Tree t, Node node){
trace(t.getRoot(),node);
}
private void trace(Node parent, Node node){
mainStack.push(parent);
if(node.data == parent.data){
for(Node iNode:mainStack){
System.out.println(iNode.data);
}
return;
}
if(parent.left != null){
trace(parent.left, node);
}
if(parent.right!=null){
trace(parent.right, node);
}
mainStack.pop();
}
}
I am getting the output properly. But its kind of messy. If you see the method trace(Node, Node), I am printing the values which I should not do. I want the trace method to properly complete. At least, I should kill the recursive structure at the stage i encounter the if condition.
Please advise.
Okay, you need to kill the recursion once you find your node. Simple enough. Change your trace method to return a boolean telling us if the node was found. That way, we go right back up the tree immediately after finding the node.
private boolean trace(Node parent, Node node){
mainStack.push(parent);
if(node.data == parent.data){
for(Node iNode:mainStack){
System.out.println(iNode.data);
}
return true;
}
if(parent.left != null){
if (trace(parent.left, node)) return true;
}
if(parent.right!=null){
if (trace(parent.right, node)) return true;
}
mainStack.pop();
return false;
}
I assume this is homework, so I will give some pointers instead of some code.
Edit: If the path node to root is wanted, a simple boolean return suffices:
private boolean trace(Node parent, Node node) {
boolean found = (node.data == parent.data)
if (!found && parent.left != null) {
found = trace(parent.left, node);
}
if (!found && parent.right != null){
found = trace(parent.right, node);
}
if (found) {
System.out.println(parent.data);
}
return found;
}
If you need the path from root to node, you can pass a List to receive the nodes in order:
private boolean trace(Node parent, Node node, List path) {
boolean found = (node.data == parent.data)
if (!found && parent.left != null) {
found = trace(parent.left, node);
}
if (!found && parent.right != null){
found = trace(parent.right, node);
}
if (found) {
path.add(0, parent);
}
return found;
}
alternatively you can build the path on your way back and return as a list.
private List trace(Node parent, Node node) {
List path = null;
if (null != node) {
if (node.data == parent.data) {
path = new ArrayList();
} else {
path = trace(parent.left, node);
if (null == path) {
path = trace(parent.right, node);
}
}
if (null != path) {
path.add(0, parent);
}
}
return path;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With