- Notifications
You must be signed in to change notification settings - Fork25
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
90aa055 Adding Tree Sort
Pratham18074bb8b55 Added Tree Sort
Pratham18070694fbf Added Tree Sort
Pratham180746e5ecb Update __init__.py
Pratham1807ade7d9b Added Tree Sort
Pratham1807bd23e92 added docs and test
Pratham18078f875f5 Modified tree_sort.py
Pratham18079a90a70 modified tree_sort
Pratham18079f755dc modified tree_sort
Pratham1807File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsallalgorithms/sorting/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletionsallalgorithms/sorting/tree_sort.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletionsdocs/sorting/tree-sort.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletiontests/test_sorting.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.