Skip to content

Commit

Permalink
PEP style and tree traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaysharma096 committed Sep 25, 2016
1 parent 0c409f3 commit 9deae5d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
7 changes: 4 additions & 3 deletions sorts/bogosort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import print_function
import random


def bogosort(collection):
"""Pure implementation of the bogosort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous
Expand All @@ -28,13 +29,13 @@ def bogosort(collection):
def isSorted(collection):
if len(collection) < 2:
return True
for i in range(len(collection)-1):
if collection[i] > collection[i+1]:
for i in range(len(collection) - 1):
if collection[i] > collection[i + 1]:
return False
return True

while not isSorted(collection):
random.shuffle(collection)
random.shuffle(collection)
return collection

if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions sorts/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For manual testing run:
python bubble_sort.py
"""

from __future__ import print_function


Expand Down
4 changes: 3 additions & 1 deletion sorts/heap_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import print_function


def heapify(unsorted, index, heap_size):
largest = index
left_index = 2 * index + 1
Expand All @@ -26,6 +27,7 @@ def heapify(unsorted, index, heap_size):
unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest]
heapify(unsorted, largest, heap_size)


def heap_sort(unsorted):
'''
Pure implementation of the heap sort algorithm in Python
Expand All @@ -44,7 +46,7 @@ def heap_sort(unsorted):
[-45, -5, -2]
'''
n = len(unsorted)
for i in range(n//2 - 1, -1, -1):
for i in range(n // 2 - 1, -1, -1):
heapify(unsorted, i, n)
for i in range(n - 1, 0, -1):
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
Expand Down
5 changes: 3 additions & 2 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def insertion_sort(collection):
[-45, -5, -2]
"""
for index in range(1, len(collection)):
while 0 < index and collection[index] < collection[index-1]:
collection[index], collection[index-1] = collection[index-1], collection[index]
while 0 < index and collection[index] < collection[index - 1]:
collection[index], collection[
index - 1] = collection[index - 1], collection[index]
index -= 1

return collection
Expand Down
4 changes: 2 additions & 2 deletions sorts/selection_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def selection_sort(collection):
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
Examples:
>>> selection_sort([0, 5, 3, 2, 2])
Expand All @@ -29,7 +29,7 @@ def selection_sort(collection):
>>> selection_sort([-2, -5, -45])
[-45, -5, -2]
"""

length = len(collection)
for i in range(length):
least = i
Expand Down
13 changes: 6 additions & 7 deletions sorts/shell_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ def shell_sort(collection):
:param collection: Some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
>>> shell_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> shell_sort([])
[]
>>> shell_sort([-2, -5, -45])
>>> shell_sort([-2, -5, -45])
[-45, -5, -2]
"""
# Marcin Ciura's gap sequence
gaps = [701, 301, 132, 57, 23, 10, 4, 1]

for gap in gaps:
i = gap
while i < len(collection):
temp = collection[i]
j = i
while j >= gap and collection[j-gap] > temp:
while j >= gap and collection[j - gap] > temp:
collection[j] = collection[j - gap]
j -= gap
collection[j] = temp
i += 1



return collection

if __name__ == '__main__':
Expand Down
96 changes: 96 additions & 0 deletions traverals/tree_traversals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
This is pure python implementation of tree traversal algorithms
"""

import queue


class TreeNode:

def __init__(self, data):
self.data = data
self.right = None
self.left = None


def build_tree():
print("Enter the value of the root node: ", end="")
data = eval(input())
if data < 0:
return None
else:
q = queue.Queue()
tree_node = TreeNode(data)
q.put(tree_node)
while not q.empty():
node_found = q.get()
print("Enter the left node of %s: " % node_found.data, end="")
left_data = eval(input())
if left_data >= 0:
left_node = TreeNode(left_data)
node_found.left = left_node
q.put(left_node)
print("Enter the right node of %s: " % node_found.data, end="")
right_data = eval(input())
if right_data >= 0:
right_node = TreeNode(right_data)
node_found.right = right_node
q.put(right_node)
return tree_node


def pre_order(node):
if not node:
return
print(node.data, end=" ")
pre_order(node.left)
pre_order(node.right)


def in_order(node):
if not node:
return
pre_order(node.left)
print(node.data, end=" ")
pre_order(node.right)


def post_order(node):
if not node:
return
post_order(node.left)
post_order(node.right)
print(node.data, end=" ")


def level_order(node):
if not node:
return
q = queue.Queue()
q.put(node)
while not q.empty():
node_dequeued = q.get()
print(node_dequeued.data, end=" ")
if node_dequeued.left:
q.put(node_dequeued.left)
if node_dequeued.right:
q.put(node_dequeued.right)


node = build_tree()

print("\n********* Pre Order Traversal ************")
pre_order(node)
print("\n******************************************\n")

print("\n********* In Order Traversal ************")
in_order(node)
print("\n******************************************\n")

print("\n********* Post Order Traversal ************")
post_order(node)
print("\n******************************************\n")

print("\n********* Level Order Traversal ************")
level_order(node)
print("\n******************************************\n")

0 comments on commit 9deae5d

Please sign in to comment.