Tree

Tree sort is a sorting algorithm that builds a binary search tree from the elements to be sorted, then traverses the tree in-order to retrieve the elements in sorted order.

The algorithm has an average and worst-case time complexity of O(n log n), where n is the number of elements to be sorted.

pseudocode:

TreeSort(array):
    Define a function to insert elements into a binary search tree (BST).

    // Build the BST
    for each element in array:
        Insert(element, root)

    // In-order traversal to get the sorted sequence
    InOrderTraversal(root)

Insert(value, node):
    if node is NULL:
        Create a new node with the given value
        Set it as the root if the tree is empty
    else if value < node.value:
        Insert value into the left subtree recursively
    else:
        Insert value into the right subtree recursively

InOrderTraversal(node):
    if node is not NULL:
        InOrderTraversal(node.left)
        Print node.value
        InOrderTraversal(node.right)
// Node class to create new nodes
class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

// Binary Search Tree class
class BST {
    constructor() {
        this.root = null;
    }

    // Insert a new node with a given value into the BST
    insert(value) {
        const newNode = new Node(value);
        if (!this.root) {
            this.root = newNode;
            return;
        }

        let current = this.root;
        while (true) {
            if (value < current.value) {
                if (!current.left) {
                    current.left = newNode;
                    break;
                }
                current = current.left;
            } else {
                if (!current.right) {
                    current.right = newNode;
                    break;
                }
                current = current.right;
            }
        }
    }

    // Perform in-order traversal of the BST
    inOrderTraversal(node, result = []) {
        if (node !== null) {
            this.inOrderTraversal(node.left, result);
            result.push(node.value);
            this.inOrderTraversal(node.right, result);
        }
        return result;
    }
}

// Tree Sort function
function treeSort(array) {
    const bst = new BST();

    // Build the BST
    array.forEach(element => {
        bst.insert(element);
    });

    // Perform in-order traversal to get the sorted sequence
    return bst.inOrderTraversal(bst.root);
}

// Example usage
const array = [4, 2, 5, 1, 3];
const sortedArray = treeSort(array);
console.log(sortedArray); // Output: [1, 2, 3, 4, 5]

In this example, we define a Node class to represent nodes in the binary search tree, and a BinaryTree class to represent the binary search tree itself.

The treeSort function takes an array as input, creates a binary search tree with its elements, and then performs an in-order traversal to retrieve the elements in sorted order.

Finally, it returns the sorted array.

Last updated