Skip to main content

Posts

Showing posts from July, 2012

Binary search tree

binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-basedbinary treedata structure which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node contains only nodes with keys greater than or equal to the node's key.Both the left and right subtrees must also be binary search trees. 
Searching Searching a binary search tree for a specific value can be a recursive or iterative process.  bool BinarySearchTree::search(int val){ Node *next = this->root();while(next !=NULL){if(val == next->value()){returntrue;}elseif(val < next->value()){ next = next->left();}else{ next = next->right();}}//not foundreturnfalse;}

Insertion Insertion begins as a search would begin; if the root is not equal to the value, we search the left or right subtrees as before. Eventually, we will reach an external node and add the va…

A tricky of using Eclipse

When using xCode, it is always fun to tap 'ES" button and use the feature of code sense. How about Eclipse? by default, you have to type 'Ctrl+Space‘, which is a very good way to interrupt your coding feeling. As you have to move your left hand back to touch the keys.

By using ESC key, you just need to reach your little finger, Done! It's a very nice invention from apple.

To do it in Eclipse, you only need to change the hot key to 'ESC' and you then can enjoy this great feature.


Quicksort implementation by using Java

source: http://www.algolist.net/Algorithms/Sorting/Quicksort.

The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:
1st: Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value(e.g. some people would like to pick the first element and do the exchange in the end)

2nd: Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Apply quicksort algorithm recursively to the left and the right parts - the previous pivot element excluded!

Partition algorithm in detail:

There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the …

Merge two sorted arrays

public class MergeArrays {

public static void main(String args[]) {
int[] a1 = new int[] { 1, 3, 5, 7, 9,23 };
int[] a2 = new int[] { 2, 4, 6,10,21 };
int[] c = mergeArrays(a1, a2);

for (int i : c)
System.out.println(" " + i);
}

public static int[] mergeArrays(int[] a1, int[] a2) {
int len = a1.length + a2.length;
int[] newA = new int[len];
int indexA1 = 0;
int indexA2 = 0;
int indexNew = 0;

while (indexA1 < a1.length && indexA2 < a2.length) {
if (a1[indexA1] <= a2[indexA2]) {
newA[indexNew] = a1[indexA1];
indexA1++;
} else {
newA[indexNew] = a2[indexA2];
indexA2++;
}

indexNew++;
}


while(indexA1 <a1.length)
{
newA[indexNew] = a1[indexA1];
indexNew++;
indexA1++;
}

while(indexA2 <a2.length)
{
newA[indexNew] = a2[indexA2++];
indexNew++;
indexA2++;
}


return newA;
}

Merge Sort implementation by using java

public class MergeSort {

private int[] numbers;
private int count;

public static void main(String[] args)
{
int[] a1 = new int[] { 3, 10, 5, 1, 9, 20, 11,8,15 };
MergeSort ms = new MergeSort(a1);
ms.mergesort(0, ms.count - 1);

for (int i : a1)
System.out.println(" " + i);
}


public MergeSort(int[] a)
{
numbers = a;
count = a.length;
}

private void mergesort(int low, int high) {

if (low < high) {
int middle = (low + high) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}

private void merge(int low, int middle, int high) {

int helper[] = new int[high+1];
for (int i = low; i <= high; i++) {
helper[i] = numbers[i];
}

int i = low;
int j = middle + 1;
int k = low;

while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}

while (i <= middle) {
numbers[k] = helper[i];
k++;
i++;
}

while (j <= high) {
numbers[k] = helper[j];
k++;
j++;
}

}

Create Binary Heap sorting by using Java

binary heap is a set of nodes with keys arranged in a complete heap-ordered binary tree


A binary tree is heap-ordered if the key in each node is larger than (or equal to) the keys in that nodes two children (if any).


Rule: 
In a heap, the parent of the node in position k is in position k/2; and, conversely, the two children of the node in position k are in positions 2k and 2k + 1. We can travel up and down by doing simple arithmetic on array indices: to move up the tree from a[k] we set k to k/2; to move down the tree we set k to 2*k or 2*k+1.


Top-down heapify (sink). If the heap order is violated because a node's key becomes smaller than one or both of that node's children's keys, then we can make progress toward fixing the violation by exchanging the node with the larger of its two children. This switch may cause a violation at the child; we fix that violation in the same way, and so forth, moving down the heap until we reach a node with both children smaller, or the botto…

The Lazy Loading in .NET Entity Framework

We did some web service testing based on SQL Server, they all worked well. However, after move to oracle, they all failed.

After adding below codes,  they all works.
_context.ContextOptions.LazyLoadingEnabled = false;

Notice the reason could be entity framework has different behavior. In SQL Server, eager loading by default. In Oracle, Lazy loading by default.

Sigh :(

Resolve the Visual Studio Crash issue

For whatever reason, my Visual Studio.NET 2010 crashed very often.

I found the best way of resolving this issue is reset the environment.
Steps:
step 1: go to folder;
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC

step 2: issue below command:
Devenv.exe /ResetSettings

How to config ODP.net in visual studio

After installing the ORACLE ODP.NET, I met THE ORA-12154:tns: cound not resolve the connect identifier specified MESSAGE.
To resove this, need to config the connection details(you can get the dialog through visual studio - > tools -> connect to database) correctly.
Data source:  Oracle Database(Oracle ODP.NET)
Connection details, Data source name: localhost/xe   comments: the format is machine name or ip/sid  , no space between them
user name: oracle user name, should have create session privilege password: the password for that user



Spring.net Application Framework - Object id and name

When using XML-based configuration metadata, you use the 'id' and/or 'name'attributes to specify the object
identifier(s). The 'id' attribute allows you to specify exactly one id, and because it is a real XML element ID
attribute, the XML parser is able to do some extra validation when other elements reference the id. As such, it is the preferred way to specify an object id. However, the XML specification does limit the characters which
are legal in XML IDs. This is usually not a constraint, but if you have a need to use one of these special XML characters, or want to introduce other aliases to the object

Spring.net Application Framework - Part One

Our new project will use Spring.net 2.0, hence, I started learning how to use it. Need to create a prototype implementation for team.

How to install it?
Like other .net library, you just copy the dll files
(Spring.Services.dll, Spring.Aop.dll and Spring.Core, Common.Logging.dll and the counterpart xmls files to
 to your project and do the configuration.

How to use it?
Take Spring Integration with WCF(Windows Communication Foundation) as an example,


Step 1: define the Services
 namespace Spring.WcfQuickStart
{
    [Serializable]
    public class BinaryOperationArgs
    {
        private double x;
        private double y;

        public BinaryOperationArgs(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double X
        {
            get { return x; }
        }

        public double Y
        {
            get { return y; }
        }
    }

    [Serializable]
    public class OperationResult
    {
        private double result;

    …