-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.jl
79 lines (64 loc) · 1.38 KB
/
tree.jl
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
using Random
using Printf
mutable struct TreeNode
key::Int
left::TreeNode
right::TreeNode
TreeNode() = new()
TreeNode(x) = new(x)
TreeNode(x,y,z) = new(x,y,z)
end
function insert(key, n::TreeNode)
if key < n.key
if !isdefined(n,:left)
n.left = TreeNode(key)
else
insert(key, n.left)
end
elseif key > n.key
if !isdefined(n,:right)
n.right = TreeNode(key)
else
insert(key, n.right)
end
end
end
function printTree(n::TreeNode)
if isdefined(n,:left)
printTree(n.left)
end
@printf("node = %d\n", n.key)
if isdefined(n,:right)
printTree(n.right)
end
end
function sumTree(n::TreeNode)
sum = 0
if isdefined(n,:left)
sum += sumTree(n.left)
end
sum += n.key
if isdefined(n,:right)
sum += sumTree(n.right)
end
return sum
end
function tree(temp, n)
root::TreeNode = TreeNode(temp[1])
for i = 2:n
insert(temp[i], root)
end
@printf("\n\nSum of %d numbers = %d\n\n ", n, sumTree(root))
end
function test(temp, n, iters)
for i = 1:iters
@time tree(temp, n)
end
end
# iterations is the number of trees
# tree_size is the number of elements in mb
rng = MersenneTwister(12345)
iterations = parse(Int64, ARGS[1])
tree_size = parse(Int64, ARGS[2]) * 1024 * 1024
temp = rand(rng, Int, tree_size)
test(temp, tree_size, iterations)