-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
243 lines (182 loc) · 6.88 KB
/
BinarySearchTree.java
File metadata and controls
243 lines (182 loc) · 6.88 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*************************************************************************
* Pace University
*
* Course: CS 608 Algorithms and Computing Theory
* Author: Heli Rawal and Karishma Rao.
* Collaborators: None
* References: None
* Project: 2
* Problem: Testing the running time .
* Description: This program measures the running time taken by Skewed Binary tree and Binary Search tree to search a number not in the tree.
* Input: Tree size
* Output: The running time taken by Skewed Binary tree and Binary Search tree to search a number not in the tree.
* Visible data fields:
* none.
* Visible methods:
* public void insert( AnyType x )
* public boolean contains( AnyType x )
* Remarks
* -------
1) Skewed BST:
----------
The skewed BST takes O(n) times to search a number not in a tree
because it traverses every node in a tree to look for a number.
-> For example: There is a tree with 16 nodes, then every node will be checked
to search for a number. So,
T(n) = O(n)
2) Balanced BST:
-------------
The Balanced BST takes O(logn) times to search a number not in
a tree because either it will search for a number in left subtree
or a right subtree depending upon the condition rather than searching
every node in a tree.
-> For example: Considering tree with 16 nodes with sorted numbers it is to be
divided four times depending upon the conditions.
16 * (1/2)^4 = 1
n * (1/2)^k = 1 (n=16, k=4)
2^k n/2^k = 2^k
n=2^k
logn=k
T(n) = O(logn)
3) After Observation:
------------------
The results match with the conjecture observed above because Skewed BST increases O(n) times
and Balanced BST increases O(logn) times.
* Chart of running times observed in nanoseconds:
*
* Size | Skewed BST | Balanced BST
* ---------------------------------------------------------------
* 10 | 31394 | 11471
* ---------------------------------------------------------------
* 100 | 65204 | 17508
* ---------------------------------------------------------------
* 1000 | 407524 | 39243
* ---------------------------------------------------------------
* 10000 | 1057139 | 89957
* ---------------------------------------------------------------
*************************************************************************/
import java.util.Random;
import java.util.Scanner;
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
{
/**
* Construct the tree.
*/
public BinarySearchTree( )
{
root = null;
}
/**
* Insert into the tree; duplicates are ignored.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
root = insert( x, root );
}
public boolean contains( AnyType x )
{
return contains( x, root );
}
public boolean isEmpty( )
{
return root == null;
}
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
public BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
return new BinaryNode<AnyType>( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
private boolean contains( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
return false;
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
return contains( x, t.left );
else if( compareResult > 0 )
return contains( x, t.right );
else
return true; // Match
}
private void printTree( BinaryNode<AnyType> t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}
public static class BinaryNode<AnyType>
{
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode<AnyType> left; // Left child
BinaryNode<AnyType> right; // Right child
}
private BinaryNode<AnyType> root;
private static Scanner input;
// Test program
@SuppressWarnings("unchecked")
public static void main( String [ ] args )
{
BinarySearchTree<Integer> Ts = new BinarySearchTree<Integer>( );
BinarySearchTree<Integer> Tr = new BinarySearchTree<Integer>( );
System.out.println( "The running time taken by tree S and tree R" );
input = new Scanner(System.in);
int x = input.nextInt();
//To insert skewed BST
for(int i=1;i<=x;i++)
{
Ts.insert(i);
}
//To insert Balanced BST
for(int i=1;i<=x;i++)
{
Tr.insert((int) ((double)1000*i*Math.random()));
}
long startTime = System.nanoTime();
// Time taken by Skewed BST to insert
Ts.insert(x);
// display the time elapsed
System.out.println("The time taken by Skewed Binary to insert item is " + (System.nanoTime() - startTime) + " nanoseconds.");
// prepare to measure the time elapsed again
startTime = System.nanoTime();
// Time taken by Balanced BST to insert
Tr.insert(x); // display the time elapsed
System.out.println("The time taken by Binary Search Tree to insert item is " + (System.nanoTime() - startTime) + " nanoseconds.");
// Time taken by Skewed BST to search for a number not in the tree
startTime = System.nanoTime();
Ts.contains(x+1);
System.out.println("The time taken by Skewed Binary Search Tree to search for number not in the tree is " + (System.nanoTime() - startTime) + " nanoseconds.");
// Time taken by Balanced BST to search for a number not in the tree
startTime = System.nanoTime();
Tr.contains(x+1);
System.out.println("The time taken by Binary Search Tree to search for random number is " + (System.nanoTime() - startTime) + " nanoseconds.");
}
}