本文主要是介绍swift算法手记-9,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在计算机科学中,trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。Trie这个术语来自于retrieval。根据词源学,trie的发明者Edward Fredkin把它读作英语发音:/ˈtriː/ "tree"。[1][2]但是,其他作者把它读作英语发音:/ˈtraɪ/ "try"。[1][2][3]在图示中,键标注在节点中,值标注在节点之下。每一个完整的英文单词对应一个特定的整数。Trie可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的。键不需要被显式地保存在节点中。图示中标注出完整的单词,只是为了演示trie的原理。trie中的键通常是字符串,但也可以是其它的结构。trie的算法可以很容易地修改为处理其它结构的有序序列,比如一串数字或者形状的排列。比如,bitwise trie中的键是一串位元,可以用于表示整数或者内存地址。//http://blog.csdn.net/myhaspl/
class ViewController: NSViewController {private let trieroot:TrieNode = TrieNode (str: " ",nstate: NoteState.Root)
//http://blog.csdn.net/myhaspl/ override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}
//http://blog.csdn.net/myhaspl/override var representedObject: AnyObject? {didSet {// Update the view, if already loaded.}}//http://blog.csdn.net/myhaspl/ private func createTrieNode(current:TrieNode,mystr:String)->TrieNode{let mynode:TrieNode=TrieNode(str: mystr,nstate: NoteState.NoneTerminal)current.link[mystr]=mynodereturn mynode}//http://blog.csdn.net/myhaspl/ private func trieInput(current:TrieNode,str:String,isfind:Bool)->TrieNode?{if let nextstate=current.link[str]{return nextstate}else if isfind{return nil}else{return createTrieNode(current,mystr:str)}}private func readstr(word:String){var currnode:TrieNode=trierootvar inputstr:String=""for mychar in word {inputstr.append(mychar)currnode = trieInput(currnode,str: inputstr,isfind: false)!}currnode.state=NoteState.Terminal}
//http://blog.csdn.net/myhaspl/private func findstr(word:String)->Bool{var mynode:TrieNode=trierootvar inputstr:String=""for mychar in word {inputstr.append(mychar)if let nextnode = trieInput(mynode,str: inputstr,isfind: true){mynode=nextnode}else{return false}}if mynode.state==NoteState.Terminal{return true}else{return false}}
//http://blog.csdn.net/myhaspl/ @IBOutlet weak var mywords: NSTextField!@IBOutlet weak var inputtxt: NSTextField!@IBAction func findword(sender: AnyObject) {let findw:Bool=findstr(inputtxt.stringValue)let messagebox:NSAlert=NSAlert()if findw==true{messagebox.messageText="找到"+inputtxt.stringValue}else{messagebox.messageText="没找到"+inputtxt.stringValue}messagebox.alertStyle=NSAlertStyle.InformationalAlertStylemessagebox.runModal()}@IBAction func createword(sender: AnyObject) {readstr(inputtxt.stringValue)mywords.stringValue+=inputtxt.stringValuemywords.stringValue+=" "}}
//http://blog.csdn.net/myhaspl/
class ViewController: NSViewController {private let trieroot:TrieNode = TrieNode (str: " ",nstate: NoteState.Root)
//http://blog.csdn.net/myhaspl/ override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}
//http://blog.csdn.net/myhaspl/override var representedObject: AnyObject? {didSet {// Update the view, if already loaded.}}//http://blog.csdn.net/myhaspl/ private func createTrieNode(current:TrieNode,mystr:String)->TrieNode{let mynode:TrieNode=TrieNode(str: mystr,nstate: NoteState.NoneTerminal)current.link[mystr]=mynodereturn mynode}//http://blog.csdn.net/myhaspl/ private func trieInput(current:TrieNode,str:String,isfind:Bool)->TrieNode?{if let nextstate=current.link[str]{return nextstate}else if isfind{return nil}else{return createTrieNode(current,mystr:str)}}private func readstr(word:String){var currnode:TrieNode=trierootvar inputstr:String=""for mychar in word {inputstr.append(mychar)currnode = trieInput(currnode,str: inputstr,isfind: false)!}currnode.state=NoteState.Terminal}
//http://blog.csdn.net/myhaspl/private func findstr(word:String)->Bool{var mynode:TrieNode=trierootvar inputstr:String=""for mychar in word {inputstr.append(mychar)if let nextnode = trieInput(mynode,str: inputstr,isfind: true){mynode=nextnode}else{return false}}if mynode.state==NoteState.Terminal{return true}else{return false}}
//http://blog.csdn.net/myhaspl/ @IBOutlet weak var mywords: NSTextField!@IBOutlet weak var inputtxt: NSTextField!@IBAction func findword(sender: AnyObject) {let findw:Bool=findstr(inputtxt.stringValue)let messagebox:NSAlert=NSAlert()if findw==true{messagebox.messageText="找到"+inputtxt.stringValue}else{messagebox.messageText="没找到"+inputtxt.stringValue}messagebox.alertStyle=NSAlertStyle.InformationalAlertStylemessagebox.runModal()}@IBAction func createword(sender: AnyObject) {readstr(inputtxt.stringValue)mywords.stringValue+=inputtxt.stringValuemywords.stringValue+=" "}}
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
这篇关于swift算法手记-9的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!