- Joined
- Jul 10, 2007
- Messages
- 6,306
It's a deep first post order loop for a tree. The tree does not point to root nodes, hence my crazy arrays =P.
Tree
Output
JASS:
local boolean array d_1 //was one side looked inside of?
local $STRUCT$ array r_1 //root node of current node
local $STRUCT$ c_1 //current total depth
local $STRUCT$ n_1 //current node
set n_1 = $THIS$ //first initialize current node to this
set c_1 = 0 //and set depth to 0
if (n_1.in != 0) //if the tree isn't empty, loop
loop
//go to lowest position
loop
//if can't go down anymore, exit
exitwhen n_1.next == 0 and n_1.in == 0
//go down because can't go right
if (n_1.in == 0) then
//if already went down, exit
if (d_1[c_1]) then
exitwhen true
else
//set went down to true
set d_1[c_1] = true
set c_1 = c_1 + 1 //increase depth
set r_1[c_1] = n_1 //store current node as root of down node
set n_1 = n_1.next //go to down node
endif
//go right
elseif (not d_1[c_1]) then
set d_1[c_1] = true //set went right to true
set c_1 = c_1 + 1 //increase depth
set r_1[c_1] = n_1 //store current node as root of right node
set n_1 = n_1.in //go to right node
else
set d_1[c_1] = false //already went right, so go down
set c_1 = c_1 + 1 //increase depth by 1
set r_1[c_1] = n_1.next //store the down node of current node as root of current node
exitwhen true
endif
endloop
//if the current node is actually a node and not a pointer, use it
if (not n_1.pointer) then
set $THIS$ = n_1
endif
//go up 1
set d_1[c_1] = false //first unflag
set n_1 = r_1[c_1] //go to root node
set c_1 = c_1 - 1 //decrease depth by 1
exitwhen n_1 == 0 //exit when the current node is null
endloop
endif
Tree
Code:
1 6
7
8
9
10
2 11
12
13
14
15
3 21
22
23
24
25
4 26
27
28
29
30
Output
Code:
10
9
8
7
6 1
15
14
13
12
11 2
20
19
18
17
16 3
25
24
23
22
21 4
30
29
28
27
26 5