import java.util.LinkedList;
import java.util.Queue;
public class SearchElementInTree {
public static Boolean findElement(TreeBNode root,int n) {
if(null == root) {
return false;
}
Queue
q.offer(root);
while(!q.isEmpty()) {
TreeBNode temp = q.poll();
if(temp.data == n) {
return true;
}
if(null!=temp.rightChild) {
q.offer(temp.rightChild);
}
if(null != temp.leftChild) {
q.offer(temp.leftChild);
}
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeBNode root = new TreeBNode(10);
root.leftChild = new TreeBNode(20);
root.rightChild = new TreeBNode(30);
root.leftChild.leftChild = new TreeBNode(40);
root.leftChild.rightChild = new TreeBNode(50);
root.rightChild.leftChild = new TreeBNode(60);
root.rightChild.rightChild = new TreeBNode(70);
Boolean status = findElement(root, 70);
System.out.println(status);
}
}
Comments
Post a Comment