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()
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
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
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)