Mainly: pre-order traversal, mid-order traversal, post-order traversal, hierarchical traversal, non-recursive pre-order traversal, non-recursive mid-order traversal, non-recursive post-order traversal
Copy codeThe Code is as follows:
#! /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 ):
"Empty """
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 ):
"Sequential 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 ):
"Subsequent 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 ):
"Hierarchical traversal """
If not r. is_empty ():
S = [r]
While len (s)> 0:
Temp = s. pop (0) # first pop the point from the first append
If temp and temp. root is 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 ):
"Non-recursive pre-order traversal """
Stack = []
Current = r
While len (stack)> 0 or (current and not current. is_empty ()):
While current 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 ):
"Non-recursive sequential traversal """
Stack = []
Current = r
While len (stack)> 0 or (current and not current. is_empty ()):
While current 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 ):
"Non-recursive subsequent traversal """
Stack = []
Current = r
Pre = None
While len (stack)> 0 or (current and not 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 ):
"Count 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 __':
"" Binary tree """
Ra, rb, rc, rd, re, rf = Tree (), 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 (), 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)
I learned the kmp algorithm when I was in college, and recently I forgot to read it. So I went to read the book again and wrote this algorithm in python:
Copy codeThe Code is as follows:
Def kmp (text, pattern ):
"Kmp algorithm """
Pattern = list (pattern)
Next = [-1] * len (pattern)
# Next Function
I, j = 1,-1
For I in range (1, len (pattern )):
J = next [I-1]
While True:
If pattern [I-1] = pattern [j] or j =-1:
Next [I] = j + 1
Break
Else:
J = next [j]
# Cyclic comparison
I, j = 0, 0
While I <len (text) and j <len (pattern ):
If text [I] = pattern [j] or j =-1:
I + = 1
J + = 1
Else:
J = next [j]
# If the returned result matches, the matched position is returned. Otherwise,-1 is returned.
If j = len (pattern ):
Print I-j
Else:
Print-1