Archive for 九月, 2014

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: , ,

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: , ,