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

7 files changed

+124
-15
lines changed

sorts/bogosort.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import print_function
1212
import random
1313

14+
1415
def bogosort(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
def isSorted(collection):
2930
if len(collection) < 2:
3031
return True
31-
for i in range(len(collection)-1):
32-
if collection[i] > collection[i+1]:
32+
for i in range(len(collection) - 1):
33+
if collection[i] > collection[i + 1]:
3334
return False
3435
return True
3536

3637
while not isSorted(collection):
37-
random.shuffle(collection)
38+
random.shuffle(collection)
3839
return collection
3940

4041
if __name__ == '__main__':

sorts/bubble_sort.py

+1
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__ import print_function
1314

1415

sorts/heap_sort.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ import print_function
1414

15+
1516
def heapify(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
def heap_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-
for i in range(n//2 - 1, -1, -1):
49+
for i in range(n // 2 - 1, -1, -1):
4850
heapify(unsorted, i, n)
4951
for i in range(n - 1, 0, -1):
5052
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]

sorts/insertion_sort.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def insertion_sort(collection):
3030
[-45, -5, -2]
3131
"""
3232
for index in range(1, len(collection)):
33-
while 0 < index and collection[index] < collection[index-1]:
34-
collection[index], collection[index-1] = collection[index-1], collection[index]
33+
while 0 < index and collection[index] < collection[index - 1]:
34+
collection[index], collection[
35+
index - 1] = collection[index - 1], collection[index]
3536
index -= 1
3637

3738
return collection

sorts/selection_sort.py

+2-2
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
for i in range(length):
3535
least = i

sorts/shell_sort.py

+6-7
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
for gap in gaps:
3434
i = gap
3535
while i < len(collection):
3636
temp = collection[i]
3737
j = i
38-
while j >= gap and collection[j-gap] > temp:
38+
while j >= gap and collection[j - gap] > temp:
3939
collection[j] = collection[j - gap]
4040
j -= gap
4141
collection[j] = temp
4242
i += 1
43-
44-
43+
4544
return collection
4645

4746
if __name__ == '__main__':

traverals/binary_tree_traversals.py

+105
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+
import queue
6+
7+
8+
class TreeNode:
9+
10+
def __init__(self, data):
11+
self.data = data
12+
self.right = None
13+
self.left = None
14+
15+
16+
def build_tree():
17+
print("Enter the value of the root node: ", end="")
18+
data = eval(input())
19+
if data < 0:
20+
return None
21+
else:
22+
q = queue.Queue()
23+
tree_node = TreeNode(data)
24+
q.put(tree_node)
25+
while not q.empty():
26+
node_found = q.get()
27+
print("Enter the left node of %s: " % node_found.data, end="")
28+
left_data = eval(input())
29+
if left_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+
if right_data >= 0:
36+
right_node = TreeNode(right_data)
37+
node_found.right = right_node
38+
q.put(right_node)
39+
return tree_node
40+
41+
42+
def pre_order(node):
43+
if not node:
44+
return
45+
print(node.data, end=" ")
46+
pre_order(node.left)
47+
pre_order(node.right)
48+
49+
50+
def in_order(node):
51+
if not node:
52+
return
53+
pre_order(node.left)
54+
print(node.data, end=" ")
55+
pre_order(node.right)
56+
57+
58+
def post_order(node):
59+
if not node:
60+
return
61+
post_order(node.left)
62+
post_order(node.right)
63+
print(node.data, end=" ")
64+
65+
66+
def level_order(node):
67+
if not node:
68+
return
69+
q = queue.Queue()
70+
q.put(node)
71+
while not q.empty():
72+
node_dequeued = q.get()
73+
print(node_dequeued.data, end=" ")
74+
if node_dequeued.left:
75+
q.put(node_dequeued.left)
76+
if node_dequeued.right:
77+
q.put(node_dequeued.right)
78+
79+
80+
if __name__ == '__main__':
81+
import sys
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+
if sys.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)