Skip to content

Commit 0931f46

Browse files
Files for binary tree and string chapter
Files for binary tree and string chapter uploaded.
1 parent 6956137 commit 0931f46

File tree

9 files changed

+1403
-0
lines changed

9 files changed

+1403
-0
lines changed

BinaryTree/Tree.py

Lines changed: 747 additions & 0 deletions
Large diffs are not rendered by default.

String/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>PyString</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

String/.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}</path>
5+
</pydev_pathproperty>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
7+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>

String/Algo.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
3+
def BruteForceSearch(text, pattern):
4+
i = 0
5+
n = len(text)
6+
m = len(pattern)
7+
while i <= n - m:
8+
j = 0
9+
while j < m and pattern[j] == text[i + j]:
10+
j += 1
11+
if j == m:
12+
return i
13+
i += 1
14+
return -1
15+
16+
def RobinKarp(text, pattern):
17+
n = len(text)
18+
m = len(pattern)
19+
prime = 101
20+
powm = 1
21+
TextHash = 0
22+
PatternHash = 0
23+
if m == 0 or m > n:
24+
return -1
25+
i = 0
26+
while i < m - 1:
27+
powm = (powm << 1) % prime
28+
i += 1
29+
i = 0
30+
while i < m:
31+
PatternHash = ((PatternHash << 1) + ord(pattern[i])) % prime
32+
TextHash = ((TextHash << 1) + ord(text[i])) % prime
33+
i += 1
34+
i = 0
35+
while i <= (n - m):
36+
if TextHash == PatternHash:
37+
j = 0
38+
while j < m:
39+
if text[i + j] != pattern[j]:
40+
break
41+
j += 1
42+
if j == m:
43+
return i
44+
TextHash = (((TextHash - ord(text[i]) * powm) << 1) + ord(text[i + m])) % prime
45+
if TextHash < 0:
46+
TextHash = (TextHash + prime)
47+
i += 1
48+
return -1
49+
50+
def KMPPreprocess(pattern, ShiftArr):
51+
m = len(pattern)
52+
i = 0
53+
j = -1
54+
ShiftArr[i] = -1
55+
while i < m:
56+
while j >= 0 and pattern[i] != pattern[j]:
57+
j = ShiftArr[j]
58+
i += 1
59+
j += 1
60+
ShiftArr[i] = j
61+
62+
def KMP(text, pattern):
63+
i = 0
64+
j = 0
65+
n = len(text)
66+
m = len(pattern)
67+
ShiftArr = [0] * (m + 1)
68+
KMPPreprocess(pattern, ShiftArr)
69+
while i < n:
70+
while j >= 0 and text[i] != pattern[j]:
71+
j = ShiftArr[j]
72+
i += 1
73+
j += 1
74+
if j == m:
75+
return (i - m)
76+
return -1
77+
78+
def KMPFindCount(text, pattern):
79+
i = 0
80+
j = 0
81+
count = 0
82+
n = len(text)
83+
m = len(pattern)
84+
ShiftArr = [0] * (m + 1)
85+
KMPPreprocess(pattern, ShiftArr)
86+
while i < n:
87+
while j >= 0 and text[i] != pattern[j]:
88+
j = ShiftArr[j]
89+
i += 1
90+
j += 1
91+
if j == m:
92+
count += 1
93+
j = ShiftArr[j]
94+
return count
95+
96+
st1 = "hello, world!"
97+
st2 = "world"
98+
print "BruteForceSearch return : " , BruteForceSearch(st1, st2)
99+
print "RobinKarp return : " , RobinKarp(st1, st2)
100+
print "KMP return : " , KMP(st1, st2)

0 commit comments

Comments
 (0)