-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
103 lines (92 loc) · 2.15 KB
/
main.cpp
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
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include "AVL_Tree.hpp"
#include <cstring>
#include <random>
using namespace std;
AVL_Tree *tree;
unsigned int count;
bool new_line_flag = false;
static void cb(Node* node)
{
if(new_line_flag)
{
cout << "\r\n";
count = tree->get_height();
new_line_flag = false;
}
while(count > node->get_height() - 1)
{
cout << "\t";
count--;
}
cout << node->get_key() << "\t";
if(node->get_height() == 1)
{
new_line_flag = true;
}
else
{
count--;
}
}
int main(int argc, char** argv)
{
tree = new AVL_Tree();
int number = 0;
int max = 0;
srand(time(NULL));
number = 10;
max = rand() % 1000;
for(int i = 1; i < argc; i++)
{
if(memcmp(argv[i],"-",1) == 0)
{
if(strcmp(argv[i],"--help") == 0)
{
cout << "AVL Tree app." << endl;
cout << "Usage:" << endl;
cout << " --help: this message" << endl;
cout << " --number <uint> : number of nodes. Default 10" << endl;
cout << " --max <uint> : max of random keys. Default 1000" << endl;
return 0;
}
if(strcmp(argv[i],"--number") == 0)
{
if(argc > i)
{
number = atoi(argv[i+1]);
}
else
{
return -1;
}
}
else if(strcmp(argv[i],"--max") == 0)
{
if(argc > i)
{
max = atoi(argv[i + 1]);
}
else
{
return -1;
}
}
else
{
cout << "Wrong argument. Try --help" << endl;
return 0;
}
}
}
tree->set_callback(cb);
for(int i = 0; i < number ; i++)
{
tree->insert(rand() % max);
}
count = tree->get_height();
tree->traverse();
delete tree;
cout << "\r\n";
return 0;
}