Skip to content

Commit 11cf8ea

Browse files
author
Justin Wetherell
committed
Updates to the data structure timing code
1 parent 88a4e7e commit 11cf8ea

File tree

8 files changed

+1051
-438
lines changed

8 files changed

+1051
-438
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @author Justin Wetherell <phishman3579@gmail.com>
1919
*/
20-
public class AVLTree<T extends Comparable<T>> extends BinarySearchTree<T> {
20+
public class AVLTree<T extends Comparable<T>> extends BinarySearchTree<T> implements BinarySearchTree.INodeCreator<T> {
2121

2222
private enum Balance {
2323
LEFT_LEFT, LEFT_RIGHT, RIGHT_LEFT, RIGHT_RIGHT
@@ -27,15 +27,7 @@ private enum Balance {
2727
* Default constructor.
2828
*/
2929
public AVLTree() {
30-
this.creator = new BinarySearchTree.INodeCreator<T>() {
31-
/**
32-
* {@inheritDoc}
33-
*/
34-
@Override
35-
public BinarySearchTree.Node<T> createNewNode(BinarySearchTree.Node<T> parent, T id) {
36-
return (new AVLNode<T>(parent, id));
37-
}
38-
};
30+
this.creator = this;
3931
}
4032

4133
/**
@@ -253,6 +245,14 @@ public String toString() {
253245
return AVLTreePrinter.getString(this);
254246
}
255247

248+
/**
249+
* {@inheritDoc}
250+
*/
251+
@Override
252+
public Node<T> createNewNode(Node<T> parent, T id) {
253+
return (new AVLNode<T>(parent, id));
254+
}
255+
256256
protected static class AVLNode<T extends Comparable<T>> extends Node<T> {
257257

258258
protected int height = 1;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public BinarySearchTree() {
5252
*/
5353
@Override
5454
public Node<T> createNewNode(Node<T> parent, T id) {
55-
return (new Node<T>(parent, id));
55+
return (new Node<T>(parent,id));
5656
}
5757
};
5858
}

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

+47-18
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,7 @@ public class PatriciaTrie<C extends CharSequence> implements ITree<C> {
2424
protected static final boolean BLACK = false; // non-terminating
2525
protected static final boolean WHITE = true; // terminating
2626

27-
public PatriciaTrie() {
28-
this.creator = new INodeCreator() {
29-
/**
30-
* {@inheritDoc}
31-
*/
32-
@Override
33-
public Node createNewNode(Node parent, char[] seq, boolean type) {
34-
return (new Node(parent, seq, type));
35-
}
36-
};
37-
}
27+
public PatriciaTrie() { }
3828

3929
/**
4030
* Constructor with external Node creator.
@@ -43,6 +33,21 @@ public PatriciaTrie(INodeCreator creator) {
4333
this.creator = creator;
4434
}
4535

36+
/**
37+
* Create a new node for sequence.
38+
*
39+
* @param parent
40+
* node of the new node.
41+
* @param seq
42+
* of characters which represents this node.
43+
* @param type
44+
* of the node, can be either BLACK or WHITE.
45+
* @return Node which was created.
46+
*/
47+
protected static Node createNewNode(Node parent, char[] seq, boolean type) {
48+
return (new Node(parent, seq, type));
49+
}
50+
4651
/**
4752
* {@inheritDoc}
4853
*/
@@ -62,8 +67,12 @@ public boolean add(C seq) {
6267
* sequence already exists.
6368
*/
6469
protected Node addSequence(C seq) {
65-
if (root == null)
66-
root = this.creator.createNewNode(null, null, BLACK);
70+
if (root == null) {
71+
if (this.creator == null)
72+
root = createNewNode(null, null, BLACK);
73+
else
74+
root = this.creator.createNewNode(null, null, BLACK);
75+
}
6776

6877
int indexIntoParent = -1;
6978
int indexIntoString = -1;
@@ -108,7 +117,11 @@ protected Node addSequence(C seq) {
108117

109118
// Create new parent
110119
if (parent != null) parent.removeChild(node);
111-
Node newParent = this.creator.createNewNode(parent, parentString, BLACK);
120+
Node newParent = null;
121+
if (this.creator == null)
122+
newParent = createNewNode(parent, parentString, BLACK);
123+
else
124+
newParent = this.creator.createNewNode(parent, parentString, BLACK);
112125
if (parent != null)
113126
parent.addChild(newParent);
114127

@@ -120,7 +133,11 @@ protected Node addSequence(C seq) {
120133

121134
// Create a new node from the rest of the string
122135
CharSequence newString = seq.subSequence(indexIntoString, seq.length());
123-
Node newNode2 = this.creator.createNewNode(newParent, newString.toString().toCharArray(), WHITE);
136+
Node newNode2 = null;
137+
if (this.creator == null)
138+
newNode2 = createNewNode(newParent, newString.toString().toCharArray(), WHITE);
139+
else
140+
newNode2 = this.creator.createNewNode(newParent, newString.toString().toCharArray(), WHITE);
124141
newParent.addChild(newNode2);
125142

126143
// New node which was added
@@ -130,7 +147,11 @@ protected Node addSequence(C seq) {
130147
// converting the previous node
131148
if (parent != null)
132149
parent.removeChild(node);
133-
Node newParent = this.creator.createNewNode(parent, parentString, WHITE);
150+
Node newParent = null;
151+
if (this.creator == null)
152+
newParent = createNewNode(parent, parentString, WHITE);
153+
else
154+
newParent = this.creator.createNewNode(parent, parentString, WHITE);
134155
if (parent != null)
135156
parent.addChild(newParent);
136157

@@ -156,12 +177,20 @@ protected Node addSequence(C seq) {
156177
} else if (node.string != null) {
157178
// Adding a child
158179
CharSequence newString = seq.subSequence(indexIntoString, seq.length());
159-
Node newNode = this.creator.createNewNode(node, newString.toString().toCharArray(), WHITE);
180+
Node newNode = null;
181+
if (this.creator == null)
182+
newNode = createNewNode(node, newString.toString().toCharArray(), WHITE);
183+
else
184+
newNode = this.creator.createNewNode(node, newString.toString().toCharArray(), WHITE);
160185
node.addChild(newNode);
161186
addedNode = newNode;
162187
} else {
163188
// Add to root node
164-
Node newNode = this.creator.createNewNode(node, seq.toString().toCharArray(), WHITE);
189+
Node newNode = null;
190+
if (this.creator == null)
191+
newNode = createNewNode(node, seq.toString().toCharArray(), WHITE);
192+
else
193+
newNode = this.creator.createNewNode(node, seq.toString().toCharArray(), WHITE);
165194
node.addChild(newNode);
166195
addedNode = newNode;
167196
}

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

+31-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @author Justin Wetherell <phishman3579@gmail.com>
2121
*/
2222
@SuppressWarnings("unchecked")
23-
public class RedBlackTree<T extends Comparable<T>> extends BinarySearchTree<T> {
23+
public class RedBlackTree<T extends Comparable<T>> extends BinarySearchTree<T> implements BinarySearchTree.INodeCreator<T> {
2424

2525
protected static final boolean BLACK = false;
2626
protected static final boolean RED = true;
@@ -29,15 +29,7 @@ public class RedBlackTree<T extends Comparable<T>> extends BinarySearchTree<T> {
2929
* Default constructor.
3030
*/
3131
public RedBlackTree() {
32-
this.creator = new BinarySearchTree.INodeCreator<T>() {
33-
/**
34-
* {@inheritDoc}
35-
*/
36-
@Override
37-
public BinarySearchTree.Node<T> createNewNode(BinarySearchTree.Node<T> parent, T id) {
38-
return (new RedBlackNode<T>(parent, id, BLACK));
39-
}
40-
};
32+
this.creator = this;
4133
}
4234

4335
/**
@@ -56,12 +48,18 @@ protected Node<T> addValue(T id) {
5648
boolean added = false;
5749
if (root == null) {
5850
// Case 1 - The current node is at the root of the tree.
59-
60-
// Defaulted to black in our creator
61-
root = this.creator.createNewNode(null, id);
62-
root.lesser = this.creator.createNewNode(root, null);
63-
root.greater = this.creator.createNewNode(root, null);
64-
51+
if (this.creator == null) {
52+
root = new RedBlackNode<T>(null, id, BLACK);
53+
root.lesser = new RedBlackNode<T>(root, null, BLACK);
54+
root.greater = new RedBlackNode<T>(root, null, BLACK);
55+
} else {
56+
root = this.creator.createNewNode(null, id);
57+
((RedBlackNode<T>) root).color = BLACK;
58+
root.lesser = this.creator.createNewNode(root, null);
59+
((RedBlackNode<T>) root.lesser).color = BLACK;
60+
root.greater = this.creator.createNewNode(root, null);
61+
((RedBlackNode<T>) root.greater).color = BLACK;
62+
}
6563
nodeAdded = (RedBlackNode<T>) root;
6664
added = true;
6765
} else {
@@ -71,11 +69,15 @@ protected Node<T> addValue(T id) {
7169
if (node.id == null) {
7270
node.id = id;
7371
((RedBlackNode<T>) node).color = RED;
74-
75-
// Defaulted to black in our creator
76-
node.lesser = this.creator.createNewNode(node, null);
77-
node.greater = this.creator.createNewNode(node, null);
78-
72+
if (this.creator == null) {
73+
node.lesser = new RedBlackNode<T>(node, null, BLACK);
74+
node.greater = new RedBlackNode<T>(node, null, BLACK);
75+
} else {
76+
node.lesser = this.creator.createNewNode(node, null);
77+
((RedBlackNode<T>) node.lesser).color = BLACK;
78+
node.greater = this.creator.createNewNode(node, null);
79+
((RedBlackNode<T>) node.greater).color = BLACK;
80+
}
7981
nodeAdded = (RedBlackNode<T>) node;
8082
added = true;
8183
break;
@@ -435,6 +437,14 @@ public String toString() {
435437
return RedBlackTreePrinter.getString(this);
436438
}
437439

440+
/**
441+
* {@inheritDoc}
442+
*/
443+
@Override
444+
public Node<T> createNewNode(Node<T> parent, T id) {
445+
return (new RedBlackNode<T>(parent, id, BLACK));
446+
}
447+
438448
protected static class RedBlackNode<T extends Comparable<T>> extends Node<T> {
439449

440450
protected boolean color = BLACK;

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,15 @@
1717
*
1818
* @author Justin Wetherell <phishman3579@gmail.com>
1919
*/
20-
public class Treap<T extends Comparable<T>> extends BinarySearchTree<T> {
20+
public class Treap<T extends Comparable<T>> extends BinarySearchTree<T> implements BinarySearchTree.INodeCreator<T> {
2121

2222
private static int randomSeed = Integer.MAX_VALUE; // This should be at least twice the number of Nodes
2323

2424
/**
2525
* Default constructor.
2626
*/
2727
public Treap() {
28-
this.creator = new BinarySearchTree.INodeCreator<T>() {
29-
/**
30-
* {@inheritDoc}
31-
*/
32-
@Override
33-
public BinarySearchTree.Node<T> createNewNode(BinarySearchTree.Node<T> parent, T id) {
34-
return (new TreapNode<T>(parent, id));
35-
}
36-
};
28+
this.creator = this;
3729
}
3830

3931
/**
@@ -151,16 +143,24 @@ public String toString() {
151143
return TreapPrinter.getString(this);
152144
}
153145

146+
/**
147+
* {@inheritDoc}
148+
*/
149+
@Override
150+
public Node<T> createNewNode(Node<T> parent, T id) {
151+
return (new TreapNode<T>(null, id));
152+
}
153+
154154
private static class TreapNode<T extends Comparable<T>> extends Node<T> {
155155

156156
private int priority = Integer.MIN_VALUE;
157157

158-
private TreapNode(Node<T> parent, int priority, T value) {
158+
private TreapNode(TreapNode<T> parent, int priority, T value) {
159159
super(parent, value);
160160
this.priority = priority;
161161
}
162162

163-
private TreapNode(Node<T> parent, T value) {
163+
private TreapNode(TreapNode<T> parent, T value) {
164164
this(parent, RANDOM.nextInt(randomSeed), value);
165165
}
166166

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

+10-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import com.jwetherell.algorithms.data_structures.BinarySearchTree.INodeCreator;
76
import com.jwetherell.algorithms.data_structures.BinarySearchTree.Node;
87
import com.jwetherell.algorithms.data_structures.interfaces.IMap;
98

@@ -19,28 +18,12 @@
1918
* @author Justin Wetherell <phishman3579@gmail.com>
2019
*/
2120
@SuppressWarnings("unchecked")
22-
public class TreeMap<K extends Comparable<K>, V> implements IMap<K,V> {
21+
public class TreeMap<K extends Comparable<K>, V> implements BinarySearchTree.INodeCreator<K>, IMap<K,V> {
2322

2423
private AVLTree<K> tree = null;
2524

2625
public TreeMap() {
27-
BinarySearchTree.INodeCreator<K> creator = new BinarySearchTree.INodeCreator<K>() {
28-
/**
29-
* {@inheritDoc}
30-
*/
31-
@Override
32-
public BinarySearchTree.Node<K> createNewNode(BinarySearchTree.Node<K> parent, K id) {
33-
return (new TreeMapNode<K, V>(parent, id, null));
34-
}
35-
};
36-
tree = new AVLTree<K>(creator);
37-
}
38-
39-
/**
40-
* Constructor with external Node creator.
41-
*/
42-
public TreeMap(INodeCreator<K> creator) {
43-
tree = new AVLTree<K>(creator);
26+
tree = new AVLTree<K>(this);
4427
}
4528

4629
/**
@@ -154,6 +137,14 @@ public String toString() {
154137
return TreeMapPrinter.getString(this);
155138
}
156139

140+
/**
141+
* {@inheritDoc}
142+
*/
143+
@Override
144+
public AVLTree.Node<K> createNewNode(AVLTree.Node<K> parent, K id) {
145+
return (new TreeMapNode<K, V>(parent, id, null));
146+
}
147+
157148
protected static class TreeMapNode<K extends Comparable<K>, V> extends AVLTree.AVLNode<K> {
158149

159150
protected V value = null;

0 commit comments

Comments
 (0)