Python-implemented two-fork tree algorithm and KMP algorithm example

Source: Internet
Author: User
Main: Pre-sequence traversal, middle sequence traversal, post-order traversal, hierarchical traversal, non-recursive pre-sequence traversal, non-recursive sequence traversal, non-recursive post-order traversal

Copy the Code code 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):
"" "is Empty" ""
If Self.root is None:
Return True
Return False

def preorder (self, R):
"" "Pre-order 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):
"" "in 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):
"" "Follow-through" ""
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):
"" "Level Traversal" ""
If not R.is_empty ():
s = [R]
While Len (s) > 0:
temp = s.pop (0) # pops up 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):
"" "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 in-sequence 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 follow-through" ""
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):
"" "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__ ':
"" "Two Forks Tree" ""
RA, RB, RC, RD, RE, RF = tree (), tree (), tree (), tree (), 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 (), tree (), 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)


University when learned KMP algorithm, recently looked at the time found unexpectedly forget, so went to read a book, and then wrote this algorithm in Python:
Copy the Code code 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]
#循环比较
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 matched, returns the matching position, otherwise returns-1
If j = = Len (pattern):
Print I–j
Else
Print-1
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.