MCPcopy Create free account
hub / github.com/BeeBombshell/Python-DSA / morris_traversal

Function morris_traversal

Tree Data Structure/morris_traversal.py:9–37  ·  view source on GitHub ↗
(root)

Source from the content-addressed store, hash-verified

7
8
9def morris_traversal(root):
10
11 current = root
12
13 while current is not None:
14
15 if current.left is None:
16 yield current.data
17 current = current.right
18 else:
19
20 # Find the inorder
21 # predecessor of current
22 pre = current.left
23 while pre.right is not None and pre.right is not current:
24 pre = pre.right
25
26 if pre.right is None:
27
28 # Make current as right
29 # child of its inorder predecessor
30 pre.right = current
31 current = current.left
32
33 else:
34 # Revert the changes made
35 pre.right = None
36 yield current.data
37 current = current.right
38
39root = Node(1,right=Node(3),left=Node(2,left=Node(4),right=Node(5)))
40

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected