-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
92 lines (80 loc) · 2.47 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <vld.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binary_tree.h"
#include "stopwatch.h"
// Right Rotation
static const NODE_KEY KEY_VALUES[] = { 0xa, 0xf, 0x5, 0xb, 0x7, 0x3, 0x12 };
static const NODE_KEY INVALID_ITEM = 0x01;
static void* DATA_VALUE = (void*)0x11;
STOPWATCH_HANDLE g_timer_handle;
int insert_items(BINARY_TREE_HANDLE handle, const NODE_KEY insert_group[], size_t count)
{
int result = 0;
for (size_t index = 0; index < count; index++)
{
if (binary_tree_insert(handle, insert_group[index], DATA_VALUE) != 0)
{
(void)printf("FAILURE: Inserting item %d\r\n", (int)index);
result = __LINE__;
break;
}
}
(void)printf(" Tree Items %d\r\n", (int)binary_tree_item_count(handle) );
(void)printf("Tree Height %d\r\n", (int)binary_tree_height(handle));
return result;
}
int find_item(BINARY_TREE_HANDLE handle, char item_to_find)
{
int result;
if (binary_tree_find(handle, item_to_find) == DATA_VALUE)
{
(void)printf("The item has been found\r\n");
result = 0;
}
else
{
(void)printf("The item was not found\r\n");
result = 1;
}
return result;
}
int main(void)
{
BINARY_TREE_HANDLE handle = binary_tree_create();
if (handle == NULL)
{
(void)printf("FAILURE: creating binary tree\r\n");
}
else if ((g_timer_handle = stopwatch_create()) == NULL)
{
binary_tree_destroy(handle);
(void)printf("FAILURE: creating stopwatch\r\n");
}
else
{
size_t count = sizeof(KEY_VALUES);
if (insert_items(handle, KEY_VALUES, count) == 0)
{
//binary_tree_print(handle);
//binary_tree_construct_visual(handle);
// Find a valid item
if (find_item(handle, KEY_VALUES[1]) != 0)
{
(void)printf("FAILURE: Looking for a valid item has failed\r\n");
}
// Look for an item not there
if (find_item(handle, INVALID_ITEM) == 0)
{
(void)printf("FAILURE: Found an item that's not there, what is going on?\r\n");
}
}
binary_tree_destroy(handle);
stopwatch_destroy(g_timer_handle);
}
(void)printf("Press any key to exit:");
(void)getchar();
}