-
Notifications
You must be signed in to change notification settings - Fork 16
/
BinaryTree.swift
42 lines (36 loc) · 885 Bytes
/
BinaryTree.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
//BinaryTree
//Sayed Mahmudul Alam
class Node {
var value: Int?
var leftChild: Node?
var rightChild: Node?
init(value: Int) {
self.value = value
leftChild = nil
rightChild = nil
}
}
class BinaryTree {
private var root: Node?
init(rootValue: Int) {
root = Node(value: rootValue)
}
func insertLeft(value: Int) {
var temp = Node(value: value)
if root!.leftChild == nil {
root!.leftChild = temp
} else {
temp.leftChild = root!.leftChild
root!.leftChild = temp
}
}
func insertRight(value: Int) {
var temp = Node(value: value)
if root!.rightChild == nil {
root!.rightChild = temp
} else {
temp.rightChild = root!.rightChild
root!.rightChild = temp
}
}
}