Skip to content

Commit 1affe34

Browse files
author
Justin Wetherell
committed
Refactored BST and List
1 parent e7ea462 commit 1affe34

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

+32-5
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,23 @@ protected boolean validateNode(Node<T> node) {
462462
*
463463
* @return breath first search sorted array representing the tree.
464464
*/
465-
public T[] getBFS() {
465+
public T[] getBFS() {
466+
return getBFS(this.root, this.size);
467+
}
468+
469+
/**
470+
* Get an array representation of the tree in breath first search order.
471+
*
472+
* @param start rooted node
473+
* @param size of tree rooted at start
474+
*
475+
* @return breath first search sorted array representing the tree.
476+
*/
477+
public static <T extends Comparable<T>> T[] getBFS(Node<T> start, int size) {
466478
Queue<Node<T>> queue = new ArrayDeque<Node<T>>();
467479
T[] values = (T[]) new Comparable[size];
468480
int count = 0;
469-
Node<T> node = root;
481+
Node<T> node = start;
470482
while (node != null) {
471483
values[count++] = node.id;
472484
if (node.lesser != null)
@@ -493,13 +505,28 @@ public T[] getLevelOrder() {
493505
/**
494506
* Get an array representation of the tree in-order.
495507
*
496-
* @return in-order sorted array representing the tree.
508+
* @param order of search
509+
*
510+
* @return order sorted array representing the tree.
497511
*/
498512
public T[] getDFS(DepthFirstSearchOrder order) {
513+
return getDFS(order, this.root, this.size);
514+
}
515+
516+
/**
517+
* Get an array representation of the tree in-order.
518+
*
519+
* @param order of search
520+
* @param start rooted node
521+
* @param size of tree rooted at start
522+
*
523+
* @return order sorted array representing the tree.
524+
*/
525+
public static <T extends Comparable<T>> T[] getDFS(DepthFirstSearchOrder order, Node<T> start, int size) {
499526
Set<Node<T>> added = new HashSet<Node<T>>(2);
500527
T[] nodes = (T[]) new Comparable[size];
501528
int index = 0;
502-
Node<T> node = root;
529+
Node<T> node = start;
503530
while (index < size && node != null) {
504531
Node<T> parent = node.parent;
505532
Node<T> lesser = (node.lesser != null && !added.contains(node.lesser)) ? node.lesser : null;
@@ -617,7 +644,7 @@ protected Node(Node<T> parent, T id) {
617644
*/
618645
@Override
619646
public String toString() {
620-
return "id =" + id + " parent=" + ((parent != null) ? parent.id : "NULL") + " lesser="
647+
return "id=" + id + " parent=" + ((parent != null) ? parent.id : "NULL") + " lesser="
621648
+ ((lesser != null) ? lesser.id : "NULL") + " greater=" + ((greater != null) ? greater.id : "NULL");
622649
}
623650
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.jwetherell.algorithms.data_structures.interfaces.IList;
66

77
@SuppressWarnings("unchecked")
8-
public interface List<T> extends IList<T> {
8+
public abstract class List<T> implements IList<T> {
99

1010
/**
1111
* A dynamic array, growable array, resizable array, dynamic table, or array
@@ -16,7 +16,7 @@ public interface List<T> extends IList<T> {
1616
*
1717
* @author Justin Wetherell <phishman3579@gmail.com>
1818
*/
19-
public static class ArrayList<T> implements List<T> {
19+
public static class ArrayList<T> extends List<T> {
2020

2121
private static final int MINIMUM_SIZE = 1024;
2222

@@ -284,7 +284,7 @@ public T set(int index, T value) {
284284
*
285285
* @author Justin Wetherell <phishman3579@gmail.com>
286286
*/
287-
public static class SinglyLinkedList<T> implements List<T> {
287+
public static class SinglyLinkedList<T> extends List<T> {
288288

289289
private int size = 0;
290290
private Node<T> head = null;
@@ -692,7 +692,7 @@ public T previous() {
692692
*
693693
* @author Justin Wetherell <phishman3579@gmail.com>
694694
*/
695-
public static class DoublyLinkedList<T> implements List<T> {
695+
public static class DoublyLinkedList<T> extends List<T> {
696696

697697
private int size = 0;
698698
private Node<T> head = null;

0 commit comments

Comments
 (0)