c - 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 代替?) 谢谢
解决方案
在搜索并做了很多试验和错误之后,我可以得出结论,所花费的时间太快了,无法衡量。在我的测试阶段,我最多只有 50 个节点。
通过参考这个网站https://aaronjwood.com/articles/binary-search-trees/,我调整了代码,获得有效遍历时间所需的最小节点数量为 1,000,000 个节点。
所以结论可以是,要在 C 中解决这个问题,我们将不得不使用大量节点。
推荐阅读
- angular - Angular - 如何在下拉菜单中选择值?
- validation - 为什么当数据中缺少字段时,“notEmpty”规则不适用?
- ejabberd - 我想要 Erling 代码来确定访客昵称的长度
- google-sheets - 过滤从站点导入的日期
- rest - Wildfly 10:ear、ejbs 和 restful-services
- react-native - 使用 Yarn 工作区/nohoist 时,如何控制 Yarn 为依赖项的 peerDependency 选择哪个版本?
- postgresql - Postgres - 多个数据库的 RDS 副本到由模式分隔的单个数据库(数据库名称)
- java - servlet appServlet 的消息 Servlet.init() 抛出异常
- php - 如何将数组传递给 PHP 中的函数
- reactjs - 我收到有关 React 中目标库的错误