-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_traversal.swift
52 lines (43 loc) · 1.16 KB
/
binary_tree_traversal.swift
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
class Node {
let value: Int
var left: Node?
var right: Node?
init(_ value: Int) {
self.value = value
}
}
let root = Node(28)
root.left = Node(24)
root.left?.right = Node(25)
root.left?.left = Node(19)
root.left?.left?.left = Node(15)
root.left?.left?.right = Node(21)
root.right = Node(47)
root.right?.left = Node(40)
root.right?.right = Node(57)
root.right?.right?.left = Node(55)
root.right?.right?.right = Node(59)
func inOrderTraversal(node: Node?) {
guard let node = node else { return }
inOrderTraversal(node: node.left)
print(node.value, terminator: " ")
inOrderTraversal(node: node.right)
}
func preOrderTraversal(node: Node?) {
guard let node = node else { return }
print(node.value, terminator: " ")
preOrderTraversal(node: node.left)
preOrderTraversal(node: node.right)
}
func postOrderTraversal(node: Node?) {
guard let node = node else { return }
postOrderTraversal(node: node.left)
postOrderTraversal(node: node.right)
print(node.value, terminator: " ")
}
print("In-Order Traversal")
inOrderTraversal(node: root)
print("\nPre-Order Traversal")
preOrderTraversal(node: root)
print("\nPost-Order Traversal")
postOrderTraversal(node: root)