From 9deae5d14ca4f5a043d75edeb2cdbe23de7329f0 Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Mon, 26 Sep 2016 02:45:14 +0530 Subject: [PATCH] PEP style and tree traversals --- sorts/bogosort.py | 7 +-- sorts/bubble_sort.py | 1 + sorts/heap_sort.py | 4 +- sorts/insertion_sort.py | 5 +- sorts/selection_sort.py | 4 +- sorts/shell_sort.py | 13 +++-- traverals/tree_traversals.py | 96 ++++++++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 traverals/tree_traversals.py diff --git a/sorts/bogosort.py b/sorts/bogosort.py index b8cb5b31b20e..2512dab51761 100644 --- a/sorts/bogosort.py +++ b/sorts/bogosort.py @@ -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 @@ -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__': diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index eb6c7185271d..54d69e5ba389 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -9,6 +9,7 @@ For manual testing run: python bubble_sort.py """ + from __future__ import print_function diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index f89ae1866bfb..2d9dd844d0fa 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -12,6 +12,7 @@ from __future__ import print_function + def heapify(unsorted, index, heap_size): largest = index left_index = 2 * index + 1 @@ -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 @@ -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] diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 347c948df6c1..caaa9305c968 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -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 diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index 80c3062265b1..14bc804637c5 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -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]) @@ -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 diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index c87651662de9..fdb98a570d9f 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -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__': diff --git a/traverals/tree_traversals.py b/traverals/tree_traversals.py new file mode 100644 index 000000000000..e28f7bccfebe --- /dev/null +++ b/traverals/tree_traversals.py @@ -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")