One of the main activities in algorithms and programs, whose purpose is manipulating data, is sorting. So if you write these kinds of programs-what kind of method for sorting data is best? In this article I will present some algorithms for sorting and their advantages and weaknesses.
Using the Sorting with Binary Tree method I will cover creating the binary tree and the algorithm that goes through the whole tree and gets values from it in ascending order.
The binary tree is created out of nodes and connections between these nodes. Each node has a data field and two pointers (these pointers present connections between nodes) to two other nodes: called the left and right child. If the node doesn't have a left or right child its pointer is set to null. The first node of this binary tree (the one that has no parent nodes) is called the root of the binary tree.
To create our binary tree we take the first element and make a root out of it. Then the next element from the array will be placed as the left or right child of the root depending on its value. If it is bigger or equal we put it as a right child, or if it is smaller we put it as a left child. For all other elements we do the same - keeping this child-parent order for all other nodes, as presented in the next picture.