Skip to content

Commit da7917c

Browse files
authored
Merge pull request abranhe#14 from Pratham1807/master
Added Tree Sort
2 parents 9eba429 + 9f755dc commit da7917c

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

allalgorithms/sorting/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
from .pidgeonhole_sort import pidgeonhole_sort
66
from .stooge_sort import stooge_sort
77
from .cocktail_shaker_sort import cocktail_shaker_sort
8+
from .tree_sort import tree_sort

allalgorithms/sorting/tree_sort.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class BinaryTreeNode(object):
2+
#initial values for value,left and right
3+
def __init__(self, value):
4+
self.value = value
5+
self.left = None
6+
self.right = None
7+
8+
9+
# inserting a new node in the binary tree
10+
def insert(tree, item):
11+
# if no initial element in the tree
12+
if tree == None:
13+
tree = BinaryTreeNode(item)
14+
else:
15+
if (item < tree.value):
16+
# if left branch of the tree is empty
17+
if (tree.left == None):
18+
tree.left = BinaryTreeNode(item)
19+
else:
20+
insert(tree.left, item)
21+
else:
22+
# if right branch of the tree is empty
23+
if (tree.right == None):
24+
tree.right = BinaryTreeNode(item)
25+
else:
26+
insert(tree.right, item)
27+
return tree
28+
29+
# funtion for the inorder traversal of the binary tree
30+
def in_order_traversal(tree,a):
31+
if (tree.left != None):
32+
in_order_traversal(tree.left,a)
33+
a.append(tree.value)
34+
if (tree.right != None):
35+
in_order_traversal(tree.right,a)
36+
37+
38+
def tree_sort(x):
39+
# root node
40+
t = insert(None, x[0]);
41+
# inserting all elements in the binary tree
42+
for i in x[1:]:
43+
insert(t,i)
44+
# the results of the inorder traversal of a binary tree is a sorted
45+
a = []
46+
in_order_traversal(t,a)
47+
return a
48+

docs/sorting/tree-sort.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Tree Sort
2+
3+
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:
4+
1. Frist is creating a binary search tree using the given array elements.
5+
2. Second phase is traversing the given binary search tree in inorder, thus resulting in a sorted array.
6+
7+
**Performance**
8+
9+
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.
10+
11+
12+
13+
## Install
14+
15+
```
16+
pip install allalgorithms
17+
```
18+
19+
## Usage
20+
21+
```py
22+
from allalgorithms.sorting import tree_sort
23+
24+
arr = [77, 2, 10, -2, 1, 7]
25+
26+
print(tree_sort(arr))
27+
# -> [-2, 1, 2, 7, 10, 77]
28+
```
29+
30+
## API
31+
32+
### tree_sort(array)
33+
34+
> Returns a sorted array
35+
36+
##### Params:
37+
38+
- `array`: Unsorted Array

tests/test_sorting.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
selection_sort,
88
pigeonhole_sort,
99
stooge_sort,
10-
cocktail_shaker_sort
10+
cocktail_shaker_sort,
11+
tree_sort
1112
)
1213

1314

@@ -32,6 +33,9 @@ def test_stooge_sort(self):
3233

3334
def test_cocktail_shaker_sort(self):
3435
self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1]))
36+
37+
def tree_sort(self):
38+
self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1]))
3539

3640

3741
if __name__ == "__main__":

0 commit comments

Comments
 (0)