Google has made great progress on migrating desktop apps into Web during last year. They propagated trend for HTML5 support and Javascript JIT compilers among modern browsers. So it's possible to run Quake 2 or CAD software directly in browser at decent speed.
Red Black Tree is a balanced BST tree. Details are described on Wikipedia.
You can run this sample app directly on appspot (it works on iPhone too :-) )
Sample code can be found here: Browse on GitHub.
Let's start from initialization.
First, we need to create Canvas element and add it to HTML. getContext2D is called to obtain drawing context.
We register a timer to redraw frames frequently:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onModuleLoad() { | |
canvasScreen = Canvas.createIfSupported(); | |
if (canvasScreen == null) { | |
RootPanel.get().add(new Label("Sorry, your browser doesn't support the HTML5 Canvas element")); | |
return; | |
} | |
canvasScreen.setCoordinateSpaceHeight(height); | |
canvasScreen.setCoordinateSpaceWidth(width); | |
canvasScreen.setSize(width + "px", height + "px"); | |
contextScreen = canvasScreen.getContext2d(); | |
RootPanel.get().add(canvasScreen); | |
final Timer timer = new Timer() { | |
@Override | |
public void run() { | |
doUpdate(); | |
} | |
}; | |
timer.scheduleRepeating(50); | |
} |
So doUpdate is called every 50 ms and whenever redrawFrame is set to true, it redraws Canvas contents.
In autoplay mode, we call processFrame in while loop. So whenever redraw procedure takes too long, we will process frames without redrawing them. This won't slow down animation on low resources.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void doUpdate() { | |
long currentTime = new Date().getTime(); | |
if (autoplay) { | |
while (frameTime < currentTime) { | |
processFrame(); | |
frameTime += 200; | |
} | |
} | |
if (refreshFrame) { | |
refreshFrame = false; | |
contextScreen.setFillStyle(backgroundColor); | |
contextScreen.fillRect(0, 0, width, height); | |
drawTree(tree.getRoot(), 20, width-20, 40, 75); | |
contextScreen.setFillStyle(blackColor); | |
contextScreen.fillText(message, 10, 10); | |
} | |
} | |
private void processFrame() { | |
int range = 64; | |
i = (i + 1)%range; | |
if (i < range/2) { | |
tree.insert(i); | |
message = "Insert " + i; | |
} else { | |
int j = i - range/2; | |
tree.remove(j); | |
message = "Remove " + j; | |
} | |
message = message + " Time: " + (frameTime - startTime); | |
refreshFrame = true; | |
} |
Then, we need to draw a tree. We use drawTree procedure, which is recursive and draws nodes along with contents and connections between them:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawTree(RBNode n, float x1, float x2, float y1, float y2) { | |
if (n != null) { | |
if (n.left != null) { | |
drawConnection((x1 + x2)/2.0f, y1, (x2 - x1)*1.f/4.0f + x1, y2); | |
} | |
if (n.right != null) { | |
drawConnection((x1 + x2)/2.0f, y1, (x2 - x1)*3.f/4.0f + x1, y2); | |
} | |
drawNode("" + n.value, n.isBlack, (x1 + x2)/2.0f, y1); | |
drawTree(n.left, (x2 - x1)*0.f/4.0f + x1, (x2 - x1)*2.f/4.0f + x1, y2, y2 + (y2-y1)); | |
drawTree(n.right, (x2 - x1)*2.f/4.0f + x1, (x2 - x1)*4.f/4.0f + x1, y2, y2 + (y2-y1)); | |
} | |
} | |
private void drawConnection(float x1, float y1, float x2, float y2) { | |
contextScreen.setStrokeStyle(drawColor); | |
contextScreen.beginPath(); | |
contextScreen.moveTo(x1, y1); | |
contextScreen.lineTo(x2, y2); | |
contextScreen.closePath(); | |
contextScreen.stroke(); | |
} | |
private void drawNode(String label, boolean isBlack, float x, float y) { | |
contextScreen.beginPath(); | |
contextScreen.arc(x, y, 15, 0, 2*Math.PI); | |
contextScreen.closePath(); | |
contextScreen.setFillStyle(isBlack ? blackColor : redColor); | |
contextScreen.fill(); | |
contextScreen.setStrokeStyle(drawColor); | |
contextScreen.stroke(); | |
contextScreen.setFillStyle(isBlack ? backgroundColor : blackColor); | |
contextScreen.fillText(label, x - label.length() * 4 + 2, y + 4); | |
} |
The best part is that we can do regular Java unit tests on Red Black tree to verify correctness of implementation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RBTreeCorrectnessTest extends TestCase { | |
private int testSize = 1000; | |
public void testInsert() { | |
Comparator comparator = new Comparator() { | |
public int compare(Object o1, Object o2) { | |
return ((Integer) o1).compareTo((Integer) o2); | |
} | |
}; | |
RBTree tree = new RBTree(comparator); | |
for (int i = 1; i < testSize; i++) { | |
tree.insert(i); | |
tree.verifyRedBlack(); | |
} | |
System.out.println(tree.verifyRedBlack()); | |
} | |
} |