-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSearchBinaryTree.java
More file actions
68 lines (48 loc) · 1.6 KB
/
SearchBinaryTree.java
File metadata and controls
68 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class SearchBinaryTree {
public static class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
public Node root;
public static boolean flag = false;
public SearchBinaryTree(){
root = null;
}
public void searchNode(Node temp, int value){
if(root == null){
System.out.println("Tree is empty");
}
else{
if(temp.data == value){
flag = true;
return;
}
if(flag == false && temp.left != null){
searchNode(temp.left, value);
}
if(flag == false && temp.right != null){
searchNode(temp.right, value);
}
}
}
public static void main(String[] args) {
SearchBinaryTree bt = new SearchBinaryTree();
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);
bt.searchNode(bt.root, 5);
if(flag)
System.out.println("Element is present in the binary tree");
else
System.out.println("Element is not present in the binary tree");
}
}