Merge pull request #30 from akshaysharma096/master

Traversal algorithms for Binary Tree.
This commit is contained in:
Harshil 2016-09-26 07:04:19 +05:30 committed by GitHub
commit 28a7381933
7 changed files with 124 additions and 15 deletions

View File

@ -11,6 +11,7 @@ python bogosort.py
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__':

View File

@ -9,6 +9,7 @@ python3 -m doctest -v bubble_sort.py
For manual testing run:
python bubble_sort.py
"""
from __future__ import print_function

View File

@ -12,6 +12,7 @@ python heap_sort.py
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]

View File

@ -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

View File

@ -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

View File

@ -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__':

View File

@ -0,0 +1,105 @@
"""
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)
if __name__ == '__main__':
import sys
print("\n********* Binary Tree Traversals ************\n")
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
# otherwise 2.x's input builtin function is too "smart"
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
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")