0% found this document useful (0 votes)
40 views18 pages

Lecture08 PDF

The document describes algorithms for traversing binary trees, including: 1. Pre-order, in-order, and post-order traversal using recursion. 2. Iterative algorithms for pre-order, in-order, and post-order traversal using stacks. The post-order traversal algorithm uses a flag to track whether the left or right subtree has been visited. 3. A second version of iterative post-order traversal is presented that uses parent pointers and pushes/pops nodes from the stack to traverse down and back up the tree.

Uploaded by

Andra Pufu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views18 pages

Lecture08 PDF

The document describes algorithms for traversing binary trees, including: 1. Pre-order, in-order, and post-order traversal using recursion. 2. Iterative algorithms for pre-order, in-order, and post-order traversal using stacks. The post-order traversal algorithm uses a flag to track whether the left or right subtree has been visited. 3. A second version of iterative post-order traversal is presented that uses parent pointers and pushes/pops nodes from the stack to traverse down and back up the tree.

Uploaded by

Andra Pufu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Game 15

• Fifteen puzzle: 4x4 tiles

4/17/2014 1
Subalg. searchSpace( initialConfig )
.
reachedConfig  { initialConfig }
unExpandedConfig  { initialConfig }

while unExpandedConfig   do
config := extractOne (unExpandedConfig)
@for any valid successor succ of config do
if succ  reachedConfig then
if isFinal(succ) then
// … !! process solution
endif
reachedConfig  {succ} reachedConfig
unExpandedConfig  {succ}  unExpandedConfig
endif
endfor
endWhile
End_searchSpace
2
Robot in a maze (1)
Consider a maze (rectangular shape) with occupied
cells (X) and free cells (*). Consider a robot (R) in
this maze, and a goal position in this maze.
(a) Verify if the robot can reach the goal position.
(b) Determine a path (if exists).
(c) Determine the shortest path (if exists).

4/17/2014 3
X * X X * * *
* X * * X * *
G * * * * * *
* X * S * * X
* X * * * * X
* X * * X * *
X * X * * *
*
4/17/2014 4
Robot in a maze (2)
Consider a maze (rectangular shape) with occupied
cells (X) and free cells (*). Consider a robot (R) in
this maze
(a) Verify if the robot can get out of the maze (can
reach any of the margins).
(b) Determine a path (if exists) to get out of the maze.
(c) Determine the shortest path (if exists) to get out of
the maze.

4/17/2014 5
X X X X X X X
X S * * * * *
X X X * X X X
* * * * X X X
X X X X X X X
X X X X X X X
X X X X X X X
4/17/2014 6
Sketch: possible project structure
Subalg. onLevels(T)
initEmpty (q) Binary tree
if not isEmpty (T) then traversal
enqueue(q, rootPos (T))
endif
while (not isEmpty(q)) do
p := dequeue(q)
@ process p^.info
if (p^.left <> NIL) then
enqueue( q, p^.left)
endif
if ( p^.right <> NIL) then
dequeue( q, p^.right)
endif
endWhile
endOnLevels
1
Binary tree traversal
Pre-order: Post-order:
root, left-subtree, right-subtree left-subtree, right-subtree, root
1. Visit the node. 1. Traverse the left subtree.
2. Traverse the left subtree. 2. Traverse the right subtree.
3. Traverse the right subtree. 3. Visit the node.

In-order:
left-subtree, root, right-subtree
1. Traverse the left subtree.
2. Visit the node.
3. Traverse the right subtree.
2
Binary tree parsing
Preorder  use stack
Subalg. preorder(p)
if p<>NIL then
@ process p^.info
preorder(p^.left)
preorder(p^.right)
endif
endPreorder

3
Subalg. iterativePreorder(T) .
initEmpty(s)
if (not isEmpty(T)) then
push(s,rootPos(T))
endif
while not isEmpty(s) do
p := pop(s)
@process p^.info
if p^.right <>NIL then push(s, p^.right) endif
if p^.left <>NIL then push(s, p^.left) endif
endwhile
end_iterativePreorder
4
Subalg. iterativeInorder(T)
.
initEmpty (s)
p := rootPos(T)
while not ( isEmpty(s) and p=NIL ) do
while (p <> NIL) do
push(s,p)
p := p^.left
endwhile
p := pop(s)
@ process p^.info
p:=p^.right
endWhile

end_iterativeInorder
5
Subalg. iterativeinorder2(T)
Subalg. iterativeinorder(T)
. initEmpty (s)
initEmpty (s)
p := rootPos(T)
p := rootPos(T)
while not ( isEmpty(s)) and
while not ( isEmpty(s))
p=NIL ) do
and p=NIL ) do
if (p <> NIL) then
while (p <> NIL) do
push(s,p)
push(s,p)
p := p^.left
p := p^.left
else
endwhile
p := pop(s)
p := pop(s)
@ process p^.info
@ process p^.info
p:=p^.right
p:=p^.right
endif
endWhile
endWhile
end_iterativeInorder
end_iterativeInorder2 6
Subalg. iterativePostorder(T) //use a flag: left subtree visited

initEmpty(s)
p:= rootPos(T) .
while not ( isEmpty(s) and p=NIL) do
while p<>NIL do //Traverse downwards to left
push(s,[p,0])
p:=p^.left
endwhile
[p,k]:=pop(s)
if k=0 then //upwards from left
push(s,[p,1])
p:=p^.right
else // upwards from right
@ process p^.info
p := NIL
endif
endwhile

End_iterativePostorder

7
Subalg. iterativePostorder2(T) // version 2

initEmpty(s)
pNode = NIL .
cNode = rootPos(T)
while not ( isEmpty(s) and cNode = NIL)
if parent(pNode,cNode) then //Traverse downwards
if (cNode^.left <>NIL) then //More traversing to do
push(s,cNode)
pNode = cNode
cNode = cNode^.left
else if (cNode^.right <>NIL) then
push(s,cNode)
pNode = cNode
cNode = cNode^.right
else //cNode does not have descendants: go upward
@ process cNode^.info
pNode = cNode
cNode = peek(s)
endif
endif 8
Subalg. iterativePostorder2(T) // version 2

initEmpty(s)
pNode = NIL .
cNode = rootPos(T)
while not ( isEmpty(s) and cNode = NIL)
if parent(pNode,cNode) then //Traverse downwards
if (cNode^.left <>NIL) then //More traversing to do
push(s,cNode)
pNode = cNode
cNode = cNode^.left
else if (cNode^.right <>NIL) then
push(s,cNode)
pNode = cNode
cNode = cNode^.right
else //cNode does not have descendants: go upward
@ process cNode^.info
pNode = cNode
cNode = peek(s)
endif
endif 9
//continuation of iterativePostorder2(a) version 2
else //cNode is parent of pNode; traverse upwards
.
if ( cNode^.left = pNode) and (cNode^.right <>NIL) thens
//upwards from left;
//go right if possible; then downwards
pNode = cNode
cNode = cNode^.right
else // upwards from right; process & go upward
@process cNode
pop(s) //!!!
pNode = cNode
cNode = peek(s)
endif
endif
endwhile
Remark:
end_iterativePostOrder2 Define peek to return NIL if stack is empty
10
Binary tree examples

11

You might also like