Skip to content

Commit f23945f

Browse files
author
phishman3579
committed
Major updates to all classes to add Java Collection compatibility, also unified under Interfaces to make testing cleaner
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@403 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent e7d4727 commit f23945f

22 files changed

+6120
-12460
lines changed

src/com/jwetherell/algorithms/DataStructures.java

Lines changed: 2257 additions & 11066 deletions
Large diffs are not rendered by default.

src/com/jwetherell/algorithms/data_structures/BTree.java

Lines changed: 219 additions & 89 deletions
Large diffs are not rendered by default.

src/com/jwetherell/algorithms/data_structures/BinaryHeap.java

Lines changed: 380 additions & 245 deletions
Large diffs are not rendered by default.

src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java

Lines changed: 135 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayDeque;
44
import java.util.ArrayList;
5+
import java.util.Deque;
56
import java.util.HashSet;
67
import java.util.List;
78
import java.util.Random;
@@ -20,7 +21,8 @@
2021
*
2122
* @author Justin Wetherell <phishman3579@gmail.com>
2223
*/
23-
public class BinarySearchTree<T extends Comparable<T>> {
24+
@SuppressWarnings("unchecked")
25+
public class BinarySearchTree<T extends Comparable<T>> implements ITree<T> {
2426

2527
private int modifications = 0;
2628

@@ -41,8 +43,7 @@ public enum DepthFirstSearchOrder {
4143
/**
4244
* Default constructor.
4345
*/
44-
public BinarySearchTree() {
45-
}
46+
public BinarySearchTree() { }
4647

4748
/**
4849
* Constructor with external Node creator.
@@ -52,12 +53,9 @@ public BinarySearchTree(INodeCreator<T> creator) {
5253
}
5354

5455
/**
55-
* Add value to the tree. Tree can contain multiple equal values.
56-
*
57-
* @param value
58-
* T to add to the tree.
59-
* @return True if successfully added to tree.
56+
* {@inheritDoc}
6057
*/
58+
@Override
6159
public boolean add(T value) {
6260
Node<T> nodeAdded = this.addValue(value);
6361
return (nodeAdded != null);
@@ -116,14 +114,11 @@ protected Node<T> addValue(T value) {
116114
}
117115

118116
/**
119-
* Does the tree contain the value.
120-
*
121-
* @param value
122-
* T to locate in the tree.
123-
* @return True if tree contains value.
117+
* {@inheritDoc}
124118
*/
119+
@Override
125120
public boolean contains(T value) {
126-
Node<T> node = getNode(value);
121+
Node<T> node = getNode((T)value);
127122
return (node != null);
128123
}
129124

@@ -284,15 +279,12 @@ protected Node<T> getLeast(Node<T> startingNode) {
284279
}
285280

286281
/**
287-
* Remove first occurrence of value in the tree.
288-
*
289-
* @param value
290-
* T to remove from the tree.
291-
* @return True if value was removed from the tree.
282+
* {@inheritDoc}
292283
*/
293-
public boolean remove(T value) {
284+
@Override
285+
public T remove(T value) {
294286
Node<T> nodeToRemove = this.removeValue(value);
295-
return (nodeToRemove != null);
287+
return ((nodeToRemove!=null)?nodeToRemove.id:null);
296288
}
297289

298290
/**
@@ -304,11 +296,21 @@ public boolean remove(T value) {
304296
*/
305297
protected Node<T> removeValue(T value) {
306298
Node<T> nodeToRemoved = this.getNode(value);
299+
if (nodeToRemoved != null) removeNode(nodeToRemoved);
300+
return nodeToRemoved;
301+
}
302+
303+
/**
304+
* Remove the node using a replacement
305+
*
306+
* @param nodeToRemoved
307+
* Node<T> to remove from the tree.
308+
*/
309+
protected void removeNode(Node<T> nodeToRemoved) {
307310
if (nodeToRemoved != null) {
308311
Node<T> replacementNode = this.getReplacementNode(nodeToRemoved);
309312
replaceNodeWithNode(nodeToRemoved, replacementNode);
310313
}
311-
return nodeToRemoved;
312314
}
313315

314316
/**
@@ -413,22 +415,19 @@ protected void replaceNodeWithNode(Node<T> nodeToRemoved, Node<T> replacementNod
413415
}
414416

415417
/**
416-
* Get number of nodes in the tree.
417-
*
418-
* @return Number of nodes in the tree.
418+
* {@inheritDoc}
419419
*/
420+
@Override
420421
public int size() {
421422
return size;
422423
}
423424

424425
/**
425-
* Validate the tree for all Binary Search Tree invariants.
426-
*
427-
* @return True if tree is valid.
426+
* {@inheritDoc}
428427
*/
428+
@Override
429429
public boolean validate() {
430-
if (root == null)
431-
return true;
430+
if (root == null) return true;
432431
return validateNode(root);
433432
}
434433

@@ -466,7 +465,6 @@ protected boolean validateNode(Node<T> node) {
466465
*
467466
* @return breath first search sorted array representing the tree.
468467
*/
469-
@SuppressWarnings("unchecked")
470468
public T[] getBFS() {
471469
Queue<Node<T>> queue = new ArrayDeque<Node<T>>();
472470
T[] values = (T[]) new Comparable[size];
@@ -500,7 +498,6 @@ public T[] getLevelOrder() {
500498
*
501499
* @return in-order sorted array representing the tree.
502500
*/
503-
@SuppressWarnings("unchecked")
504501
public T[] getDFS(DepthFirstSearchOrder order) {
505502
Set<Node<T>> added = new HashSet<Node<T>>(2);
506503
T[] nodes = (T[]) new Comparable[size];
@@ -582,6 +579,14 @@ public T[] getSorted() {
582579
return getDFS(DepthFirstSearchOrder.inOrder);
583580
}
584581

582+
/**
583+
* {@inheritDoc}
584+
*/
585+
@Override
586+
public java.util.Collection<T> toCollection() {
587+
return (new JavaCompatibleBinarySearchTree<T>(this));
588+
}
589+
585590
/**
586591
* {@inheritDoc}
587592
*/
@@ -674,4 +679,102 @@ private static <T extends Comparable<T>> String getString(Node<T> node, String p
674679
return builder.toString();
675680
}
676681
}
682+
683+
private static class JavaCompatibleBinarySearchTree<T extends Comparable<T>> extends java.util.AbstractCollection<T> {
684+
685+
protected BinarySearchTree<T> tree = null;
686+
687+
public JavaCompatibleBinarySearchTree(BinarySearchTree<T> tree) {
688+
this.tree = tree;
689+
}
690+
691+
/**
692+
* {@inheritDoc}
693+
*/
694+
@Override
695+
public boolean add(T value) {
696+
return tree.add(value);
697+
}
698+
699+
/**
700+
* {@inheritDoc}
701+
*/
702+
@Override
703+
public boolean remove(Object value) {
704+
return (tree.remove((T)value)!=null);
705+
}
706+
707+
/**
708+
* {@inheritDoc}
709+
*/
710+
@Override
711+
public boolean contains(Object value) {
712+
return tree.contains((T)value);
713+
}
714+
715+
/**
716+
* {@inheritDoc}
717+
*/
718+
@Override
719+
public int size() {
720+
return tree.size();
721+
}
722+
723+
/**
724+
* {@inheritDoc}
725+
*/
726+
@Override
727+
public java.util.Iterator<T> iterator() {
728+
return (new BinarySearchTreeIterator<T>(this.tree));
729+
}
730+
731+
private static class BinarySearchTreeIterator<C extends Comparable<C>> implements java.util.Iterator<C> {
732+
733+
private BinarySearchTree<C> tree = null;
734+
private BinarySearchTree.Node<C> last = null;
735+
private Deque<BinarySearchTree.Node<C>> toVisit = new ArrayDeque<BinarySearchTree.Node<C>>();
736+
737+
protected BinarySearchTreeIterator(BinarySearchTree<C> tree) {
738+
this.tree = tree;
739+
if (tree.root!=null) toVisit.add(tree.root);
740+
}
741+
742+
/**
743+
* {@inheritDoc}
744+
*/
745+
@Override
746+
public boolean hasNext() {
747+
if (toVisit.size()>0) return true;
748+
return false;
749+
}
750+
751+
/**
752+
* {@inheritDoc}
753+
*/
754+
@Override
755+
public C next() {
756+
while (toVisit.size()>0) {
757+
// Go thru the current nodes
758+
BinarySearchTree.Node<C> n = toVisit.pop();
759+
760+
// Add non-null children
761+
if (n.lesser!=null) toVisit.add(n.lesser);
762+
if (n.greater!=null) toVisit.add(n.greater);
763+
764+
// Update last node (used in remove method)
765+
last = n;
766+
return n.id;
767+
}
768+
return null;
769+
}
770+
771+
/**
772+
* {@inheritDoc}
773+
*/
774+
@Override
775+
public void remove() {
776+
tree.removeNode(last);
777+
}
778+
}
779+
}
677780
}

0 commit comments

Comments
 (0)