Advertisement
LBPHacker

Untitled

May 11th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. #! /usr/bin/lua
  2.  
  3. local _max, _abs = math.max, math.abs
  4.  
  5. local avl_tree = {}
  6.  
  7. local avl_tree_empty_child__ = {
  8.     depth = 0
  9. }
  10.  
  11. function avl_tree:insert_r(rootparent, rootdir, root, data)
  12.     local cmpres = self.compare(data, root.data)
  13.     local next_root_dir
  14.     if cmpres > 0 then
  15.         next_root_dir = 0
  16.     elseif cmpres < 0 then
  17.         next_root_dir = 1
  18.     else
  19.         root.data = data
  20.         return true -- no insertion, no problem
  21.     end
  22.     -- local next_root
  23.     if root.children[next_root_dir] == avl_tree_empty_child__ then
  24.         root.children[next_root_dir] = {
  25.             data = data,
  26.             depth = 1,
  27.             children = {
  28.                 [0] = avl_tree_empty_child__,
  29.                 [1] = avl_tree_empty_child__
  30.             }
  31.         }
  32.     else
  33.         if self:insert_r(root, next_root_dir, root.children[next_root_dir], data) then
  34.             return true -- no insertion, no problem
  35.         end
  36.     end
  37.     local chdiff = root.children[0].depth - root.children[1].depth
  38.     if _abs(chdiff) > 1 then
  39.         local deepdir = (chdiff > 0) and 0 or 1
  40.        
  41.         local new_root
  42.        
  43.         local c = root
  44.         local b = c.children[deepdir]
  45.        
  46.         if b.children[deepdir].depth < b.children[1 - deepdir].depth then
  47.             local a = b.children[1 - deepdir]
  48.            
  49.             c.children[deepdir] = a.children[1 - deepdir]
  50.             b.children[1 - deepdir] = a.children[deepdir]
  51.             a.children[deepdir] = b
  52.             a.children[1 - deepdir] = c
  53.            
  54.             c.depth = _max(c.children[0].depth, c.children[1].depth) + 1
  55.             b.depth = _max(b.children[0].depth, b.children[1].depth) + 1
  56.             a.depth = _max(a.children[0].depth, a.children[1].depth) + 1
  57.            
  58.             new_root = a
  59.         else
  60.             c.children[deepdir], b.children[1 - deepdir] = b.children[1 - deepdir], c
  61.            
  62.             c.depth = _max(c.children[0].depth, c.children[1].depth) + 1
  63.             b.depth = _max(b.children[0].depth, b.children[1].depth) + 1
  64.    
  65.             new_root = b
  66.             print(b.data)
  67.             print(b.children[0].data)
  68.             print(b.children[1].data)
  69.         end
  70.        
  71.        
  72.         if rootparent then
  73.             rootparent.children[rootdir] = new_root
  74.         end
  75.        
  76.         return
  77.     end
  78.     root.depth = _max(root.children[0].depth, root.children[1].depth) + 1
  79. end
  80.  
  81. function avl_tree:insert(data)
  82.     if not self.ghostroot.children[0] then
  83.         self.ghostroot.children[0] = {
  84.             data = data,
  85.             depth = 1,
  86.             children = {
  87.                 [0] = avl_tree_empty_child__,
  88.                 [1] = avl_tree_empty_child__
  89.             }
  90.         }
  91.         return
  92.     end
  93.     print(self.ghostroot)
  94.     self:insert_r(self.ghostroot, 0, self.ghostroot.children[0], data)
  95. end
  96.  
  97. function avl_tree:debug_r(root, tlen)
  98.     local pstr = tostring(root.data) .. " [" .. root.depth .. "]"
  99.     local ntlen = tlen + #pstr
  100.     if root.children[0] ~= avl_tree_empty_child__ then
  101.         self:debug_r(root.children[0], ntlen + 1)
  102.         print((" "):rep(ntlen) .. "/")
  103.     end
  104.     print((" "):rep(tlen) .. pstr)
  105.     if root.children[1] ~= avl_tree_empty_child__ then
  106.         print((" "):rep(ntlen) .. "\\")
  107.         self:debug_r(root.children[1], ntlen + 1)
  108.     end
  109.     -- lol no
  110. end
  111.  
  112. function avl_tree:debug()
  113.     print(("-"):rep(50))
  114.     if self.ghostroot.children[0] then
  115.         self:debug_r(self.ghostroot.children[0], 0)
  116.     end
  117.     print(("-"):rep(50))
  118. end
  119.  
  120. local meta_avl_tree = {__index = avl_tree}
  121. local function avl_tree_new(cmpfunc)
  122.     return setmetatable({
  123.         compare = cmpfunc,
  124.         ghostroot = {
  125.             children = {}
  126.         }
  127.     }, meta_avl_tree)
  128. end
  129.  
  130.  
  131. local myavltree = avl_tree_new(function(a, b)
  132.     return (a > b) and -1 or ((a < b) and 1 or 0)
  133. end)
  134.  
  135. while true do
  136.     local s = io.read()
  137.     local n = tonumber(s)
  138.     if not n then break end
  139.     myavltree:insert(n)
  140.     myavltree:debug()
  141. end
  142.  
  143. --[=[for ix = 1, 64 do
  144.     myavltree:insert(math.random(1, 1000))
  145. end
  146. myavltree:debug()--]=]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement