Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALGORITHM


  Binary tree iterator algorithm

Binary tree pre-order,in-order and post-order traversal are basics in algorithm and data structure.And the recursive binary tree traversal is a classical application of recursion.We define a binary tree node as :// C++struct Node { int value; Node *left; Node *right;}In order binary tree traversal can be:// C++void inorder_traverse(Node *node) { if (NULL != node->left) { inorder_traverse(node->left); } do_something(node); if (NULL != node->right) { inorder_traverse(node->right); }}Easy enough, right? Pre-order and post-order traversal algorithm...

11,624 1       ITERATOR BINARY TREE TRAVERSAL


  Generate first N prime numbers

Recently I am taking an online course named "Startup Engineering" on coursera. There is one assignment which is to use NodeJS to write a program to generate the first 100 prime numbers. This may be very easy for many people. Here I just want to share with you the code to generate not only the first 100 prime numbers. but also any number of prime numbers.Below is the code snippet://Helper class to generate prime numbersvar PrimeProcessor=(function(){ var primeArray=[]; function _isPrime(num){ if(num<=1){ throw new Error("Number cannot be sm...

11,779 5       JAVASCRIPT ALGORITHM NODEJS PRIME NUMBER COURSERA


  The significance of coding competition to actual work

Some friends asked this question: In secondary schools we participate in the NOI, in university we participate in ACM/ICPC, TopCoder.What's the significance of coding competition to actual work?I participated in the NOI from 1994 to 1996, ACM/ICPC from 1999 to 2000 and TopCoder in 2002 for six months. Later I paid little attention to coding competition, so I may not understand the changes in recent years and race mode .The capabilities we can get by participating the competition can be summarized below:1, Basic technical skills. People who participate in the competition and get good results sh...

2,832 0       PROGRAMMING FACEBOOK ACM TOPCODER


  Find the kth smallest number in an array

This is an classical question, the general solution to this question is first using sorting algorithm to sort the array, then get the kth number in the array, the complexity is O(nlogn), we can also use selective sort or head sort to, the complexity of selective sort is O(kn) and heap sort is O(nlogk). The better solution is to use quick sort to find the kth smallest number, the complexity is O(n), the worst case cost is O(n^2). But today we introduce one more solution which has the worst case cost O(n).First let's check the quick sort solution, quick sort is to find one number, then put all n...

5,417 0       SORT SEARCH QUICK SORT SMALLEST


  Significance and use of do{...}while(0)

In some Linux kernel and other open source codes, we can see some codes like below:do{ ...}while(0)This code snippet is not a loop, it seems there is no significance of using do...while this way, then why should we use it?In fact, the significance of do{...}while(0) is better than optimizing your code. After some research, we summarize some benefits of it.1. Help define complex macro to avoid error#define DOSOMETHING()\ foo1();\ foo2();The meaning of the macro is when calling DOSOMETHING(), the functions foo1() and foo2() will be called. But if you write it like thi...

8,930 0       DO{...}WHILE(0) OPTIMIZATION


  Integer overflow

You may be familiar with integer overflow, but what you may not be familiar with is how gcc handles signed integer overflow.First let's look at the standard, for unsigned integer, the standard says :A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.In other words, unsigned integer overflow will be wrapped so that it will be still in the range of an unsigned integer.For signed integer,...

3,857 0       LINUX GCC INTEGER OVERFLOW


  Implementation of Tower of Hanoi

The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:Only one disk may be moved at a time.Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present...

3,223 0       ALGORITHM TOWER OF HANOI


  Implementation of +,-,*,/ with bitwise operator

There is a question asked on Stackoverflow : Divide a number by 3 without using *,/,+,-,% operators. This question is an Oracle interview question. Some people give excellent answers. You can go there and take a look. Usually we need to use bitwise operators to do this kind of implementations. Here I want to show you ways to implement +,-,*,/ with bitwise operators.A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons ...

39,151 3       DIVISION BITWISE OPERATOR SHIFT ADD SUBTRACT MULTIPLICATION