Skip to content

Instantly share code, notes, and snippets.

@nikhil-RGB
Last active February 21, 2024 12:44
Show Gist options
  • Save nikhil-RGB/dec418464634651c42d6a0e9931a4611 to your computer and use it in GitHub Desktop.
Save nikhil-RGB/dec418464634651c42d6a0e9931a4611 to your computer and use it in GitHub Desktop.
BFS/DFS Search in a Tree
package experiments;
import java.util.*;
class TreeNode<T>{
public ArrayList<TreeNode<T>> children;//can be empty
//NEVER PASS NULL to this parameter.
T value;
public TreeNode(T value, ArrayList<TreeNode<T>> children)
{
this.value=value;
this.children=children;
}
//Checks if a node is child-less
public boolean isTerminal()
{
if(this.children.size()==0)
{
return true;
}
return false;
}
}
class Tree<T>
{
TreeNode<T> parent;
public Tree(TreeNode<T> root)
{
this.parent=root;
}
//True for BFS, false for DFS
public boolean search(T value,boolean BFS)
{
ArrayList<TreeNode<T>> arr=new ArrayList<>(0);
arr.add(this.parent);
for(int iterations=0;arr.size()!=0;++iterations)
{
TreeNode<T> current= arr.get(0);
if(current.value.equals(value))
{
System.out.print(BFS?"(BFS)":"(DFS)");
System.out.println("Value found on iteration-number(0-index): "+iterations);
return true;
}
ArrayList<TreeNode<T>> children=current.children;
arr.remove(0);
if(children.size()==0)
{continue;}
if(BFS)
{
arr.addAll(children);
}
else
{
arr.addAll(0, children);
}
}
return false;
}
}
public class TreeFinder
{
public static void main(String[] args)
{
System.out.println("BFS/DFS Representation");
ArrayList<String> empty=new ArrayList<>(0);
TreeNode<String> D=new TreeNode("D",empty);
TreeNode<String> E=new TreeNode("E",empty);
TreeNode<String> F=new TreeNode("F",empty);
TreeNode<String> G=new TreeNode("G",empty);
ArrayList <TreeNode<String>> childrenB=new ArrayList<>(0);
ArrayList<TreeNode<String>> childrenC=new ArrayList<>(0);
childrenB.add(D);
childrenB.add(E);
childrenC.add(F);
childrenC.add(G);
TreeNode<String> B=new TreeNode("B",childrenB);
TreeNode<String> C=new TreeNode("C",childrenC);
ArrayList<TreeNode<String>> childrenA=new ArrayList<>(0);
childrenA.add(B);
childrenA.add(C);
TreeNode<String> A=new TreeNode("A",childrenA);
TreeNode<String> K=new TreeNode("K",empty);
Tree<String> tree=new Tree<>(A);
System.out.println("DFS search for D:" +tree.search(D.value, false));
System.out.println("BFS search for D:"+tree.search(D.value, true));
System.out.println();
System.out.println("DFS search for F:" +tree.search(F.value, false));
System.out.println("BFS search for F:"+tree.search(F.value, true));
System.out.println();
System.out.println("BFS search for K:"+tree.search(K.value, true));
System.out.println("DFS search for K:"+tree.search(K.value, false));
}
}
@nikhil-RGB
Copy link
Author

OUTPUT:

BFS/DFS Representation
(DFS)Value found on iteration-number(0-index): 2
DFS search for D:true
(BFS)Value found on iteration-number(0-index): 3
BFS search for D:true

(DFS)Value found on iteration-number(0-index): 5
DFS search for F:true
(BFS)Value found on iteration-number(0-index): 5
BFS search for F:true

BFS search for K:false
DFS search for K:false

Sample Tree:

           A
    B            C
D      E      F      G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment