Skip to content

Commit 28a7381

Browse files
authored
Merge pull request TheAlgorithms#30 from akshaysharma096/master
Traversal algorithms for Binary Tree.
2 parents 0c409f3 + fb8d4a5 commit 28a7381

File tree

7 files changed

+124
-15
lines changed

7 files changed

+124
-15
lines changed

‎sorts/bogosort.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ importprint_function
1212
importrandom
1313

14+
1415
defbogosort(collection):
1516
"""Pure implementation of the bogosort algorithm in Python
1617
:param collection: some mutable ordered collection with heterogeneous
@@ -28,13 +29,13 @@ def bogosort(collection):
2829
defisSorted(collection):
2930
iflen(collection) <2:
3031
returnTrue
31-
foriinrange(len(collection)-1):
32-
ifcollection[i] >collection[i+1]:
32+
foriinrange(len(collection)-1):
33+
ifcollection[i] >collection[i+1]:
3334
returnFalse
3435
returnTrue
3536

3637
whilenotisSorted(collection):
37-
random.shuffle(collection)
38+
random.shuffle(collection)
3839
returncollection
3940

4041
if__name__=='__main__':

‎sorts/bubble_sort.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
For manual testing run:
1010
python bubble_sort.py
1111
"""
12+
1213
from __future__ importprint_function
1314

1415

‎sorts/heap_sort.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ importprint_function
1414

15+
1516
defheapify(unsorted, index, heap_size):
1617
largest=index
1718
left_index=2*index+1
@@ -26,6 +27,7 @@ def heapify(unsorted, index, heap_size):
2627
unsorted[largest], unsorted[index] =unsorted[index], unsorted[largest]
2728
heapify(unsorted, largest, heap_size)
2829

30+
2931
defheap_sort(unsorted):
3032
'''
3133
Pure implementation of the heap sort algorithm in Python
@@ -44,7 +46,7 @@ def heap_sort(unsorted):
4446
[-45, -5, -2]
4547
'''
4648
n=len(unsorted)
47-
foriinrange(n//2-1, -1, -1):
49+
foriinrange(n//2-1, -1, -1):
4850
heapify(unsorted, i, n)
4951
foriinrange(n-1, 0, -1):
5052
unsorted[0], unsorted[i] =unsorted[i], unsorted[0]

‎sorts/insertion_sort.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def insertion_sort(collection):
3030
[-45, -5, -2]
3131
"""
3232
forindexinrange(1, len(collection)):
33-
while0<indexandcollection[index] <collection[index-1]:
34-
collection[index], collection[index-1] =collection[index-1], collection[index]
33+
while0<indexandcollection[index] <collection[index-1]:
34+
collection[index], collection[
35+
index-1] =collection[index-1], collection[index]
3536
index-=1
3637

3738
returncollection

‎sorts/selection_sort.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def selection_sort(collection):
1717
:param collection: some mutable ordered collection with heterogeneous
1818
comparable items inside
1919
:return: the same collection ordered by ascending
20-
20+
2121
2222
Examples:
2323
>>> selection_sort([0, 5, 3, 2, 2])
@@ -29,7 +29,7 @@ def selection_sort(collection):
2929
>>> selection_sort([-2, -5, -45])
3030
[-45, -5, -2]
3131
"""
32-
32+
3333
length=len(collection)
3434
foriinrange(length):
3535
least=i

‎sorts/shell_sort.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,30 @@ def shell_sort(collection):
1717
:param collection: Some mutable ordered collection with heterogeneous
1818
comparable items inside
1919
:return: the same collection ordered by ascending
20-
20+
2121
>>> shell_sort([0, 5, 3, 2, 2])
2222
[0, 2, 2, 3, 5]
2323
2424
>>> shell_sort([])
2525
[]
26-
27-
>>> shell_sort([-2, -5, -45])
26+
27+
>>> shell_sort([-2, -5, -45])
2828
[-45, -5, -2]
2929
"""
3030
# Marcin Ciura's gap sequence
3131
gaps= [701, 301, 132, 57, 23, 10, 4, 1]
32-
32+
3333
forgapingaps:
3434
i=gap
3535
whilei<len(collection):
3636
temp=collection[i]
3737
j=i
38-
whilej>=gapandcollection[j-gap] >temp:
38+
whilej>=gapandcollection[j-gap] >temp:
3939
collection[j] =collection[j-gap]
4040
j-=gap
4141
collection[j] =temp
4242
i+=1
43-
44-
43+
4544
returncollection
4645

4746
if__name__=='__main__':
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
This is pure python implementation of tree traversal algorithms
3+
"""
4+
5+
importqueue
6+
7+
8+
classTreeNode:
9+
10+
def__init__(self, data):
11+
self.data=data
12+
self.right=None
13+
self.left=None
14+
15+
16+
defbuild_tree():
17+
print("Enter the value of the root node: ", end="")
18+
data=eval(input())
19+
ifdata<0:
20+
returnNone
21+
else:
22+
q=queue.Queue()
23+
tree_node=TreeNode(data)
24+
q.put(tree_node)
25+
whilenotq.empty():
26+
node_found=q.get()
27+
print("Enter the left node of %s: "%node_found.data, end="")
28+
left_data=eval(input())
29+
ifleft_data>=0:
30+
left_node=TreeNode(left_data)
31+
node_found.left=left_node
32+
q.put(left_node)
33+
print("Enter the right node of %s: "%node_found.data, end="")
34+
right_data=eval(input())
35+
ifright_data>=0:
36+
right_node=TreeNode(right_data)
37+
node_found.right=right_node
38+
q.put(right_node)
39+
returntree_node
40+
41+
42+
defpre_order(node):
43+
ifnotnode:
44+
return
45+
print(node.data, end=" ")
46+
pre_order(node.left)
47+
pre_order(node.right)
48+
49+
50+
defin_order(node):
51+
ifnotnode:
52+
return
53+
pre_order(node.left)
54+
print(node.data, end=" ")
55+
pre_order(node.right)
56+
57+
58+
defpost_order(node):
59+
ifnotnode:
60+
return
61+
post_order(node.left)
62+
post_order(node.right)
63+
print(node.data, end=" ")
64+
65+
66+
deflevel_order(node):
67+
ifnotnode:
68+
return
69+
q=queue.Queue()
70+
q.put(node)
71+
whilenotq.empty():
72+
node_dequeued=q.get()
73+
print(node_dequeued.data, end=" ")
74+
ifnode_dequeued.left:
75+
q.put(node_dequeued.left)
76+
ifnode_dequeued.right:
77+
q.put(node_dequeued.right)
78+
79+
80+
if__name__=='__main__':
81+
importsys
82+
print("\n********* Binary Tree Traversals ************\n")
83+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
84+
# otherwise 2.x's input builtin function is too "smart"
85+
ifsys.version_info.major<3:
86+
input_function=raw_input
87+
else:
88+
input_function=input
89+
90+
node=build_tree()
91+
print("\n********* Pre Order Traversal ************")
92+
pre_order(node)
93+
print("\n******************************************\n")
94+
95+
print("\n********* In Order Traversal ************")
96+
in_order(node)
97+
print("\n******************************************\n")
98+
99+
print("\n********* Post Order Traversal ************")
100+
post_order(node)
101+
print("\n******************************************\n")
102+
103+
print("\n********* Level Order Traversal ************")
104+
level_order(node)
105+
print("\n******************************************\n")

0 commit comments

Comments
(0)