-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathBinarySearchTreeExample.java
128 lines (108 loc) · 2.55 KB
/
BinarySearchTreeExample.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.*;
class Node{
int data;
Node left;
Node right;
public Node(int d){
data = d;
left = null;
right = null;
}
}
class BST{
Node head;
public BST(int d){
head = new Node(d);
}
public void addNode(Node h,Node n){
if(n.data < h.data){
if(h.left == null){
h.left = n;
return;
}
else
addNode(h.left,n);
}
else{
if(h.right == null){
h.right = n;
return;
}
else
addNode(h.right,n);
}
}
public void search(int d){
boolean found = false;
Node ptr = head; //Creating a pointer node, which will help us to find the element, initializing it from the head(searching drom the head)
while(ptr!=null){
if(ptr.data == d){
System.out.println("Yes, the element is present in the tree!");
found = true;
break;
}
else if(d > ptr.data)
ptr = ptr.right;
else
ptr = ptr.left;
}
if(!found)
System.out.println("Element not present!");
}
public void inorder(Node n) { //InOrder Tree Traversal
if(n!=null)
{
inorder(n.left);
System.out.print(n.data+"\t");
inorder(n.right);
}
}
public void preorder(Node n) { //PreOrder Tree Traversal
if(n!=null)
{
System.out.print(n.data+"\t");
preorder(n.left);
preorder(n.right);
}
}
public void postorder(Node n) { //PostOrder Tree Traversal
if(n!=null)
{
postorder(n.left);
postorder(n.right);
System.out.print(n.data+"\t");
}
}
}
public class BinarySearchTreeExample{
public static void main(String[] args){
int[] arr = {5,2,7,4,1,9}; //Example array
BST bst = new BST(arr[0]); //Setting the first element as the head of the binary search tree
for(int i=1;i<6;i++){
Node nodeToBeAdded = new Node(arr[i]);
bst.addNode(bst.head , nodeToBeAdded); // The elements of the array are added one by one
}
bst.search(4); //Let's search the Node with the value 4 in the bst we have created
bst.search(3);//Searching 3
bst.search(9);//Searching 9
bst.search(6);//Searching 6
System.out.println("\nInOrder Traversal");
bst.inorder(bst.head); //InOrder Traversal (Left-Root-Right)
System.out.println("\nPreOrder Traversal");
bst.preorder(bst.head);//PreOrder Traversal (Root-Left-Right)
System.out.println("\nPostOrder Traversal");
bst.postorder(bst.head);//PostOrder Traversal (Left-Right-Root)
/*Output:
Yes, the element is present in the tree!
Element not present!
Yes, the element is present in the tree!
Element not present!
InOrder Traversal
1 2 4 5 7 9
PreOrder Traversal
5 2 1 4 7 9
PostOrder Traversal
1 4 2 9 7 5
*/
}
}