-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
202 lines (161 loc) · 4.82 KB
/
BST.java
File metadata and controls
202 lines (161 loc) · 4.82 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
/*
* OpenDSA Project Distributed under the MIT License
*
* Copyright (c) 2011-2016 - Ville Karavirta and Cliff Shaffer
*
* @author Maricel Vicente
*/
// Unfinished Binary Search Tree implementation
class BST<E extends Comparable<E>> {
protected BSTNode<E> root; // Root of the BST
protected int nodecount; // Number of nodes in the BST
// constructor
BST() {
root = null;
nodecount = 0;
}
// Reinitialize tree
public void clear() {
root = null;
nodecount = 0;
}
// Insert a record into the tree.
// Records can be anything, but they must be Comparable
// e: The record to insert.
public void insert(E e) {
root = inserthelp(root, e);
nodecount++;
}
private BSTNode<E> inserthelp(BSTNode<E> rt, E e) {
if (rt == null)
return new BSTNode<E>(e);
if (rt.element().compareTo(e) >= 0)
rt.setLeft(inserthelp(rt.left(), e));
else
rt.setRight(inserthelp(rt.right(), e));
return rt;
}
// Return the record with key value k, null if none exists
// key: The key value to find
public E find(E key) {
return findhelp(root, key);
}
private E findhelp(BSTNode<E> rt, E key) {
if (rt == null)
return null;
if (rt.element().compareTo(key) > 0)
return findhelp(rt.left(), key);
if (rt.element().compareTo(key) == 0)
return rt.element();
else
return findhelp(rt.right(), key);
}
// Return the number of records in the dictionary
public int size() {
return nodecount;
}
// UNFINISHED METHODS BELOW THIS POINT**************************
/**
* Recursively calculate the number of nodes in this BST.
*/
public int recursiveSize() {
// You will need a recursive helper method!
return sizeHelper(root);
}
public int sizeHelper(BSTNode<E> node) {
// if node is 0, return null
if (node == null) {
return 0;
}
// return recursive: 1 + left node + right node
return 1 + sizeHelper(node.left()) + sizeHelper(node.right());
}
/**
* Return the smallest element in the tree.
*/
public E minElement() {
return minHelper(root);
}
public E minHelper(BSTNode<E> node) {
// base case
if (node == null) {
return null;
}
// check if the left node is null, then return the smallest element
if (node.left() == null) {
return node.element();
}
// return recursive call to check the left node until it is null
return minHelper(node.left());
}
/**
* Return the largest element in the tree.
*/
public E maxElement() {
return maxHelper(root);
}
public E maxHelper(BSTNode<E> node) {
// base case
if (node == null) {
return null;
}
// check if the node is null, then return the largest element
if (node.right() == null) {
return node.element();
}
// return recursive call to check right node until it is null
return maxHelper(node.right());
}
/**
* Return an ordered linked list containing all of the elements from the tree.
*/
public LList<E> makeOrderedList() {
LList<E> newList = new LList<E>();
// helper function
orderedListHelper(root,newList);
//return the new list
return newList;
}
public void orderedListHelper(BSTNode<E> node, LList<E> list){
if(node != null)
{
// add nodes from left subtree into the list
orderedListHelper(node.left(), list);
// need to add element in list by appending
list.append(node.element());
// add nodes from right subtree into the list
orderedListHelper(node.right(), list);
// don't need to append element at the end because the if statement checks then adds
}
}
/**
* Return true if this BST actually has the BST property and false if it does
* not.
*
* (Note that this method would typically not be necessary. It a correctly
* coded BST there will be no possibility of violating the BST property.)
*
*/
public boolean isValidBST() {
return isValidHelper(root);
}
public boolean isValidHelper(BSTNode<E> node) {
// base case, if the node is true
if (node == null || node.isLeaf()) {
return true;
}
// For false BST,
// check if maximum element on left subtreee is larger than the current element
// CANNOT compare maxHelper(node.left()) < node.element() so use compareTo
if (node.left() != null && maxHelper(node.left()).compareTo(node.element()) > 0) {
return false;
}
// For false BST,
// check if minimum element on right subtree is smaller than current element
if (node.right() != null && minHelper(node.right()).compareTo(node.element()) < 0) {
return false;
}
// recursive check the left and right subtree
return isValidHelper(node.left()) && isValidHelper(node.right());
}
}