public class SearchANodeUsingRecusrtion {
public static void main(String args[]) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left = new Node(6);
node.right.right = new Node(7);
SearchANodeUsingRecusrtion searchANodeUsingRecusrtion = new SearchANodeUsingRecusrtion();
int output = searchANodeUsingRecusrtion.searchNode(node,6);
if(output==1)
System.out.println("found");
else
System.out.println("not found");
}
public int searchNode(Node node,int key) {
if(null!=node) {
if(node.value==key) {
return 1;
}else {
int temp = searchNode(node.left,key);
if(temp!=0) {
return 1;
}else {
temp = searchNode(node.right,key);
return temp;
}
}
}else {
return 0;
}
}
}
class BinayTree{
static class Node{
String value;
Node left;
Node right;
Node(String val){
value = val;
left = right = null;
}
}
}
public static void main(String args[]) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left = new Node(6);
node.right.right = new Node(7);
SearchANodeUsingRecusrtion searchANodeUsingRecusrtion = new SearchANodeUsingRecusrtion();
int output = searchANodeUsingRecusrtion.searchNode(node,6);
if(output==1)
System.out.println("found");
else
System.out.println("not found");
}
public int searchNode(Node node,int key) {
if(null!=node) {
if(node.value==key) {
return 1;
}else {
int temp = searchNode(node.left,key);
if(temp!=0) {
return 1;
}else {
temp = searchNode(node.right,key);
return temp;
}
}
}else {
return 0;
}
}
}
class BinayTree{
static class Node{
String value;
Node left;
Node right;
Node(String val){
value = val;
left = right = null;
}
}
}
No comments:
Post a Comment