From 90aa055a2f7f5072c8248f32505ef5c36da95110 Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:41:35 +0530 Subject: [PATCH 1/9] Adding Tree Sort --- allalgorithms/sorting/tree_sort.py | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 allalgorithms/sorting/tree_sort.py diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py new file mode 100644 index 0000000..5113cbb --- /dev/null +++ b/allalgorithms/sorting/tree_sort.py @@ -0,0 +1,47 @@ +class BinaryTreeNode(object): + #initial values for value,left and right + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + +# inserting a new node in the binary tree +def insert(tree, item): + # if no initial element in the tree + if tree == None: + tree = BinaryTreeNode(item) + else: + if (item < tree.value): + # if left branch of the tree is empty + if (tree.left == None): + tree.left = BinaryTreeNode(item) + else: + insert(tree.left, item) + else: + # if right branch of the tree is empty + if (tree.right == None): + tree.right = BinaryTreeNode(item) + else: + insert(tree.right, item) + return tree + + +# funtion for the inorder traversal of the binary tree +def in_order_traversal(tree): + if (tree.left != None): + in_order_traversal(tree.left) + print(tree.value) + if (tree.right != None): + in_order_traversal(tree.right) + + +if __name__ == '__main__': + x = list(map(int,input().split(" "))) + # root node + t = insert(None, x[0]); + # inserting all elements in the binary tree + for i in x[1:]: + insert(t,i) + # the results of the inorder traversal of a binary tree is a sorted + in_order_traversal(t) From 4bb8b55dfe57016c03d170015378acca51e536ad Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:50:09 +0530 Subject: [PATCH 2/9] Added Tree Sort --- allalgorithms/sorting/tree_sort.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 5113cbb..8e325eb 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -36,8 +36,7 @@ def in_order_traversal(tree): in_order_traversal(tree.right) -if __name__ == '__main__': - x = list(map(int,input().split(" "))) +def TreeSort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree From 0694fbf9ad83a85b75a68c2be145ec38161c2641 Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:52:54 +0530 Subject: [PATCH 3/9] Added Tree Sort --- allalgorithms/sorting/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index f249576..8dadeb4 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -5,3 +5,4 @@ from .pidgeonhole_sort import pidgeonhole_sort from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort +from .tree_sort import TreeSort From 46e5ecbaf411240d674a1573c161943087a0ca1e Mon Sep 17 00:00:00 2001 From: Pratham1807 <35078777+Pratham1807@users.noreply.github.com> Date: Wed, 10 Oct 2018 21:55:31 +0530 Subject: [PATCH 4/9] Update __init__.py --- allalgorithms/sorting/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 8dadeb4..3c45246 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -5,4 +5,4 @@ from .pidgeonhole_sort import pidgeonhole_sort from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort -from .tree_sort import TreeSort +from .tree_sort import tree_sort From ade7d9b80ab49e5ed31a4f669f962511d9d2148f Mon Sep 17 00:00:00 2001 From: Pratham1807 <35078777+Pratham1807@users.noreply.github.com> Date: Wed, 10 Oct 2018 21:56:37 +0530 Subject: [PATCH 5/9] Added Tree Sort --- allalgorithms/sorting/tree_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 8e325eb..d5b3cbd 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -36,7 +36,7 @@ def in_order_traversal(tree): in_order_traversal(tree.right) -def TreeSort(x): +def tree_sort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree From bd23e92411bac35fe89f9af4da6a3e175409fd00 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 02:12:54 +0530 Subject: [PATCH 6/9] added docs and test --- docs/sorting/tree-sort.md | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_sorting.py | 6 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 docs/sorting/tree-sort.md diff --git a/docs/sorting/tree-sort.md b/docs/sorting/tree-sort.md new file mode 100644 index 0000000..bd87af6 --- /dev/null +++ b/docs/sorting/tree-sort.md @@ -0,0 +1,38 @@ +# Tree Sort + +A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree (in-order) so that the elements come out in sorted order. It has two phases: +1. Frist is creating a binary search tree using the given array elements. +2. Second phase is traversing the given binary search tree in inorder, thus resulting in a sorted array. + +**Performance** + +The average number of comparisions for this method is O(nlogn). But in worst case, number of comparisions is reduced by O(n^2), a case which arrives when the tree is skewed. + + + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import tree_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(tree_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### tree_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array \ No newline at end of file diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 7fe083c..90ef887 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -7,7 +7,8 @@ selection_sort, pidgeonhole_sort, stooge_sort, - cocktail_shaker_sort + cocktail_shaker_sort, + tree_sort ) @@ -32,6 +33,9 @@ def test_stooge_sort(self): def test_cocktail_shaker_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) + + def tree_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": From 8f875f5254db0c5c19934125e6cbc4ec8e556efd Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:14:06 +0530 Subject: [PATCH 7/9] Modified tree_sort.py --- allalgorithms/sorting/tree_sort.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index d5b3cbd..3a2ffb3 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -1,3 +1,4 @@ + class BinaryTreeNode(object): #initial values for value,left and right def __init__(self, value): @@ -26,21 +27,22 @@ def insert(tree, item): insert(tree.right, item) return tree - # funtion for the inorder traversal of the binary tree -def in_order_traversal(tree): +def in_order_traversal(tree,a): if (tree.left != None): - in_order_traversal(tree.left) - print(tree.value) + in_order_traversal(tree.left,a) + a.append(tree.value) if (tree.right != None): - in_order_traversal(tree.right) + in_order_traversal(tree.right,a) -def tree_sort(x): +def TreeSort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree for i in x[1:]: insert(t,i) # the results of the inorder traversal of a binary tree is a sorted - in_order_traversal(t) + a = [] + in_order_traversal(t,a) + return a From 9a90a70124a6dacc338dfc6f1cf4e18a9b284840 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:27:58 +0530 Subject: [PATCH 8/9] modified tree_sort --- allalgorithms/sorting/tree_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 3a2ffb3..341c305 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -1,4 +1,3 @@ - class BinaryTreeNode(object): #initial values for value,left and right def __init__(self, value): @@ -36,7 +35,7 @@ def in_order_traversal(tree,a): in_order_traversal(tree.right,a) -def TreeSort(x): +def tree_sort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree @@ -46,3 +45,4 @@ def TreeSort(x): a = [] in_order_traversal(t,a) return a + From 9f755dc52f29326b348cbda92eda5af2b8a9b815 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:40:54 +0530 Subject: [PATCH 9/9] modified tree_sort --- tests/test_sorting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 90ef887..dc4cc80 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -35,7 +35,7 @@ def test_cocktail_shaker_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) def tree_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__":