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 from __future__ import print_function
import random import random
def bogosort(collection): def bogosort(collection):
"""Pure implementation of the bogosort algorithm in Python """Pure implementation of the bogosort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous :param collection: some mutable ordered collection with heterogeneous

View File

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

View File

@ -12,6 +12,7 @@ python heap_sort.py
from __future__ import print_function from __future__ import print_function
def heapify(unsorted, index, heap_size): def heapify(unsorted, index, heap_size):
largest = index largest = index
left_index = 2 * index + 1 left_index = 2 * index + 1
@ -26,6 +27,7 @@ def heapify(unsorted, index, heap_size):
unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest]
heapify(unsorted, largest, heap_size) heapify(unsorted, largest, heap_size)
def heap_sort(unsorted): def heap_sort(unsorted):
''' '''
Pure implementation of the heap sort algorithm in Python Pure implementation of the heap sort algorithm in Python

View File

@ -31,7 +31,8 @@ def insertion_sort(collection):
""" """
for index in range(1, len(collection)): for index in range(1, len(collection)):
while 0 < index and collection[index] < collection[index - 1]: while 0 < index and collection[index] < collection[index - 1]:
collection[index], collection[index-1] = collection[index-1], collection[index] collection[index], collection[
index - 1] = collection[index - 1], collection[index]
index -= 1 index -= 1
return collection return collection

View File

@ -41,7 +41,6 @@ def shell_sort(collection):
collection[j] = temp collection[j] = temp
i += 1 i += 1
return collection return collection
if __name__ == '__main__': 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")