Modul:Benutzer:Thoken/Algor2

Die Dokumentation für dieses Modul kann unter Modul:Benutzer:Thoken/Algor2/Doku erstellt werden

-- binary search tree
-- konsole> tn=p.treenode(); tn:insert(3,"c"); tn:insert(5,"e"); tn:insert(2,"b"); tn:insert(1,"a"); tn:insert(4,"d");
-- konsole> =tn:search(4)
--   returning  "d"
-- konsole> =tn:list()
--   returning  ";a:1;b:2;c:3;d:4;e:5"
-- konsole> =tn:show()
--   returning         a
--                  b
--               c
--                     d
--                  e
p={}
function p.treenode() -- using code from http://www.mail-archive.com/[email protected]/msg00211.html
  return {
    search = function(self, key)
        if self.key == nil then return end -- leafnode case
        if key == self.key then return self.value end -- key match
        if key < self.key then return self.left:search(key) end
        return self.right:search(key) 
    end,
    insert = function(self, key, value)
        if self.key == nil then -- morph into an internal node
          self.key, self.value, self.left, self.right = key, value, p.treenode(), p.treenode()
          return
        end
        if key == self.key then return end -- no key collision handling
        if key < self.key then return self.left:insert(key, value) end
        return self.right:insert(key, value)
    end,
    list = function(self) -- raw value-key list
        if self.key == nil then return "" end
        return self.left:list() .. ";" ..  self.value .. ":" .. self.key .. self.right:list()
    end,
    show = function(self, h) -- show indentation value tree
        if self.key == nil then return "" end
        local h = h or 0; local CR = "\n"; local TAB = "   "
        local indent = (function() local i,s = 1,""; while i<=h do s = s..TAB; i=i+1 end; return s end)()
        return self.left:show(h+1) .. indent .. self.value .. CR .. self.right:show(h+1)
    end
  }
end
return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.