Skip to content

Commit

Permalink
moved performance comparisons to new package.
Browse files Browse the repository at this point in the history
  • Loading branch information
David committed Oct 25, 2017
1 parent e4ec017 commit 3f5cd93
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 207 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Comparing in-order insertions with OpenJDK’s JDK 1.8 TreeMap and this AVL impl
| AVL | 36ms | 192ms |
| Red-black | 54ms | 323ms |

Balanced binary trees such as red-black, AVL and WAVL can have a reputation for being difficult to code, when each tree transformation necessary for insert and delete is
Balanced binary trees such as red-black, AVL and WAVL can have a reputation for being difficult to code,
when each tree transformation necessary for insert and delete is
drawn out then it becomes clear that the code will be quick to implement.

## The AVL vs. Red-black Tree History
Expand Down
2 changes: 2 additions & 0 deletions randomInts.txt

Large diffs are not rendered by default.

139 changes: 0 additions & 139 deletions src/main/java/bbst_showdown/Standoff.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/main/java/bbst_showdown/TreeMapAVL.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Set;
import java.util.Spliterator;

import bbst_showdown.TreeMapAVLRB.Entry;


/**
* An AVL tree implementation.
Expand Down Expand Up @@ -1140,4 +1142,57 @@ public Map.Entry<K, V> higherEntry(K key) {
public K higherKey(K key) {
return keyOrNull(getHigherEntry(key));
}

public void printSubtree(int indent, Entry<K, V> x) {
for( int i = 0; i < indent; ++i) {
System.out.print(" ");
}

if(x.left != null || x.right != null) {
System.out.println("(" + x.value);
if (x.left != null)
printSubtree(indent + 12, x.left); //this is a recursive call, alternatively use the indent formula above if you don't use recursion
if (x.right != null)
printSubtree(indent + 12, x.right);

//we have a new line so print the indent again
for( int i = 0; i < indent; ++i) {
System.out.print(" ");
}
System.out.println(")");
} else if(x.value != null) {
System.out.println(x.value);
} else { //empty/non existing node
System.out.println("()");
}
}

public static void main(String[] agrs) {
String fileName = "randomInts.txt";
java.io.File file = new java.io.File(fileName);

try {
java.util.Scanner inputStream = new java.util.Scanner(file);
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
Integer[] v = new Integer[1000000];
for (int i = 0; i < 1000000; i++) {
v[i] = Integer.parseInt(values[i]);
}
TreeMapAVL<Integer, Integer> x = new TreeMapAVL<Integer, Integer>();
long start = System.currentTimeMillis();
for (int i = 0; i < 24; i++) {
x.put(v[i], v[i]);
}
long stop = System.currentTimeMillis();
x.printSubtree(0, x.root);

System.out.println("Time red-black:" + (stop - start) + " rotations:" + x.rotations() + " height:" + x.treeHeight());
}
inputStream.close();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
}
}
}
60 changes: 59 additions & 1 deletion src/main/java/bbst_showdown/TreeMapAVLRB.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public int hashCode() {
}

public String toString() {
return key + "=" + value;
return key + "=" + value + "," + deltaR;
}
}

Expand Down Expand Up @@ -338,6 +338,7 @@ else if (cmp > 0)
}

if (sibling == null) {
System.out.println("INSERTING:" + e);
fixAfterInsertion(parent);
}

Expand Down Expand Up @@ -369,6 +370,7 @@ private void fixAfterInsertion(Entry<K, V> x) {
if (p.left == x) { // node was added on left so check if left side is unbalanced
Entry<K, V> sibling = p.right;
if (sibling == null || sibling.deltaR == TWO) { // need to rebalance
System.out.println("ROTATING:" + sibling);
if (sibling != null)
sibling.deltaR = ONE;

Expand All @@ -384,6 +386,7 @@ private void fixAfterInsertion(Entry<K, V> x) {
} else {
Entry<K, V> sibling = p.left;
if (sibling == null || sibling.deltaR == TWO) { // need to rebalance
System.out.println("ROTATING 2");
if (sibling != null)
sibling.deltaR = ONE;

Expand All @@ -394,6 +397,8 @@ private void fixAfterInsertion(Entry<K, V> x) {
rotateLeft(p);
break;
} else if (sibling.deltaR == ONE) {
if ((int) x.value == 2130768989)
System.out.println("HERE");
sibling.deltaR = TWO;
}
}
Expand Down Expand Up @@ -652,4 +657,57 @@ public void inOrderTraversal(Entry<K, V> x) {
System.out.println(x.value + ", " + x.deltaR);
inOrderTraversal(x.right);
}
public void printSubtree(int indent, Entry<K, V> x) {
for( int i = 0; i < indent; ++i) {
System.out.print(" ");
}

if(x.left != null || x.right != null) {
System.out.println("(" + x.value);
if (x.left != null)
printSubtree(indent + 12, x.left); //this is a recursive call, alternatively use the indent formula above if you don't use recursion
if (x.right != null)
printSubtree(indent + 12, x.right);

//we have a new line so print the indent again
for( int i = 0; i < indent; ++i) {
System.out.print(" ");
}
System.out.println(")");
} else if(x.value != null) {
System.out.println(x.value);
} else { //empty/non existing node
System.out.println("()");
}
}

public static void main(String[] agrs) {
String fileName = "randomInts.txt";
java.io.File file = new java.io.File(fileName);

try {
java.util.Scanner inputStream = new java.util.Scanner(file);
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
Integer[] v = new Integer[1000000];
for (int i = 0; i < 1000000; i++) {
v[i] = Integer.parseInt(values[i]);
}
TreeMapAVLRB<Integer, Integer> x = new TreeMapAVLRB<Integer, Integer>();
long start = System.currentTimeMillis();
for (int i = 0; i < 23; i++) {
x.put(v[i], v[i]);
}
long stop = System.currentTimeMillis();
x.printSubtree(0, x.root);

System.out.println("Time red-black:" + (stop - start) + " rotations:" + x.rotations() + " height:" + x.treeHeight());
}
inputStream.close();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
}
}

}
Loading

0 comments on commit 3f5cd93

Please sign in to comment.