首页 > 解决方案 > C中二叉搜索树中的定时器实现

问题描述

我一直在编写一个程序,该程序应该确定该过程花费了多长时间,但是,它失败并始终返回 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;
}

这是程序的一部分,它将在二叉搜索树中找到最大值。(编辑:我添加了额外的代码进行澄清。该程序将让用户输入树的节点,然后相应地重新排列节点,理论上节点数量没有限制。我的主要问题再次是我的实现计时器的时间是正确的吗?还是有其他方法可以做到这一点?)这只是我编写的代码的一部分,我想知道是否有任何替代过程所花费的时间,或者我是否编码错误. 我希望所用的时间是经过的时间。这种方法仅适用于循环吗?

我尝试过类似的东西

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

它返回 0 的开始时间和一个大数字作为结束时间,但是将其转换为秒似乎对我不起作用。

(附加信息:我已经阅读了 time.h 文档,并且似乎 clock() 应该可以工作,我一直在尝试 StackOverflow 上提到的其他方法,但它们似乎都没有工作。是因为我使用了 struct 代替?) 谢谢

标签: calgorithm

解决方案


在搜索并做了很多试验和错误之后,我可以得出结论,所花费的时间太快了,无法衡量。在我的测试阶段,我最多只有 50 个节点。

通过参考这个网站https://aaronjwood.com/articles/binary-search-trees/,我调整了代码,获得有效遍历时间所需的最小节点数量为 1,000,000 个节点。

所以结论可以是,要在 C 中解决这个问题,我们将不得不使用大量节点。


推荐阅读