Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Last active August 29, 2015 14:10
Show Gist options
  • Save imouaddine/7da5054e8a430ce60ec7 to your computer and use it in GitHub Desktop.
Save imouaddine/7da5054e8a430ce60ec7 to your computer and use it in GitHub Desktop.
Tree
# 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