Thursday, October 18, 2012
Classic Shell: great replacement for missing windows 8 start menu
Pokki constantly Page Faulting
My mac is overheating, possibly in part due to this (and parallels itself), and I'm not running anything else intensive. I've contacted Pokki help/support, and I'll update with what they say. Edit: They replied saying that this behaviour is normal and should not be a cause for concern. Hm.
Tuesday, October 9, 2012
Demonstrating usage of a Binary Search Tree in Java
What is a Binary Search Tree?
Binary Search Tree (BST) is a tree in which each node has up to two child nodes (subtrees). If pointers are used, then each node contains references to the left, right, and parent nodes. Otherwise, like the implementation I found for Java, each node contains a left and right subtree in the form of a left and right node (each of which have their own two subtrees and so on...) along with the data associated with the node.
A BST is said to have a binary-search-tree property:
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] <= key[x]. If y is a node in the right subtree of x, then key[x] <= key[y].1
Since Java doesn't have pointers that we can use, we won't keep track of keys. We'll work directly with node data.
Cormen et-all mention that BSTs support the following operations:
Search trees are data structures that support many dynamic-set operations, including
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and
DELETE. Thus, a search tree can be used both as a dictionary and as a priority
queue.
What are some applications of BST?
This StackOverflow question answers this well:
What are the applications of binary trees?
Apparently, it is also used in the solution to the Traveling Salesman Problem and the related Shortest Path Problem. I'll be checking these out soon.
How do I use a BST in Java?
I found a good Java BST impl here:
BST.java
BSTNode.java
Class BST contains a root BSTNode node and provides most BST operations (as identified by Cormen et-all). The PREDECESSOR and SUCCESSOR operations are not implemented. Search is implemented, though not exposed directly (it's exposed as isInTree(T el)).
I've slapped together an Eclipse project that demonstrates the usage of this BST implementation. I added some code to BST.java (the one in the Eclipse project) borrowed from this Stanford CS Library page on BSTs. I also borrowed some inspiration from this SO page: How to print binary tree diagram. The class BTreePrinter in the project is pretty much directly from this SO page.
It would be worthwhile to study at least the BST.java and BSTNode.java.
>Download BST_Demo (Eclipse Project)
Here is the output:
>String arrays:
|
1 Introduction to Algorithms Second Edition (Cormen, Leiserson, Rivest, Stein), page 254
Thursday, October 4, 2012
Synchronization and Thread Safety in Hashtable, Hashmap, ConcurerntHashMap
Here are three examples of thread unsafe usage, followed by three examples of safe usage:
1. Unsafe
package unsafe.demo;
|
1. Unsafe cont.
package unsafe.threads;
|
2. Unsafe
package unsafe.demo;
|
2. Unsafe cont.
package unsafe.threads;
|
package unsafe.demo; |
package unsafe.threads;
|
Examples of safe usage:
1. Safe
package safe.demo;
|
package safe.threads;
|
2. Safe
package safe.demo;
|
2. Safe cont.
package safe.threads;
|
3. Safe
package safe.demo;
|
package safe.threads; |