zoukankan      html  css  js  c++  java
  • swift算法手记-10

    http://blog.csdn.net/myhaspl
    
        private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl
    
            //查找结点http://blog.csdn.net/myhaspl
    
            if let mysltop = slinktop{
                var mynode:skipLinkNode=mysltop
                while true{
                    while true{
                        if let nextnd = mynode.nextnode {
                           let nodeval = nextnd.ndval
                           if  nodeval < val{
                               mynode=nextnd
                               continue
                            }
                            if nodeval == val{
                               return true
                            }
                        }
                        break
                    }
                    if mynode.downnode == nil{
                       return false
                    }
                    else{
                        mynode = mynode.downnode!
                    }
                }
            }
            else{
                return false
            }
    
        }
        ....
        ....
    ....
    
       
        
        private func deletenode(val:Int){
            if let mysltop=slinktop{
                var mynode:skipLinkNode=mysltop
                while true{
                    while true{
                        if let nextnd = mynode.nextnode {
                            let nodeval = nextnd.ndval
                            if  nodeval < val{
                                mynode=nextnd
                                continue
                            }
                            if nodeval == val{
                                //delete node from the level
                                mynode.nextnode=nextnd.nextnode
                            }
                        }
                        break
                    }
                    if mynode.downnode == nil{
                        //最底层http://blog.csdn.net/myhaspl
    
                        break
                    }
                    else{
                        mynode = mynode.downnode!
                    }
                }
            }
        }
    
    
        private func insertnode(val:Int){
            //插入结点
            let insertlv=getinsertlv()
            let currtop=currlist(insertlv)
            var mynode:skipLinkNode = currtop
    
            var isfind:Bool=false
            var searchnodes=[(skipLinkNode,skipLinkNode)]()
            
            while true{
                while let ntnode=mynode.nextnode{
                    if ntnode.ndval < val {
                        mynode = ntnode
                    }
                    else if ntnode.ndval == val {
                        isfind=true
                        searchnodes.append((ntnode,ntnode.nextnode!))
                        break
                    }
                    else{
                        searchnodes.append((mynode,ntnode))
                        break
                    }
                }
                if let dnnode=mynode.downnode {
                    mynode=dnnode
                }
                else{
                    break
                }
            }
    
            
            var newnd:skipLinkNode?
            var upnd:skipLinkNode?

    var dnnd:skipLinkNode? var prend:skipLinkNode var ntnd:skipLinkNode if !isfind { for nodes in searchnodes{ (prend,ntnd)=nodes upnd=newnd newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val) if upnd != nil{ upnd!.downnode=newnd } else{ dnnd = newnd! } } if insertlv>slinklevel { addnewlevel(val,dnnode: dnnd!) } } else{ let nodelist=searchnodes.last (prend,ntnd)=nodelist! newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val) } } private func slinkstatus()->String{ var mystatus:String="" var nownode:skipLinkNode var i=slinklevel while i>=0{ nownode=slist[i] mystatus+="||top=>" while true{ if let ntnode=nownode.nextnode { if ntnode.ndtype != ntype.end { mystatus+=String(ntnode.ndval)+"--" } else{ mystatus+="==>end||" } nownode=ntnode } else{ break } } mystatus += " " i-=1 } return mystatus }

    本博客全部内容是原创。假设转载请注明来源

    http://blog.csdn.net/myhaspl/



    跳跃链表是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作须要O(log n)平均时间),而且对并发算法友好。
    基本上,跳跃列表是对有序的链表添加上附加的前进链接,添加是以随机化的方式进行的,所以在列表中的查找能够高速的跳过部分列表(因此得名)。全部操作都以对数随机化的时间进行。

    跳跃列表是按层建造的。

    底层是一个普通的有序链表。每一个更高层都充当以下列表的"高速跑道"。这里在层 i 中的元素按某个固定的概率 p 出如今层 i+1 中。平均起来,每一个元素都在 1/(1-p) 个列表中出现,而最高层的元素(一般是在跳跃列表前端的一个特殊的头元素)在 O(log1/pn) 个列表中出现。

    1 - - - - - - 4 - - - 6
    1 - - - 3 - 4 - - - 6 - - - - - - 9
    1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10
    结构实例结构实例
    要查找一个目标元素。起步于头元素和顶层列表。并沿着每一个链表搜索。直到到达小于或的等于目标的最后一个元素。

    通过跟踪起自目标直到到达在更高列表中出现的元素的反向查找路径,在每一个链表中预期的步数显而易见是 1/p。所以查找的整体代价是 O(log1/p n / p)。当p 是常数时是 O(log n)。通过选择不同 p 值。就能够在查找代价和存储代价之间作出权衡。

    这里元素不多,体现不出优势,假设元素足够多,这样的索引结构就能体现出优势来了。


  • 相关阅读:
    C++ 中int,char,string,CString类型转换
    《Regularized Robust Coding for Face Recognition》
    备份:一个Python最简单的接口自动化框架
    python自动化初始页面踩坑指南
    appium连接夜神浏览器,踩坑指南。
    sublime python环境配置
    appium+夜神模拟器
    python学习随笔
    xampp+discuz 安装踩坑后总结
    XAMPP 安装时 MySQL 无法启动,且提示端口占用。
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7258982.html
Copyright © 2011-2022 走看看