Mainly: Pre-sequence traversal, middle-sequence traversal, sequential traversal, hierarchical traversal, non-recursive forward traversal, non-recursive sequence traversal, non-recursive sequential traversal
#!/usr/bin/env python
#-*-Coding:utf8-*-
Class TreeNode (object):
def __init__ (self, data=none, Left=none, Right=none):
Self.data = Data
Self.left = Left
Self.right = Right
Class Tree (Object):
def __init__ (self, Root=none):
Self.root = None
def maketree (self, data, left, right):
Self.root = TreeNode (data, left, right)
def is_empty (self):
"" Is null "" "
if Self.root is None:
return True
return False
def preorder (self, R):
"" "Forward Traversal" "
if not R.is_empty ():
Print R.root.data
if R.root.left is not None:
R.preorder (r.root.left)
if R.root.right is not None:
R.preorder (r.root.right)
def inorder (self, R):
"" Sequence Traversal ""
if not R.is_empty ():
if R.root.left is not None:
R.preorder (r.root.left)
Print R.root.data
if R.root.right is not None:
R.preorder (r.root.right)
def postorder (self, R):
"" "Next Traversal" ""
If not R.is_empty ():
If R.root.left is not None:
R.preorder (R.root.left)
Print R.root.data
If R.root.right is not None:
R.preorder (R.root.right)
def levelorder (self, R):
"" Hierarchy Traversal "" "" "" "" ""
If not R.is_empty ():
s = [R]
While Len (s) > 0:
temp = s.pop (0) # First eject the first append to the point
If temp and temp.root are not None:
Print Temp.root.data
If Temp.root.left is not None:
S.append (Temp.root.left)
If Self.root.right is not None:
S.append (Temp.root.right)
def preOrder1 (self, R):
"" "" "" "" "" "" ""
stack = []
Current = R
While Len (stack) > 0 or (Current.is_empty ()):
While, and not Current.is_empty ():
Print Current.root.data
Stack.append (current)
Current = Current.root.left
If Len (Stack) > 0:
Current = Stack.pop ()
Current = Current.root.right
def inOrder1 (self, R):
"" "" "not recursively
stack = []
Current = R
While Len (stack) > 0 or (Current.is_empty ()):
While, and not Current.is_empty ():
Stack.append (current)
Current = Current.root.left
If Len (Stack) > 0:
Current = Stack.pop ()
Print Current.root.data
Current = Current.root.right
def postOrder1 (self, R):
"" "" "" "" "" is not recursive
stack = []
Current = R
Pre = None
While Len (stack) > 0 or (Current.is_empty ()):
If current and not Current.is_empty ():
Stack.append (current)
Current = Current.root.left
Elif stack[-1].root.right!= Pre:
Current = Stack[-1].root.right
Pre = None
Else
Pre = Stack.pop ()
Print Pre.root.data
def leaves_count (self, R):
"" "" "to find the number of leaf nodes" ""
If R.is_empty ():
return 0
Elif (not R.root.left) and (not r.root.right):
Return 1
Else
Return R.root.left.leaves_count (R.root.left) + r.root.right.leaves_count (r.root.right)
if __name__ = = ' __main__ ':
"" "" "" "
RA, RB, RC, RD, RE, RF = Tree (), the tree (), the tree (), the tree (), the tree (), the tree ()
Ra.maketree ("A", none, none)
Rb.maketree ("B", none, none)
Rc.maketree ("C", none, none)
Rd.maketree ("D", none, none)
Re.maketree ("E", none, none)
Rf.maketree ("F", none, none)
R1, R2, R3, r4, R = Tree (), the tree (), the tree (), the tree (), the tree ()
R1.maketree ("-", RC, RD)
R2.maketree ("*", RB, R1)
R3.maketree ("+", RA, R2)
R4.maketree ("/", Re, RF)
R.maketree ("-", R3, R4)
R.preorder (R)
R.inorder (R)
R.postorder (R)
R.levelorder (R)
Print R.leaves_count (R)