用法 二叉树
程序代码:#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);
}







