Example #1
0
 def travPre(self, visit=print):  # 递归版本先序遍历
     S = Stack()
     x = self
     while True:
         while x:
             visit(x.data)
             S.push(x.rc)
             x = x.lc
         if S.empty(): break
         x = S.pop()
Example #2
0
 def travIn(self, visit=print):  # 递归版中序遍历
     S = Stack()
     x = self
     while True:
         while x:
             S.push(x)
             x = x.lc
         if S.empty(): break
         x = S.pop()
         visit(x.data)
         x = x.rc
Example #3
0
 def travIn2(self, visit=print):  # 递归版中序遍历
     S = Stack()
     x = self
     while True:
         if x:
             S.push(x)
             x = x.lc
         elif not S.empty():
             x = S.pop()
             visit(x.data)
             x = x.rc
         else:
             break
Example #4
0
 def travPost(self, visit=print):  # 递归版本后序遍历
     S = Stack()
     x = self
     if x:
         S.push(x)
     while not S.empty():
         if x.parent and S.top() != x.parent:
             x = S.top()
             while x:
                 if x.HasLChild:
                     if x.HasRChild: S.push(x.rc)
                     S.push(x.lc)
                 else:
                     S.push(x.rc)
                 x = S.top()
             S.pop()
         x = S.pop()
         visit(x.data)