-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1991.kt
85 lines (71 loc) · 2.09 KB
/
1991.kt
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
import java.io.*
import java.util.*
val bw = BufferedWriter(OutputStreamWriter(System.out))
val br = BufferedReader(InputStreamReader(System.`in`))
data class TreeNode<String>(
var data: String,
var left: TreeNode<String>? = null,
var right: TreeNode<String>? = null
)
class Tree{
var root : TreeNode<String>? = null
fun add(data: String, left:String, right:String){
if(root == null){
root = TreeNode(data)
if(left!=".")
root!!.left = TreeNode(left)
if(right!=".")
root!!.right = TreeNode(right)
}
else
search(root!!, data, left, right)
}
fun search(root:TreeNode<String>, data : String, left: String, right :String){
if(root.data == data){
if(left!=".")
root.left = TreeNode(left)
if(right!=".")
root.right = TreeNode(right)
}else{
if(root.left!=null)
search(root.left!!, data, left, right)
if(root.right!=null)
search(root.right!!,data,left,right)
}
}
fun inOrder(root:TreeNode<String>){
if(root.left!=null)
inOrder(root.left!!)
bw.write(root.data)
if(root.right!=null)
inOrder(root.right!!)
}
fun preOrder(root:TreeNode<String>){
bw.write(root.data)
if(root.left!=null)
preOrder(root.left!!)
if(root.right!=null)
preOrder(root.right!!)
}
fun postOrder(root:TreeNode<String>){
if(root.left!=null)
postOrder(root.left!!)
if(root.right!=null)
postOrder(root.right!!)
bw.write(root.data)
}
}
fun main() {
val n = br.readLine().toInt()
val tree : Tree = Tree()
repeat(n){
val (a,b,c) = br.readLine().split(" ")
tree.add(a,b,c)
}
tree.preOrder(tree.root!!)
bw.write("\n")
tree.inOrder(tree.root!!)
bw.write("\n")
tree.postOrder(tree.root!!)
bw.flush()
}