v

Зеркало из https://github.com/vlang/v
Форк
0
/
tree_of_nodes.v 
27 строк · 573.0 Байт
1
type Tree = Empty | Node
2

3
struct Empty {}
4

5
struct Node {
6
	value int
7
	left  Tree
8
	right Tree
9
}
10

11
// Note: a match expression, infers the type of its result
12
// from the type of the return value in the first branch,
13
// => it needs an explicit int(0) cast here:
14
fn size(tree Tree) int {
15
	return match tree {
16
		Empty { int(0) }
17
		Node { 1 + size(tree.left) + size(tree.right) }
18
	}
19
}
20

21
fn main() {
22
	node1 := Node{30, Empty{}, Empty{}}
23
	node2 := Node{20, Empty{}, Empty{}}
24
	tree := Node{10, node1, node2}
25
	println('tree structure:\n ${tree}')
26
	println('tree size: ${size(tree)}')
27
}
28

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.