2009-03-30

Preorder, Inorder, Postorder


$ \mbox{$\bullet$}$
If a tree is null, then the empty list is the preorder, inorder, and
postorder listing of T
 
$ \mbox{$\bullet$}$
If T comprises a single node, that node itself is the preorder, inorder,
and
postorder list of T
 
$ \mbox{$\bullet$}$
Otherwise

1.
The preorder listing of T is the root of T, followed by the nodes of
T1 in preorder, . . . , and the nodes of Tk in preorder.
2.
The inorder listing of T is the nodes of T1 in inorder, followed by
the root r, followed by the nodes of T2 in inorder, . . . , and the nodes of
Tk in inorder.
3.
The postorder listing of T is the nodes of T1 in postorder, . . . ,
the nodes of Tk in postorder, all followed by the root r.


 
$ \mbox{$\bullet$}$
Example: see Figure 4.2.





Preorder 1,2,3,5,8,9,6,10,4,7

Postorder 2,8,9,5,10,6,3,7,4,1

Inorder 2,1,8,5,9,3,10,6,7,4
 
$ \mbox{$\bullet$}$
Note that the order of appearance of the leaves is the same in all the three
schemes. This is true in general.


  

Figure 4.2:
Example of a general tree
\begin{figure}<br />\centerline{\psfig{figure=figures/Ftree2.ps}}<br />\end{figure}
 blog it

Task Preemption in TinyOS-2.x

clipped from www.tinyos.net
The MISL research lab at UCC & InfoLab21 at Lancaster University have developed a new scheduler for TinyOS-2.x as a tinyos-2.x-contrib


By adding task preemption the new scheduler is capable of interrupting non-critical tasks and begin processing tasks of greater priority immediately. Preemptive schedulers can make it easier for users to meet their application timing constraints.

The new scheduler allows programmers to define a TinyOS task with a specific priority. 5 task priority levels are possible one of which supports the basic TinyOS task.

Information: Visit the tinyos-2.x-contrib directory.
Or alternatively download a pdf containing additional information regarding the scheduler design from: http://www.cs.ucc.ie/~cd5/publications/PL_DESIGN.pdf

Download:
The project can be downloaded from cvs with
cvs -d:pserver:anonymous@tinyos.cvs.sourceforge.net:/cvsroot/tinyos login
cvs -z3 -d:pserver:anonymous@tinyos.cvs.sourceforge.net:/cvsroot/tinyos co -P tinyos-2.x-contrib/ucc/PLScheduler
 blog it

mb_string 字元編碼偵測轉換

clipped from tzangms.com

除了 iconv, mb_string 也是一個用來轉換偵測字元編碼的好東西。先前再做信件編碼處理的部份, 都是用到 iconv, 不過這次用 mb_string 處理一些簡體、繁體及UTF-8之間的轉換, 所以來寫一下 memo。(基本上只對簡體繁體及UTF-8做處理)

function detectCharset($str){
$encoding_list = 'ASCII, EUC-CN, BIG-5, UTF-8';
return mb_detect_encoding($str, $encoding_list);
}

嗯~ 看到 $encoding_list , EUC-CN 是..?? 嗯, 在 mb_string 中 EUC-CN 代表的就是 GB2312, 而平常在打的 big5 在 mb_string 則是要成 BIG-5, 就是要多一個 dash ( - ), 跟一般用的編碼名稱稍微有些不一樣, 這裡列出部分 mb_string 的 encoding 名稱:

  • UTF-8

  • EUC-JP

  • ISO-2022-JP

  • EUC-CN

  • CP936

  • BIG-5
  • 在繁簡中文以及UTF-8的偵測的時候, 要注意 $encoding_list 的順序, 如果將 $encoding_list 變成下列的順序位置, 那麼結果可能不會是你想要的。

    $encoding_list = 'BIG-5, UTF-8, EUC-CN';

    如果用這個 encoding_list 來偵測 GB2312 的字元的時候, 所得到的結果將會是 BIG-5。我想這個結果可能是因為, BIG-5 也包含了一些簡體字元, 不過我測試過的數量不多, 也許會有例外, 但目前這個 encoding_list 的順序還不錯, 可以正確抓到我要的結果。

    function convertCharset($str, $encoding){
    if ($encoding == 'EUC-CN') $encoding = 'CP936';
    if ($encoding == 'UTF-8' || $encoding == 'ASCII')
    return $str;
    else
    return mb_convert_encoding($str, 'UTF-8', $encoding);
    }

     blog it

    Unsigned vs. Signed

    在定義整數變數的型態的時候可以加上 unsigned 或是 signed, 例如
    unsigned char
    unsigned short (int)
    unsigned long (int)
    unsigned int
    ----------
    signed char
    signed short (int)
    signed long (int)
    signed int
    --------------
    上面 signed 有加和沒有加是一樣的意義
    加上 unsigned 以後,
    1. 所需要的資料儲存空間和沒有加 unsigned 時是一樣的
    2. 在使用 printf() 列印時基本上你必須分清楚
    unsigned 有影響到的是參數的傳遞, 使用 %d 或是
    %u 基本上是看程式設計者自己的選擇
    int i=-1;
    printf("%d %u\n", i, i);
    會印出
    -1 4294967295
    unsigned int i=-1;
    printf("%d %u\n", i, i);
    也會印出
    -1 4294967295
    char i=-1;
    printf("%d %u\n", i, i);
    還是會印出
    -1 4294967295
    但是
    unsigned char i=-1;
    printf("%d %u\n", i, i);
    則會印出
    255 255
    這不是 %d 和 %u 的問題, 而是
    參數傳遞時資料轉換的問題 (見下面第 3 項)
     blog it

    C pointers

    clipped from wps.prenhall.com
  • Pointers are variables that contain as their values addresses of other variables.
  • Pointers must be defined before they can be used.
  • The definition

    int *ptr;

  • defines ptr to be a pointer to an object of type int and is read, "ptr is a pointer to int." The * as used here indicates that the variable is a pointer.

  • There are three values that can be used to initialize a pointer; 0, NULL, or an address. Initializing a pointer to 0 and initializing that same pointer to NULL are identical.
  • The only integer that can be assigned to a pointer is 0.
  • The & (address) operator returns the address of its operand.
  • The * operator, referred to as the indirection or dereferencing operator, returns the value of the object that its operand points to in memory. This is called dereferencing the pointer.
  •  blog it

    The basic stack algorithm

    clipped from www.yucs.org

    Here is a simple C implementation of the multi-stack algorithm.


    unsigned int f(unsigned int);
    const int K=10; /* The number of stacks */
    const int stackSize=50;
    int multiStack(unsigned int x0)
    {
    struct { unsigned int val; int t; }
    stack[K][stackSize];
    unsigned int x=x0;
    int h[K]; /* The stack sizes */
    int i, k, time=0;
    for (i=0; i<K; i++)
    h[i]=0;
    while(1)
    {
    k = x % K;
    for (i=h[k]-1; i>=0; i--)
    if (stack[k][i].val<=x)
    break;
    if (i>=0 && stack[k][i].val==x)
    return time - stack[k][i].t;
    stack[k][i+1].val=x;
    stack[k][i+1].t=time;
    h[k]=i+2;
    assert(h[k]<stackSize);
    x=f(x);
    time++;
    }
    }
     blog it

    2009-03-20

    一种比较方便的安装tinyOS-2.x的方法!

    clipped from blog.csdn.net

    在tinyOS的官方网站有tinyOS的安装教程,但是比较繁琐!

    下面介绍一种比较方便的安装tinyOS-2.x的方法

    1、安装crossbow提供的motework,不要修改安装路径。属于傻瓜式安装,安装完motework后,就有cygwin、tinyOS-1.x了,那么接下来就是升级到2.x。

    2、下载tinyos-2.0.0-3.cygwin.noarch.rpm,从cygwin进入tinyos-2.0.0-3.cygwin.noarch.rpm所在的目录,执行:

    rpm -ivh --ignoreos tinyos-2.0.0-3.cygwin.noarch.rpm就好了!

     

    现在就可以在1.x和2.x中随便切换使用了。

    需要修改C:\Crossbow\cygwin\etc下的profile,在profile文件结尾处加上

    alias use1="source /etc/profile.d/tinyos.sh"
    alias use2="source /etc/profile.d/tinyos-2.x.sh"

    这下就可以在cygwin中用use1来使用tinyOS-1.x而use2使用tinyOS-2.x了!

     

    修改完profile,如果这个时侯你的cygwin还开着,那就麻烦把cygwin关掉重开一下!

     blog it