• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

利用Lua实现二叉查找树并进行各种遍历

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
-- author : coder_zhang
-- date : 2014-6-25

root = nil function insert_node(number) if root == nil then root = {value = number, left = nil, right = nil, parent = nil} else q = root r = nil while q ~= nil do r = q if q.value > number then q = q.left elseif q.value < number then q = q.right else return end end if r.value > number then r.left = {value = number, left = nil, right = nil, parent = r} else r.right = {value = number, left = nil, right = nil, parent = r} end end end function find_node(p, number) while p ~= nil do if p.value == number then return p elseif p.value > number then p = p.left else p = p.right end end return p end function delete_node(number) p = find_node(root, number) if p == nil then print ("can\'t find " .. number) return end real_del = nil if p.left == nil or p.right == nil then real_del = p else q = p.right r = nil while q ~= nil do r = q q = q.left end real_del = r end child = nil if real_del.left ~= nil then child = real_del.left else child = real_del.right end if child ~= nil then child.parent = real_del.parent end if real_del.parent == nil then root = child else if real_del.parent.left == real_del then real_del.parent.left = child else real_del.parent.right = child end end if real_del ~= p then p.value = real_del.value end real_del = nil end function pre_order(p) if p ~= nil then print (p.value) pre_order(p.left) pre_order(p.right) end end function in_order(p) if p ~= nil then in_order(p.left) print (p.value) in_order(p.right) end end function post_order(p) if p ~= nil then post_order(p.left) post_order(p.right) print (p.value) end end function pre_order_no_rec(p) stack = {} while p ~= nil or #stack ~= 0 do if p == nil then p = stack[#stack] stack[#stack] = nil end print (p.value) if p.right ~= nil then stack[#stack + 1] = p.right end p = p.left end end function in_order_no_rec(p) stack = {} while p ~= nil or #stack ~= 0 do if p == nil then p = stack[#stack] stack[#stack] = nil print (p.value) p = p.right else stack[#stack + 1] = p p = p.left end end end function post_order_no_rec(p) stack = {} while p ~= nil do stack[#stack + 1] = {node = p, status = 0} p = p.left end while #stack ~= 0 do p = stack[#stack] if p.node.right == nil or p.status == 1 then print (p.node.value) stack[#stack] = nil else p = p.node.right stack[#stack].status = 1 while p ~= nil do stack[#stack + 1] = {node = p, status = 0} p = p.left end end end end array = {5, 3, 2, 4, 7, 6, 8} i = 1 while i <= #array do insert_node(array[i]) i = i + 1 end print ("--------pre order---------") pre_order(root) print ("--------------------------") print ("-------in order-----------") in_order(root) print("---------------------------") print ("-------post order---------") post_order(root) print ("--------------------------") print ("-----pre order no rec-----") pre_order_no_rec(root) print ("--------------------------") print ("-----in order no rec------") in_order_no_rec(root) print ("--------------------------") print ("---post order no rec------") post_order_no_rec(root) print ("--------------------------") delete_node(3) pre_order(root)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
FreeSWITCH 使用 lua 脚本 接管 分机注册,鉴权等发布时间:2022-07-22
下一篇:
lua常用函数发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap