这个概念是: 你已经给出了一系列字符串,如:
#输入将始终采用以下格式“x1> x2> x3 ...> xn =整数”否则输入无效#
private let input = [
"a>b>c=1",
"a>b>c=2",
"d>c=3",
"f>e=10",
"a>b=20", // Invalid, Node(b) has Child, cannot insert the value
"a>b>c>d=11", // Invalid , Node(c) has value, but trying to insert child, invalid case
"a>a>a=100"
]
(1)。让我们考虑第一个输入:“a> b> c = 1”,它由2个部分组成,基于“=”运算符“a> b> c“和”1“。
第一部分用于树层次结构,其中 x> y表示x是y的父项,值“1”将是最后一个节点的子项。它创建第一次像树一样的树,因为树在开始时是空的。
( Root ) --> ("a") --> ("b") --> ("c") --> [1]
(2)考虑第二个输入:“a> b> c = 2”。将输入分为2个部分“a> b> c”和“2” “基于”=“。在处理“a> b> c”时,它只是遍历,因为节点(“a”),节点(“b”),节点(“c”)处于正确的层次结构中并通过附加值= 2更新值节点所以值([1,2])。树看起来像:
( Root ) --> ("a") --> ("b") --> ("c") --> [1,2]
(3)考虑到“d> c = 3”,树看起来像:
( Root ) --> ("a") --> ("b") --> ("c") --> [1,2]
|
--> ("d") --> ("c") --> [3]
(4)考虑“a> b = 20”输入会产生 ERROR ,因为节点(“b”)有一个子节点(“c”)因此您不能为节点(“b”)插入值([20]),因此,请勿处理此输入。 (树中没有修改)
(5)考虑“a> b> c> d = 11”输入,产生 ERROR 因为节点(“c”) )有一个Value([1,2])子节点,你不能添加一个节点(“d”)作为节点的子节点(“c”),所以,不要处理这个输入。 (树中没有修改)
考虑到上述输入,你必须像下面那样构建Trie。
{
"a" : { "b" : { "c" : [1,2] },
"a" : { "a" : [100] }
},
"d" : { "c" : [3] },
"f" : { "e" : [10] }
}
答案 0 :(得分:-1)
import Foundation
// Element must have either value or childDictionary, not at the same time
class Element {
var value : [Int]?
var childDictionary : Dictionary<String, Element>?
init(value : [Int]?, childKey : String?, childValue : Element? ) {
self.value = value
self.childDictionary = nil
if let childKey = childKey, let childValue = childValue {
var dict = Dictionary<String, Element>()
dict[childKey] = childValue
childDictionary = dict
}
}
}
class JsonCreator {
private var root : Element?
private let jsonInput = [
"a>b>c=1",
"a>b>c=2",
"d>c=3",
"f>e=10",
"a>b=20", // Invalid, Node(b) has Child, cannot insert the value
"a>b>c>d=11" // Invalid , Node(c) has value, but trying to insert child, invalid case
]
public func create() {
root = Element(value: nil, childKey: nil, childValue: nil) // root one
createJSON(input: jsonInput)
print("\n\n Output \n\n")
printJson(root: root)
}
private func createJSON(input : [String]) {
let rootJson = root
for each in input {
print("\n---------------------- New Input ----------------------\n")
// split the string into 2 parts
let parts = each.components(separatedBy: "=")
if parts.count > 1 { // go through the hierarchy
let literals = parts[0].filter{ $0 != ">" }
print(literals)
var traversal = rootJson
var char : Character!
var hasError = false
// literals
for i in 0..<literals.count {
char = literals[literals.index(literals.startIndex, offsetBy: i)]
print("Processing : \(char!)")
if let _ = traversal?.value {
print("ERROR : Node has value, but trying to insert child, invalid case")
hasError = true
break
} else if traversal?.childDictionary == nil {
print("Child is Empty : Do Insertion")
let node = Element(value: nil, childKey:nil , childValue: nil)
var dict = Dictionary<String, Element>()
dict[String(char)] = node
traversal?.childDictionary = dict
traversal = node // update traversal
} else if let isChildPresent = traversal?.childDictionary?.keys.contains(String(char)), !isChildPresent {
print("Child is not Empty, but not present in the hierary, Do Insertion")
let node = Element(value: nil, childKey:nil , childValue: nil)
traversal?.childDictionary?[String(char)] = node
traversal = node // update traversal
} else {
print("Child is not Empty, but present in the hierary: NO Insertion, only traverse ")
if let updateTraversal = traversal?.childDictionary?[String(char)] {
traversal = updateTraversal // update traversal
}
}
}
// Value
if let _ = char, let val = Int(parts[1]), !hasError {
print("value : \(val)")
if let _ = traversal?.childDictionary {
print("ERROR : Node has Child, cannot insert the value")
} else {
print("Append the value")
if let _ = traversal?.value {
traversal?.value?.append(val)
} else {
traversal?.value = [val]
}
}
}
root = rootJson // update root for every input
}
}
}
private func printJson(root : Element?) {
if let childDictionary = root?.childDictionary {
for each in childDictionary {
print("{ \(each.key) : ", terminator : "")
printJson(root: each.value)
print("}")
}
}
if let values = root?.value {
print("\(values)")
}
}
}
let jsonCreateObj = JsonCreator()
jsonCreateObj.create()
---------------------- New Input ----------------------
abc
Processing : a
Child is Empty : Do Insertion
Processing : b
Child is Empty : Do Insertion
Processing : c
Child is Empty : Do Insertion
value : 1
Append the value
---------------------- New Input ----------------------
abc
Processing : a
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : b
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : c
Child is not Empty, but present in the hierary: NO Insertion, only traverse
value : 2
Append the value
---------------------- New Input ----------------------
dc
Processing : d
Child is not Empty, but not present in the hierary, Do Insertion
Processing : c
Child is Empty : Do Insertion
value : 3
Append the value
---------------------- New Input ----------------------
fe
Processing : f
Child is not Empty, but not present in the hierary, Do Insertion
Processing : e
Child is Empty : Do Insertion
value : 10
Append the value
---------------------- New Input ----------------------
ab
Processing : a
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : b
Child is not Empty, but present in the hierary: NO Insertion, only traverse
value : 20
ERROR : Node has Child, cannot insert the value
---------------------- New Input ----------------------
abcd
Processing : a
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : b
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : c
Child is not Empty, but present in the hierary: NO Insertion, only traverse
Processing : d
ERROR : Node has value, but trying to insert child, invalid case
Output
{ d : { c : [3]
}
}
{ a : { b : { c : [1, 2]
}
}
}
{ f : { e : [10]
}
}