Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package made
- object Task4 extends App {
- import scala.io.StdIn.{readInt, readLine}
- import scala.collection.mutable.ListBuffer
- class Node() {
- var name: String = ""
- var children: ListBuffer[Node] = new ListBuffer[Node]()
- var increment: Int = 0
- def this(name: String) {
- this()
- this.name = name
- }
- @Override
- override def toString(): String = {
- name + ": " + children.toString
- }
- def add(path: String) = {
- val splitPath = path.split("/")
- addNode(splitPath, 1, this)
- }
- def addNode(splitPath: Array[String], index: Int, node: Node): Unit = {
- var nodeAdded = false
- for (child <- node.children) {
- if (child.name == splitPath(index)) {
- addNode(splitPath, index + 1, child)
- nodeAdded = true
- }
- }
- if (!nodeAdded) {
- var addingNode = new Node(splitPath(index))
- node.children += addingNode
- if (index != splitPath.length - 1) {
- addNode(splitPath, index + 1, addingNode)
- }
- }
- }
- def calc: Int = {
- deleteFile(this)
- increment
- }
- def deleteFile(node: Node): Unit = {
- if (node.children.nonEmpty) {
- increment += 1
- for (child <- node.children) {
- if (child.children.nonEmpty) {
- deleteFile(child)
- increment += 1
- }
- }
- }
- }
- }
- // val n = readInt
- // var paths = List[String]()
- // for (_ <- 0 until n) {
- // paths = readLine :: paths
- // }
- val n = 2
- val paths = List[String]("/out/qq/ww", "/out/qq/ee")
- val test = new Node()
- for (path <- paths) {
- test.add(path)
- }
- println(test.calc)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement