Skip to content

Added Tree Sort algorithms #513

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

Closed
wants to merge 1 commit into from
Closed
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
71 changes: 71 additions & 0 deletions Sorts/TreeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Tree Sort
Wikipedia said : 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. Its typical use is sorting elements online: after
each insertion, the set of elements seen so far is available in sorted order.

More information: https://en.wikipedia.org/wiki/Tree_sort

*/
class node {
constructor(val){
this.value = val
this.left = 0
this.right = 0
}

insert(val){
if(this.value){
if (val < this.value) {
if (this.left == 0){
this.left = new node(val)
} else {
this.left.insert(val)
}
} else if (val > this.value){
if(this.right == 0){
this.right = new node(val)
} else {
this.right.insert(val)
}
}
} else {
this.value = val
}
}
}

function inorder(root, res){
if (root){
inorder(root.left, res)
res.push(root.value)
inorder(root.right, res)
}
}

function tree_sort(arr) {

if(arr.length == 0){
return arr
}
var root = new node(arr[0])
for (let i = 1; i < arr.length; i++){
root.insert(arr[i])
}
var res = []
inorder(root, res)
return res

}


const array = [3, 0, 100, 2, 5, -1, 4, 1, -2]

// Array before Sort
console.log('-----Before Tree Sorting-----')
console.log(array)

// Array after sort
console.log('-----After Tree Sorting-----')
console.log(tree_sort(array))