-- 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 ~= do
if p == nil then
p = stack[#stack]
stack[#stack] = nil
end
print (p.value)
if p.right ~= nil then
stack[#stack + ] = p.right
end
p = p.left
end
end function in_order_no_rec(p)
stack = {}
while p ~= nil or #stack ~= do
if p == nil then
p = stack[#stack]
stack[#stack] = nil
print (p.value)
p = p.right
else
stack[#stack + ] = p
p = p.left
end
end
end function post_order_no_rec(p)
stack = {}
while p ~= nil do
stack[#stack + ] = {node = p, status = }
p = p.left
end
while #stack ~= do
p = stack[#stack]
if p.node.right == nil or p.status == then
print (p.node.value)
stack[#stack] = nil
else
p = p.node.right
stack[#stack].status =
while p ~= nil do
stack[#stack + ] = {node = p, status = }
p = p.left
end
end
end
end array = {, , , , , , } i = while i <= #array do
insert_node(array[i])
i = i +
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() pre_order(root)

利用Lua实现二叉查找树并进行各种遍历的更多相关文章

  1. PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  2. 利用foreach对页面控件的遍历 及三目运算符的使用

    1.利用foreach对页面控件的遍历 及三目运算符的使用 利用div将一组CheckBox放在一起用于遍历 <body> <form id="form1" ru ...

  3. 利用 Lua 实现 App 动态化方案

    因为动态化的东西我第一次看实现方案的源码,而且目前还是大三的学生,缺少很多实践经验说错的地方还请原谅,也希望能指出,被告知.想了很久还是决定写出来,求大神勿喷. 并且我的一个朋友bestswifter ...

  4. Nginx利用lua剪辑FastDFS图片

    Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户端要获取不同尺寸的图片是,lua根据url中的尺寸大 ...

  5. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  6. PHP利用lua实现Redis Sorted set的zPop操作

    function zPop($key) { $script = <<<EOD local v = redis.call('zrange', KEYS[1], 0, 0); if v[ ...

  7. 利用Lua读写本地文件

    缘由 今天在使用Lua编写脚本时,需要用到读写文件的操作,很久没有使用Lua了,特写下此文来备忘一下. 简介 Lua对文件的操作与C对文件的操作基本一致,不管是参数还是方法.Lua中可以直接通过全局方 ...

  8. JavaScript利用数组原型,添加方法实现遍历多维数组每一个元素

    原型就是提供给我们为了让我们扩展更多功能的. 今天学习了用js模拟底层代码,实现数组多维的遍历.思想是在数组原型上添加一个方法. // js中的数组forEach方法,传入回掉函数 能够帮助我们遍历数 ...

  9. Nginx 中利用 Lua 脚本做访问控制

    使用场景 需要在后端服务之前做访问控制,或没有后端服务的场景,如静态文件. 实验环境 Ubuntu 14.04 Nginx 1.4.6 安装 Lua 运行环境 sudo apt-get install ...

随机推荐

  1. 基于 VirtualApp 结合 whale hook框架实现hook第三方应用

    要点 1. whale hook framework 使用示例: 2. 参考项目:VirtualHook: 3. 按照 VirtualHook 修改 VirtualApp: 4. 编写 hook pl ...

  2. 【串线篇】spring boot日志使用

    一.默认配置 1.SpringBoot默认帮我们配置好了日志: //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test pub ...

  3. django之请求和响应

    一:请求 1. 浏览器向服务器传参的几种方式: 方式一:通过正则的形式进行传递,名字为未命名模式 路由的正则形式: from django.conf.urls import url from . im ...

  4. 三星GT S7562 PIN 解锁方法

    三星GT S7562  PIN 解锁方法 请认真阅读完下文再进行操作,操作基本安全,请保证你手机电池有电续航超过1小时 首先把内存开和电话卡取出(以防万一数据丢失) 关机状态下: 同时按音量上下键 加 ...

  5. python绘制图的度分布柱状图, draw graph degree histogram with Python

    图的度数分布 import collections import matplotlib.pyplot as plt import networkx as nx G = nx.gnp_random_gr ...

  6. Xcode出现报错,但是没有给出详细信息,可以看这里

    Xcode出现报错,"Xcode build:clang: error: linker command failed with exit code 1 (use -v to..." ...

  7. Centos7网卡绑定的两种方法

    https://blog.51cto.com/youdong/1963416 选择模式4,并且layer选择3+4,交换机要配置LAG 一.传统的bond方式 (1)bond几种主要模式介绍 ü mo ...

  8. php单点登录实现原理实例详解

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任. 单点登录在大型网站里使用 ...

  9. discuz x3.2设置注册邮件激活_企业邮箱发送邮件失败

    在discuz x2.5邮箱设置里面已经说了很多关于邮件设置和常见问题的处理办法了,今天这里主要是说明下Discuz! 邮件发送失败排查思路,适用于任何板块的Discuz程序. Discuz! 邮件发 ...

  10. Netty学习笔记(一)

    学习圣思园Netty笔记,个人理解 2.netty宏观理解-本节内容: 1.阶段性事件驱动,一个请求分为若干阶段处理,每个阶段根据情况合理分配线程去处理,各阶段间通信采用异步事件驱动方式. 2.net ...