mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: use Empty instead of Leaf in tree_of_nodes.v
This commit is contained in:
parent
944bb294e3
commit
0da40c4ea9
@ -1,6 +1,6 @@
|
||||
type Tree = Leaf | Node
|
||||
type Tree = Empty | Node
|
||||
|
||||
struct Leaf {}
|
||||
struct Empty {}
|
||||
|
||||
struct Node {
|
||||
value int
|
||||
@ -13,14 +13,14 @@ struct Node {
|
||||
// => it needs an explicit int(0) cast here:
|
||||
fn size(tree Tree) int {
|
||||
return match tree {
|
||||
Leaf { int(0) }
|
||||
Empty { int(0) }
|
||||
Node { 1 + size(tree.left) + size(tree.right) }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
node1 := Node{30, Leaf{}, Leaf{}}
|
||||
node2 := Node{20, Leaf{}, Leaf{}}
|
||||
node1 := Node{30, Empty{}, Empty{}}
|
||||
node2 := Node{20, Empty{}, Empty{}}
|
||||
tree := Node{10, node1, node2}
|
||||
println('tree structure:\n $tree')
|
||||
println('tree size: ${size(tree)}')
|
||||
|
Loading…
Reference in New Issue
Block a user