简体   繁体   English

C语言中二进制搜索树中的计时器实现

[英]Timer Implementation in Binary Search Tree in C

I have been writing a program that is supposed to determine how long that the process has taken, however, it fails and always returns a value of 0.0000 seconds. 我一直在编写一个程序,该程序应该确定该过程花费了多长时间,但是它失败了,并且始终返回0.0000秒的值。

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <time.h>

struct node
{
        int data;
        struct node *left;
        struct node *right;
};
struct node *tree;
struct node *InsertElement(struct node *, int); //declaration//
struct node *findLargestElement(struct node *);
void create_tree(struct node *);

void create_tree(struct node *tree)
{
        tree=NULL;  //resets the tree//
}

struct node *findLargestElement(struct node *tree)
{
    if((tree==NULL)|| (tree->right==NULL))
        return tree;
    else
        return findLargestElement(tree->right);
}

   struct node *InsertElement(struct node *tree, int val)
{
    struct node *ptr, *nodeptr, *parentptr;
    ptr = (struct node*)malloc(sizeof(struct node)); //memory allocation//
    ptr->data = val;
    ptr->left = NULL;
    ptr->right = NULL;
    if(tree==NULL) //for root node//
    {
        tree=ptr;
        tree->left=NULL;
        tree->right=NULL;
    }
    else
    {
        parentptr=NULL;
        nodeptr=tree;
        while(nodeptr!=NULL)
        {
        parentptr=nodeptr;
        if(val<nodeptr->data)
        nodeptr=nodeptr->left; //if value is less than root go left//
        else
        nodeptr = nodeptr->right;//if more go right//
        }
        if(val<parentptr->data)//if less than parent go left//
        parentptr->left = ptr;
        else
        parentptr->right = ptr;
    }
    return tree;
}

int main()
{
    int option, val;
    struct node *ptr;
    clock_t begin, end;
    create_tree(tree);

         do
    {
        printf("\n\n\n\t\t*****Main Menu****\n\n");
        printf("\t\t 1. Add new nodes: \n");
        printf("\t\t 2. Find the largest element\n");
        printf("\t\t 11. Exit\n");
        printf("Enter your option : ");
        scanf("%d", &option);

        switch(option)
        {
            case 1:   printf("\nEnter the value of the new node:");
                        scanf("%d", &val);
                        tree= InsertElement(tree,val);
                        break;

            case 2:     begin=clock();  //timer begin//
                        ptr = findLargestElement(tree);
                        end=clock();   //timer ends//
                        double time_spent=(double)(end-begin)/CLOCKS_PER_SEC; //time elapsed//
                        printf("\nThe largest element is:%d\n", ptr->data);
                        printf("The  time taken is %f  end time is", time_spent);
                        break;

        }
    }while(option!=11);

    return 0;
}

This is part of a program which will find the largest value in a binary search tree. 这是程序的一部分,该程序将在二进制搜索树中找到最大值。 (EDIT: I have added additional code for clarification. This program would have the user input the nodes for the tree and then rearrange the nodes accordingly, there is theoretically no limit for a number of nodes. My main question is once again is my implementation of the timer is correct? or is there another way to do it?) This is just part of the code that I have written and I would like to know whether there are any alternatives to time the process has taken or did I code it wrongly. (编辑:我添加了其他代码来进行澄清。该程序将让用户输入树的节点,然后相应地重新排列节点,理论上节点数没有限制。我的主要问题还是我的实现计时器是正确的吗?还是有另一种方法呢?)这只是我编写的代码的一部分,我想知道是否有其他方法可以替代过程所花费的时间,或者我是否编码错误? 。 I would like the time taken to be the elapsed time. 我希望所花费的时间是经过的时间。 Is this method only viable for loops only? 这种方法仅适用于循环吗?

I have tried something like 我已经尝试过类似的东西

begin=clock();
ptr = findLargestElement(tree);
end=clock();
printf("The  start time %f and the end time is %f", begin,end)

it returns a start time of 0 and a large number as the end time, however converting it to seconds does not seem to work for me. 它返回的开始时间为0,较大的数字作为结束时间,但是将其转换为秒似乎对我不起作用。

(Additional info: I have gone through the time.h documentation and it seems like the clock() should work, I have been trying other methods mentioned on StackOverflow but none of them seem to be working. Is it because I use a struct instead?) Thanks (其他信息:我已经阅读了time.h文档,并且似乎clock()应该可以工作,我一直在尝试在StackOverflow上提到的其他方法,但是它们似乎都不起作用。是因为我改用了struct ?) 谢谢

The type clock_t is not necessarily double -- it could be an integer type. clock_t类型不一定是double类型-它可以是整数类型。

One approach is 一种方法是

double begin = ((double )clock()) / CLOCKS_PER_SEC;
ptr = findLargestElement(tree);
double end = ((double )clock()) / CLOCKS_PER_SEC;
printf("The time taken is %f\n", end - begin);

Note that clock() is converted to double before the divide to preserve information in case clock_t is an integer type. 请注意,在clock_t为整数类型的情况下, clock()在除法之前会转换为double以保留信息。

After searching and doing a lot of trial and error, I can conclude that the time taken is just too fast to be measured. 经过搜索并进行了大量的试验和错误后,我可以得出结论,所花费的时间太快了,无法测量。 During my testing phase, I had only a maximum of 50 nodes. 在测试阶段,我最多只有50个节点。

By referring to this website https://aaronjwood.com/articles/binary-search-trees/ , I tweaked the code and the minimum amount of nodes that I needed to get a valid traversal time was 1,000,000 nodes. 通过访问该网站https://aaronjwood.com/articles/binary-search-trees/ ,我对代码进行了调整,获得有效遍历时间所需的最小节点数为1,000,000个节点。

So the conclusion can be, to solve this problem in C, we would have to use a large number of nodes. 因此可以得出结论,要解决C中的这个问题,我们将不得不使用大量节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM