Skip to content

Added Tree Sort #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allalgorithms/sorting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 tree_sort
48 changes: 48 additions & 0 deletions allalgorithms/sorting/tree_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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,a):
if (tree.left != None):
in_order_traversal(tree.left,a)
a.append(tree.value)
if (tree.right != None):
in_order_traversal(tree.right,a)


def tree_sort(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
a = []
in_order_traversal(t,a)
return a

38 changes: 38 additions & 0 deletions docs/sorting/tree-sort.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
selection_sort,
pidgeonhole_sort,
stooge_sort,
cocktail_shaker_sort
cocktail_shaker_sort,
tree_sort
)


Expand All @@ -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__":
Expand Down