利用Lua实现二叉查找树并进行各种遍历
-- 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实现二叉查找树并进行各种遍历的更多相关文章
- 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 ...
- 利用foreach对页面控件的遍历 及三目运算符的使用
1.利用foreach对页面控件的遍历 及三目运算符的使用 利用div将一组CheckBox放在一起用于遍历 <body> <form id="form1" ru ...
- 利用 Lua 实现 App 动态化方案
因为动态化的东西我第一次看实现方案的源码,而且目前还是大三的学生,缺少很多实践经验说错的地方还请原谅,也希望能指出,被告知.想了很久还是决定写出来,求大神勿喷. 并且我的一个朋友bestswifter ...
- Nginx利用lua剪辑FastDFS图片
Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户端要获取不同尺寸的图片是,lua根据url中的尺寸大 ...
- 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 ...
- PHP利用lua实现Redis Sorted set的zPop操作
function zPop($key) { $script = <<<EOD local v = redis.call('zrange', KEYS[1], 0, 0); if v[ ...
- 利用Lua读写本地文件
缘由 今天在使用Lua编写脚本时,需要用到读写文件的操作,很久没有使用Lua了,特写下此文来备忘一下. 简介 Lua对文件的操作与C对文件的操作基本一致,不管是参数还是方法.Lua中可以直接通过全局方 ...
- JavaScript利用数组原型,添加方法实现遍历多维数组每一个元素
原型就是提供给我们为了让我们扩展更多功能的. 今天学习了用js模拟底层代码,实现数组多维的遍历.思想是在数组原型上添加一个方法. // js中的数组forEach方法,传入回掉函数 能够帮助我们遍历数 ...
- Nginx 中利用 Lua 脚本做访问控制
使用场景 需要在后端服务之前做访问控制,或没有后端服务的场景,如静态文件. 实验环境 Ubuntu 14.04 Nginx 1.4.6 安装 Lua 运行环境 sudo apt-get install ...
随机推荐
- Java JNA (四)—— void**、void*、char**、char*、int*等类型映射关系及简单示例
ByReference类有很多子类,这些类都非常有用. ByteByReference.DoubleByReference.FloatByReference. IntByReference.LongB ...
- 2019-8-14-win10-使用-SMB-v1
title author date CreateTime categories win10 使用 SMB v1 lindexi 2019-08-14 08:55:55 +0800 2018-2-13 ...
- Linux 部署或升级openssh7.5p1
运维Linux系统,部署或升级openssh是经常面临的事,以下已redhat6和redhat7为例. 在redhat6中部署openssh会有什么坑,在编辑openssh源码包时会报一些类似的错误, ...
- java并发学习--第一章 线程的创建
所谓的并发就是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行.所以我们看似几个线程在同时进行,其实在操作系统中 ...
- MyBatis(五)
MyBatis Generator CMyBatis代码生成器,简称 MBG)
- hadoop中的一些术语介绍
1.MR作业是客户端执行的一个工作单元:包括输入数据,MR的程序和配置信息. Hadoop将作业分成若干个任务task来执行,分为两种任务:map和reduce任务.这些任务运行在集群的节点上,并通过 ...
- mysql 5.6 datetime default now()
CREATE TABLE `test` ( id int, `gmt_create` datetime DEFAULT NOW() not NULL )ENGINE=InnoDB; mysq ...
- JDK1.8 红黑树
序言 当在10亿数据中只需要进行10几次比较就能查找到目标时,不禁感叹编程之魅力!人类之伟大呀! —— 学红黑树有感. 红黑树的应用 红黑树的应用比较广泛,主要是用它来存储有序的数据,它的时间复杂度是 ...
- C#通过文件头判断文件的类型(不是后缀名)
FileStream fs=new FileStream(@"D:\6",FileMode.Open,FileAccess.Read); BinaryReader reader= ...
- Python_004(列表和元组)
一.列表 1. 列表: 列表的创建:li = [],列表中可以放置字符串,元组,列表,字典,列表等各种数据类型,32位的Python可以存放2^32个数据 2. 列表的索引和切片 列表的索引:格式ls ...