forked from MuhammadZain01/Html-parser-with-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser_Tree.kt
37 lines (37 loc) · 1.07 KB
/
Parser_Tree.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
class TreeNode(var tag: String, var attributes: MutableMap<String,String> = mutableMapOf()) {
var parent: TreeNode? = null
private set
var children: MutableList<TreeNode> = mutableListOf()
private set
var depth = 0
private set
var index = 0
private set
var lowestChildIndex = 0
private set
fun addChild(tag: String, attributes: MutableMap<String, String>) {
val childNode = TreeNode(tag, attributes)
childNode.parent = this
childNode.index = lowestChildIndex
lowestChildIndex++
childNode.depth = depth + 1
children.add(childNode)
}
}
class Tree {
var root: TreeNode? = null
fun inOrderTraversal() {
inOrderTraversalHelper(root)
}
private fun inOrderTraversalHelper(currentNode: TreeNode?) {
if (currentNode == null) {
return
}
for (childNode in currentNode.children) {
if (childNode != null) {
print(childNode.tag)
}
inOrderTraversalHelper(childNode)
}
}
}