| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 380 人关注过本帖
标题:用法 二叉树
只看楼主 加入收藏
bowenzhang
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-12-26
结帖率:0
收藏
 问题点数:0 回复次数:2 
用法 二叉树
程序代码:
#include "stdlib.h"
#include "stdio.h"
#include "winsock2.h"
#include "mysql.h"

#pragma comment(lib, "libmysql.lib")

typedef struct url_node{
    char url[128];
    int count;
    url_node *pleft;
    url_node *pright;
}url_node_t;

url_node *create_node(char *url);
url_node *add_node(char *url, url_node *pNode);
void freenodes(url_node *pNode);

int
main (int argc, char *argv[])
{
    if (argc != 8)
    {
        printf("argument is less than given");
        return -1;
    }

    char *devid;
    char *domain;
    char *start;
    char *end;
    char *table;
    char *pTable[1024] = {NULL};
    char sqlBuf[1024];
    char mini[64];
    int iTableNum;

    int iStart;
    int iLimit;

    devid = argv[1];
    domain = argv[2];
    start = argv[3];
    end = argv[4];
    table = argv[5];
    iStart = atoi(argv[6]);
    iLimit = atoi(argv[7]);

    MYSQL mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;
    my_ulonglong num;
    

    if (mysql_init(&mysql) == NULL){
        printf("%s", mysql_error(&mysql));
        return -1;
    }

    if (mysql_real_connect(&mysql, "localhost", "root",
        "", "", 3306, 0, 0)== NULL){
        printf("%s", mysql_error(&mysql));
        return -1;
    }
   
    char *p;
    int i;
    

    i = 0;
    iTableNum = 0;

    // Divide strTable
    p = strtok(table, "/");
    while (p != NULL){
        pTable[iTableNum++] = p;
        p = strtok(NULL, "/");
    }

   
    if (! strcmp(domain, "none"))
        domain = "";

    url_node *proot = NULL;
    char *url = NULL;
   
    for (i = 0; i < iTableNum; i++)
    {
       
        sprintf(sqlBuf," select DEVID, HOST"
            " FROM %s where ACCTIME between"
            " %u and %u and HOST like '%%%s%%'"
            " and DEVID = %d", pTable[i],
            atol(start), atol(end), domain, atoi(devid));
        if (mysql_query(&mysql, sqlBuf))
        {
            if (1146 == mysql_errno(&mysql))
                continue;
        }

        if ((result = mysql_store_result(&mysql)) == NULL)
            continue;

        while (row = mysql_fetch_row(result))
        {
            url = row[1];
           
            /* establish tree */
            /* if root is null */
            if (proot == NULL)
                proot = create_node(url);
            else    /* else add a node */
                add_node(url, proot);
        }

        /* free result resource */
        mysql_free_result(result);
       
    }

    mysql_close(&mysql);

    return num;
}

url_node
*create_node(char *url)
{
    /* alloc memory for a new node */
    url_node *pNode = (url_node *)malloc(sizeof(url_node));

    strcpy (pNode->url, url);
    pNode->count = 1;
    pNode->pleft = pNode->pright = NULL;
    return pNode;
}

url_node
*add_node(char *url, url_node *pNode)
{
    /* if root node is null and create*/
    if (pNode == NULL)
    {
        pNode = create_node(url);
        return pNode;
    }

    /* if url equal pnode's value */
    if (! strcmp(pNode->url, url))
    {
        ++pNode->count;
        return pNode;
    }

    /* if url is min than pnode left */
    if (strcmp(pNode->url, url) < 0)
    {
        if (pNode->pright == NULL)
        {
            pNode->pright = create_node(url);
            return pNode->pright;
        }else
            return add_node(url, pNode->pright);
    }

    /* if url is max than pnode left */
    if (strcmp(pNode->url, url) > 0)
    {
        if (pNode->pleft == NULL)
        {
            pNode->pleft = create_node(url);
            return pNode->pleft;
        }else
            return add_node(url, pNode->pleft);
    }

}

void freenodes(url_node *pNode)
{
    if (pNode == NULL)
        return;

    if (pNode->pleft != NULL)
        freenodes(pNode->pleft);

    if (pNode->pright != NULL)
        freenodes(pNode->pright);

    freenodes(pNode);
}
搜索更多相关主题的帖子: 二叉树 
2010-12-26 12:08
aufish
Rank: 2
等 级:论坛游民
威 望:1
帖 子:59
专家分:94
注 册:2010-4-22
收藏
得分:0 
顶下
2010-12-26 22:31
bowenzhang
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-12-26
收藏
得分:0 
这里面还有一个输出结点值的算法没有写出来
2010-12-27 10:04
快速回复:用法 二叉树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034427 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved