1
九月

資料結構程式 09/01: ex10.c

   Posted by: admin   in 103(暑)資料結構

#include <stdio.h> //標準輸出入函數的標頭檔
#include <stdlib.h>
void Travel(int* Tree, int curNode)
{
     if (Tree[curNode]==-1)
        return;
     Travel(Tree, curNode * 2);//走訪左子樹
     printf("%3d ", Tree[curNode]);//走訪樹根
     Travel(Tree, curNode * 2 + 1);//走訪右子樹
}
int main(int argc, char *argv[])//主函數入口
 
    int Level = 12;
    int Nodes = 8192;
    int Tree[Nodes];
    int i, j, data, curNode, curLevel;
    srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子
    //讓每個節點啟始值是-1表示是空的 
    for (i=0; i< Nodes; i++)
        Tree[i] = -1;
    //插入20個資料項到這個樹
    for (i=0; i< 20; i++)
    {
        data = rand()%100;
        curNode = 1;//根節點是編號是1 
        curLevel = 1;//目前階層 
        while (curLevel <= Level)
        { 
          if (Tree[curNode] == -1)//目前節點是空的
          { 
            Tree[curNode] = data;
            break;
          } 
          else if (data < Tree[curNode])
            curNode = 2 * curNode;
          else //if (data >= Tree[curNode])
              curNode = 2 * curNode +1;
          curLevel++; 
        } 
    }
    //使用中序列印出整個二元搜尋樹
    Travel(Tree, 1);
    return 0; 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

This entry was posted on 星期一, 九月 1st, 2014 at 03:43:28 and is filed under 103(暑)資料結構. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Trackbacks/Pings

  1. eddie    十一月 19 2014 / 4上午:

    conveniently@lucien.koop” rel=”nofollow”>.…

    thanks….

  2. dean    十一月 27 2014 / 11下午:

    islandia@laxative.nouvelle” rel=”nofollow”>.…

    ñïàñèáî!…

  3. Fred    十二月 24 2014 / 2下午:

    showmanship@dusting.agglutinins” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  4. Clyde    一月 16 2015 / 9下午:

    hemorrhoids@flickered.soothingly” rel=”nofollow”>.…

    ñïñ çà èíôó….

  5. Adrian    一月 22 2015 / 3下午:

    appliances@signal.titter” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  6. Matt    二月 01 2015 / 5上午:

    accommodated@chisels.fairfax” rel=”nofollow”>.…

    good!…

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment