-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
141 lines (125 loc) · 4.09 KB
/
BinarySearchTree.java
File metadata and controls
141 lines (125 loc) · 4.09 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
import java.lang.ArrayStoreException;
/**
* The BinarySearchTree class implements an binary search tree(BST)
*
* This version of a BST is a simple implementation for the purpose of fast binary search
* No logic is added to insure a balanced tree and there is no way to rearrange/delete the tree
*
* All objects add to the tree must Extend Comparable and be unique, any 2 objects that compare equal
* cannot both exist in the tree. This may require you to implement some sort of hashing to the compareTo.
*
* @author James Kozlowski
* @version March 30, 2017
*/
public class BinarySearchTree<T extends Comparable<T>>
{
/**
* object stored in this tree node.
*/
private T value;
/**
* Left side child tree
*/
private BinarySearchTree<T> left;
/**
* Right side child tree
*/
private BinarySearchTree<T> right;
/**
* This is the constructor that creats the binary tree root
* @param valueIn the object to store in this tree node
* @return Nothing.
*/
public BinarySearchTree(T valueIn)
{
value = valueIn;
left = null;
right = null;
}
/**
* Returns the object stored in this node
* @return the Object stored in this node.
*/
public T getValue()
{
return value;
}
/**
* Adds a object to the correct place in the tree
*
* No logic is added to insure a balanced tree
* Any 2 objects that compare equal cannot both exist in the tree.
* This may require you to implement some sort of hashing to the compareTo.
*
* @param valueIn the value to store in this tree node
* @return Nothing.
* @throws ArrayStoreException if the object is allready in the tree
*/
public void add(T valueIn) throws ArrayStoreException
{
//if the object is less then this
if (valueIn.compareTo(value) < 0)
{
//if there is no left add this object to the left child
if (left == null)
{
BinarySearchTree<T> newTree = new BinarySearchTree<T>(valueIn);
left = newTree;
}
//otherwise keep going left
else
left.add(valueIn);
}
//if the object is greater then this
else if (valueIn.compareTo(value) > 0)
{
//if there is no right add this object to the right child
if (right == null)
{
BinarySearchTree<T> newTree = new BinarySearchTree<T>(valueIn);
right = newTree;
}
//otherwise keep going right
else
right.add(valueIn);
}
//if the object is not less then or greater then this, it must be the same
//this is not supported so throw an exception
else
throw new ArrayStoreException("Object {" + valueIn + "} is allready in the Binary Search Tree");
}
/**
* Finds the given object in the Binary Tree
* @param valueIn the object to find
* @return the object if found, or null if the object was not found
*/
public T find(T valueToFind)
{
//if this is the object we are looking for return the value
if (value.compareTo(valueToFind) == 0)
return value;
//if the value we are looking for is less then this go left
else if (value.compareTo(valueToFind) > 0 && left != null)
return left.find(valueToFind);
//if the value we are looking for is larger then this go right
else if (value.compareTo(valueToFind) < 0 && right != null)
return right.find(valueToFind);
//otherwise the object must not be in the list
else
return null;
}
/**
* Overrides the toString method, returns the tree as a "in order" list of objects
* @return a string of the tree.
*/
public String toString()
{
String s = "";
if (left != null)
s = s + left;
s = s + value + "\n";
if (right != null)
s = s + right;
return s;
}
}