-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.tiny
More file actions
175 lines (142 loc) · 4.78 KB
/
Copy pathtree.tiny
File metadata and controls
175 lines (142 loc) · 4.78 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
####################################################################
#
# Small example script showing Tiny working with trees and methods
# in dictionaries.
#
####################################################################
import 'utils'
import 'math'
# A function that returns a dictionary from an operator to
# its implementation
fn opMap -> {
"+" : {x, y -> x + y }
"-" : {x, y -> x - y }
"*" : {x, y -> x * y }
"/" : {x, y -> x / y }
"%" : {x, y -> x % y }
}
info("Function that constructs a tree node")
# Construct a binary tree node. This function adds
# methods to the hashtable
fn tree val, left, right -> {
# The value of this node
val : val
# The left subtree
left : left
# The right subtree
right : right
# Method to print the tree using indentation to reflect depth.
PrintTree : { offset ->
offset ?= 0
match this.left
| [<IDictionary>] -> this.left.PrintTree(offset+2)
| -> println(" " * (offset+2) + this.left)
match this.right
| [<IDictionary>] -> this.right.PrintTree(offset+2)
| -> println(" " * (offset+2) + this.right)
println(" " * offset + this.Val)
}
# method to recursively evaluate a tree
Eval : {
v1 = match this.left
| [<IDictionary>] -> this.left.eval()
| -> AsNumber(this.left)
v2 = match this.right
| [<IDictionary>] -> this.right.eval()
| -> AsNumber(this.right)
opMap()[this.val].invoke([v1, v2])
}
# Method to compute the depth of this tree
Depth : {
ld = match this.left
| [<IDIctionary>] -> this.Left.Depth()
| null -> 0
| -> 1
rd = match this.right
| [<IDIctionary>] -> this.right.Depth()
| null -> 0
| -> 1
1 + utils.max(ld, rd)
}
}
# Create a tree using the constructor function which
# adds methods to the nodes
testtree1 = tree('+',
tree('*',
tree('-', 10, 2),
3
),
tree('/',
4,
tree('%',
5 ,
tree('+',
tree('*',
2,
3
),
14
)
)
)
)
# Create a tree directly using object literal notation
testtree2 = {
val : '+'
left : {
val : '*'
left : {
val : '-'
left : 10
right: 2
}
right: 3
}
right: {
val : '/'
left : {
val: '+'
left: 10
right: 5
}
right: {
val : '%'
left : 5
right: 6
}
}
}
undef printTree
def printTree {:: left right val ::} depth {
printTree(left, depth+2)
printTree(right, depth+2)
println("{0}{1}", ' ' * depth, val)
}
def printTree value depth {
println("{0}{1}", ' ' * depth, value)
}
def printTree treeToPrint -> printTree(treeToPrint, 0)
# A function set to evaluate the tree; uses pattern-valued parameters
undef TreeEval
def TreeEval {:: left right val:'+' ::} -> TreeEval(left) + TreeEval(right)
def TreeEval {:: left right val:'-' ::} -> TreeEval(left) - TreeEval(right)
def TreeEval {:: left right val:'*' ::} -> TreeEval(left) * TreeEval(right)
def TreeEval {:: left right val:'/' ::} -> TreeEval(left) / TreeEval(right)
def TreeEval {:: left right val:'%' ::} -> TreeEval(left) % TreeEval(right)
def TreeEval treeToEval -> treeToEval # bugbug as [<double>]
info('Print the tree using the printtree() function:')
printtree(testtree1)
info('Print the tree using the printtree() method:')
testtree1.PrintTree()
info('Evaluating tree using function results in: {0}', treeeval(testtree1))
info('Evaluating tree using method results in: {0} ', testtree1.Eval())
info('Print tree2 using the printtree() function:')
printtree(testtree2)
info('Evaluating tree2: {0}', treeeval(testtree2))
# Function set to compute the depth of a tree.
undef depth
def depth {:: left right val ::} -> 1 + math.max(depth(left), depth(right))
def depth _ -> 1
info("Depth of tree using depth method is {0}", testtree1.depth())
info("Depth of tree using depth function is {0}", depth(testtree1))
info("Depth of tree2 using depth function is {0}", depth(testtree2))