Last active
August 29, 2015 14:10
-
-
Save imouaddine/7da5054e8a430ce60ec7 to your computer and use it in GitHub Desktop.
Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Iterative using visited attribute | |
def in_order_ite | |
s = Stack.new | |
s.push root | |
until s.empty? | |
c = s.last | |
if c.left && !c.left.visited | |
s.push c.left | |
else | |
c = s.pop | |
c.visit | |
if c.right | |
s.push c.right | |
end | |
end | |
end | |
end | |
end | |
#Iterative without uusing visited attribute | |
def in_order_ite | |
s = Stack.new | |
current = root | |
done = false | |
until done | |
if current | |
s.push current | |
current = current.left | |
else | |
if !s.empty? | |
current = s.pop | |
current.visit | |
current = current.right | |
else | |
done = true | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment