forked from dblandin/Binary-Search-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
executable file
·187 lines (172 loc) · 5.23 KB
/
BinarySearchTree.java
File metadata and controls
executable file
·187 lines (172 loc) · 5.23 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
/*
* Devon Blandin
* Assignment 3 :: CSC383 (SSII)
* August 4th, 2012
*/
public class BinarySearchTree<T extends Comparable>
{
private TreeNode overallRoot;
// Constructs an empty binary tree
public BinarySearchTree()
{
overallRoot = null;
}
// Constructs a binary tree with the given node as its root.
public BinarySearchTree(TreeNode overallRoot)
{
this.overallRoot = overallRoot;
}
//returns the element with the largest value
public T getMax()
{
return getMax(overallRoot);
}
private T getMax(TreeNode root)
{
if (root == null)
return null;
else if (root.right == null)
return (T)root.data;
else
return getMax(root.right);
}
//returns the element with the smallest value
public T getMin()
{
return getMin(overallRoot);
}
private T getMin(TreeNode root)
{
if (root == null)
return null;
else if (root.left == null)
return (T)root.data;
else
return getMin(root.left);
}
// returns the depth of the tree
public int getDepth()
{
return getDepth(overallRoot);
}
private int getDepth(TreeNode root)
{
int depth = 0;
if (root != null)
{
depth = 1;
depth += Math.max(getDepth(root.left), getDepth(root.right));
}
return depth;
}
// returns the number of leaves in the tree
public int getNumLeaves()
{
return getNumLeaves(overallRoot);
}
private int getNumLeaves(TreeNode root)
{
int numLeaves = 0;
if (root != null)
{
if (root.left == null && root.right == null)
numLeaves++;
else
{
if (root.left != null)
numLeaves += getNumLeaves(root.left);
if (root.right != null)
numLeaves += getNumLeaves(root.right);
}
}
return numLeaves;
}
// returns a String of the elements “inOrder”
public String toString()
{
return toString(overallRoot);
}
public String toString(TreeNode root)
{
StringBuffer s = new StringBuffer();
if (root != null) {
s.append(toString(root.left)); // append my left sub-tree
s.append(root.data + " "); // append myself
s.append(toString(root.right)); // append my right sub-tree
}
return s.toString();
}
// prints tree sideways
public void printSideways()
{
printSideways(overallRoot, 0);
}
public void printSideways(TreeNode root, int indent)
{
if (root != null)
{
String indentS = "";
for (int i = 0; i < indent; i++)
indentS += " ";
indent++;
printSideways(root.right, indent);
System.out.println(indentS + "(" + root.data + ")");
printSideways(root.left, indent);
}
}
// Prints all elements of this tree in left to right order.
public void print()
{
print(overallRoot);
System.out.println();
}
// Prints a portion of the overall tree
private void print(TreeNode root)
{
// implicit base case: if null, do nothing
if (root != null)
{
print(root.left); // print my left sub-tree
System.out.print(root.data + " "); // print myself
print(root.right); // print my right sub-tree
}
}
// Returns true if the overall tree contains the given target value,
// false otherwise
public boolean contains(T target)
{
return contains(overallRoot, target);
}
// Returns true if a portion of the overall tree contains the given
// target value, false otherwise.
private boolean contains(TreeNode root, T target)
{
if (root == null)
return false;
else if (root.data == target)
return true;
else if (target.compareTo(root.data) < 0)
return contains(root.left, target);
else
return contains(root.right, target);
}
// Adds the value to the tree such that sorted BST order is maintained
public void add(T value)
{
overallRoot = add(overallRoot, value);
}
// Adds the value to the given subtree. Does not add duplicates.
// A node's initial state is passed in and its modified
// state is returned. This is the x = change(x) pattern and
// it allows attaching new nodes to the tree.
private TreeNode add(TreeNode root, T value)
{
if (root == null)
root = new TreeNode(value);
else if (value.compareTo(root.data) < 0)
root.left = add(root.left, value);
else if (value.compareTo(root.data) > 0)
root.right = add(root.right, value);
return root;
}
} // end BinarySearchTree