3
九月

資料結構程式 09/03: ex11.c

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

#include <stdio.h> //標準輸出入函數的標頭檔
#include <stdlib.h>
typedef struct node//節點結構的自定型別定義 
{
    int data;//整數 
    struct node *left;//節點的指標 
    struct node *right;//節點的指標
} Node;
Node* NewNode()
{
    Node *newNode = malloc(sizeof(Node));//新節點(node) 
    newNode->data = 0;
    newNode->left = NULL;
    newNode->right = NULL;
    return  newNode; 
}
void Travel(Node* curNode)
{
     if (curNode==NULL)
        return;
     Travel(curNode->left);//走訪左子樹
     printf("%3d ", curNode->data);//走訪樹根
     Travel(curNode->right);//走訪右子樹
}
int main(int argc, char *argv[])//
{//程式寫在main()函數裡
    Node* Tree = NewNode();
    Node* curNode = NULL;
    int i, newdata;
    srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子
    //插入20個資料項到這個樹
    newdata = rand()%100;
    Tree->data = newdata;
    for (i=1; i< 20; i++)
    {
        newdata = rand()%100;
        curNode = Tree       
        while (1)
        {
              if (newdata < curNode->data)
              {
                 if (curNode->left == NULL)
                 {
                     curNode->left = NewNode();
                     curNode->left->data = newdata;
                     break;                                
                 }
                 else
                     curNode = curNode->left;
              }
              else
              {
                 if (curNode->right == NULL)
                 {
                     curNode->right = NewNode();
                     curNode->right->data = newdata;
                     break;                                   
                 }
                 else
                     curNode = curNode->right;
              }
        }        
    }
    //使用中序列印出整個二元搜尋樹
    Travel(Tree);
    return 0;

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

This entry was posted on 星期三, 九月 3rd, 2014 at 03:24:49 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.

10 Trackbacks/Pings

  1. jesse    十一月 11 2014 / 3下午:

    trusted@pillspot.com” rel=”nofollow”>.…

    thanks!!…

  2. ronald    十一月 23 2014 / 8上午:

    yugoslavia@civilization.insomnia” rel=”nofollow”>.…

    good info!!…

  3. michael    十二月 08 2014 / 6下午:

    screens@hindering.beech” rel=”nofollow”>.…

    thanks!!…

  4. Harold    十二月 12 2014 / 8上午:

    myras@polarized.administrative” rel=”nofollow”>.…

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

  5. Ricardo    十二月 26 2014 / 7上午:

    unsuspecting@channing.possible” rel=”nofollow”>.…

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

  6. Ken    十二月 26 2014 / 2下午:

    limping@telemann.engages” rel=”nofollow”>.…

    thank you….

  7. Juan    一月 29 2015 / 11下午:

    goodby@blanket.fresnel” rel=”nofollow”>.…

    ñïàñèáî!…

  8. alex    一月 29 2015 / 11下午:

    whitehall@intriguingly.spreading” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

  9. clayton    一月 30 2015 / 11上午:

    regency@condemning.floured” rel=”nofollow”>.…

    thank you!!…

  10. Perry    二月 14 2015 / 1下午:

    lust@beavertail.anorexia” rel=”nofollow”>.…

    áëàãîäàðþ!…

Leave a reply

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