- 论坛徽章:
- 0
|
#!/usr/bin/awk -F
#向空树中插入1000个随机数,然后遍历这棵树。
BEGIN
{ n = 1000;root = null = -1
for(i = 1;i <= n;i++)
root = insert(root,int(n*rand()))
traverse(root)
exit
}
function insert(p,x)
{
if(p == null)
{
val[p = (++nodecount)] = x
lson[p] = rson[p] = null
}
else if(x < val[p])
{
lson[p] = insert(lson[p],x)
}
else if(x > val[p])
{
rson[p] = insert(rson[p],x)
}
# else {}//本来有注释的,我去掉之后还是不行
return p
}
function traverse(p)
{
if(p != null)
{
traverse(lson[p])
print val[p]
traverse(rson[p])
}
}
在我的机器上运行出错:
jiang@jiang:~/src/c/programmingpearl$ awk -f tree.awk
awk: tree.awk: line 3: syntax error at or near end of line
请问出错的原因?谢谢了~~ |
|