Data update
This commit is contained in:
parent
35bcdeebf8
commit
74c69a0df6
2427 changed files with 31826 additions and 3468 deletions
72
Task/Tree-traversal/EasyLang/tree-traversal.easy
Normal file
72
Task/Tree-traversal/EasyLang/tree-traversal.easy
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
tree[] = [ 1 2 3 4 5 6 -1 7 -1 -1 -1 8 9 ]
|
||||
#
|
||||
proc preorder ind . .
|
||||
if ind > len tree[] or tree[ind] = -1
|
||||
return
|
||||
.
|
||||
write " " & tree[ind]
|
||||
preorder ind * 2
|
||||
preorder ind * 2 + 1
|
||||
.
|
||||
write "preorder:"
|
||||
preorder 1
|
||||
print ""
|
||||
#
|
||||
proc inorder ind . .
|
||||
if ind > len tree[] or tree[ind] = -1
|
||||
return
|
||||
.
|
||||
inorder ind * 2
|
||||
write " " & tree[ind]
|
||||
inorder ind * 2 + 1
|
||||
.
|
||||
write "inorder:"
|
||||
inorder 1
|
||||
print ""
|
||||
#
|
||||
proc postorder ind . .
|
||||
if ind > len tree[] or tree[ind] = -1
|
||||
return
|
||||
.
|
||||
postorder ind * 2
|
||||
postorder ind * 2 + 1
|
||||
write " " & tree[ind]
|
||||
.
|
||||
write "postorder:"
|
||||
postorder 1
|
||||
print ""
|
||||
#
|
||||
global tail head queue[] .
|
||||
proc initqu n . .
|
||||
len queue[] n
|
||||
tail = 1
|
||||
head = 1
|
||||
.
|
||||
proc enqu v . .
|
||||
queue[tail] = v
|
||||
tail = (tail + 1) mod1 len queue[]
|
||||
.
|
||||
func dequ .
|
||||
if head = tail
|
||||
return -1
|
||||
.
|
||||
h = head
|
||||
head = (head + 1) mod1 len queue[]
|
||||
return queue[h]
|
||||
.
|
||||
initqu len tree[]
|
||||
proc levelorder n . .
|
||||
enqu n
|
||||
repeat
|
||||
ind = dequ
|
||||
until ind = -1
|
||||
if ind < len tree[] and tree[ind] <> -1
|
||||
write " " & tree[ind]
|
||||
enqu ind * 2
|
||||
enqu ind * 2 + 1
|
||||
.
|
||||
.
|
||||
.
|
||||
write "level-order:"
|
||||
levelorder 1
|
||||
print ""
|
||||
|
|
@ -10,9 +10,9 @@ singleton DummyNode
|
|||
|
||||
class Node
|
||||
{
|
||||
rprop int Value;
|
||||
rprop Node Left;
|
||||
rprop Node Right;
|
||||
int Value : rprop;
|
||||
Node Left : rprop;
|
||||
Node Right : rprop;
|
||||
|
||||
constructor new(int value)
|
||||
{
|
||||
|
|
@ -82,7 +82,7 @@ class Node
|
|||
{
|
||||
bool next() = queue.isNotEmpty();
|
||||
|
||||
get()
|
||||
get Value()
|
||||
{
|
||||
Node item := queue.pop();
|
||||
Node left := item.Left;
|
||||
|
|
|
|||
37
Task/Tree-traversal/SETL/tree-traversal.setl
Normal file
37
Task/Tree-traversal/SETL/tree-traversal.setl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
program tree_traversal;
|
||||
tree := [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]];
|
||||
|
||||
print("preorder ", preorder(tree));
|
||||
print("inorder ", inorder(tree));
|
||||
print("postorder ", postorder(tree));
|
||||
print("level-order ", levelorder(tree));
|
||||
|
||||
proc preorder(tree);
|
||||
if tree = om then return []; end if;
|
||||
[item, left, right] := tree;
|
||||
return [item] + preorder(left) + preorder(right);
|
||||
end proc;
|
||||
|
||||
proc inorder(tree);
|
||||
if tree = om then return []; end if;
|
||||
[item, left, right] := tree;
|
||||
return inorder(left) + [item] + inorder(right);
|
||||
end proc;
|
||||
|
||||
proc postorder(tree);
|
||||
if tree = om then return []; end if;
|
||||
[item, left, right] := tree;
|
||||
return postorder(left) + postorder(right) + [item];
|
||||
end proc;
|
||||
|
||||
proc levelorder(tree);
|
||||
items := [];
|
||||
loop init queue := [tree]; while queue /= [] do
|
||||
[item, left, right] fromb queue;
|
||||
items with:= item;
|
||||
if left /= om then queue with:= left; end if;
|
||||
if right /= om then queue with:= right; end if;
|
||||
end loop;
|
||||
return items;
|
||||
end proc;
|
||||
end program;
|
||||
Loading…
Add table
Add a link
Reference in a new issue