Sorting is a fundamental concept in programming, helping us organize data efficiently. One interesting and powerful sorting method is Tree Sort, which uses a Binary Search Tree (BST) to arrange elements in order. Unlike simple sorting algorithms like Bubble Sort or Insertion Sort, Tree Sort leverages the structure of trees to achieve fast and organized sorting. Understanding Tree Sort also helps beginners grasp the basics of binary trees, a key data structure in computer science.
with hands-on learning.
get the skills and confidence to land your next move.
In Dart, Tree Sort can be implemented elegantly using recursion and object-oriented programming principles. This makes it a great exercise for beginners who want to strengthen both their algorithmic thinking and Dart programming skills. By learning Tree Sort, you also gain insights into how data structures like BSTs can be used for searching, insertion, and sorting, which are common tasks in real-world applications.
Program 1: Basic Tree Sort Using Recursive BST Insertion
This first program demonstrates the basic Tree Sort by creating a Binary Search Tree and performing an in-order traversal to get the sorted array. It’s perfect for beginners who want to understand the core concept.
class Node {
int data;
Node? left;
Node? right;
Node(this.data);
}
Node? insert(Node? root, int data) {
if (root == null) return Node(data);
if (data < root.data) {
root.left = insert(root.left, data);
} else {
root.right = insert(root.right, data);
}
return root;
}
void inorder(Node? root, List<int> result) {
if (root != null) {
inorder(root.left, result);
result.add(root.data);
inorder(root.right, result);
}
}
void treeSort(List<int> arr) {
Node? root;
for (var num in arr) {
root = insert(root, num);
}
List<int> sorted = [];
inorder(root, sorted);
for (int i = 0; i < arr.length; i++) {
arr[i] = sorted[i];
}
}
void main() {
List<int> numbers = [5, 2, 9, 1, 5, 6];
print("Original List: $numbers");
treeSort(numbers);
print("Sorted List: $numbers");
}In this example, each number is inserted into the BST, which automatically arranges them so that smaller numbers go left and larger numbers go right. Performing an in-order traversal then extracts the numbers in sorted order. Beginners can see how a tree structure naturally organizes data, making Tree Sort both intuitive and efficient.
Program 2: Tree Sort Using Iterative Insertion
Instead of recursion, Tree Sort can also be implemented using iterative insertion. This approach avoids recursive calls and is useful when dealing with very large datasets.
class Node {
int data;
Node? left;
Node? right;
Node(this.data);
}
Node? insertIterative(Node? root, int data) {
Node? newNode = Node(data);
if (root == null) return newNode;
Node? current = root;
Node? parent;
while (current != null) {
parent = current;
if (data < current.data) {
current = current.left;
} else {
current = current.right;
}
}
if (data < parent!.data) {
parent.left = newNode;
} else {
parent.right = newNode;
}
return root;
}
void inorder(Node? root, List<int> result) {
if (root != null) {
inorder(root.left, result);
result.add(root.data);
inorder(root.right, result);
}
}
void treeSort(List<int> arr) {
Node? root;
for (var num in arr) {
root = insertIterative(root, num);
}
List<int> sorted = [];
inorder(root, sorted);
for (int i = 0; i < arr.length; i++) {
arr[i] = sorted[i];
}
}
void main() {
List<int> numbers = [7, 3, 8, 4, 2, 6];
print("Original List: $numbers");
treeSort(numbers);
print("Sorted List: $numbers");
}Iterative insertion provides an alternative to recursion, helping beginners understand different ways to navigate and modify a BST. This approach is particularly useful in environments with limited stack memory.
Program 3: Tree Sort Using Recursive Traversal Only
Sometimes, the BST is built beforehand, and Tree Sort only focuses on traversal. This program shows how sorting can be achieved using recursive traversal of an existing tree.
class Node {
int data;
Node? left;
Node? right;
Node(this.data);
}
void inorderTraversal(Node? root, List<int> result) {
if (root == null) return;
inorderTraversal(root.left, result);
result.add(root.data);
inorderTraversal(root.right, result);
}
void main() {
Node root = Node(5);
root.left = Node(2);
root.right = Node(9);
root.left!.left = Node(1);
root.left!.right = Node(4);
List<int> sorted = [];
inorderTraversal(root, sorted);
print("Sorted List from Prebuilt Tree: $sorted");
}In this example, the tree is prebuilt, and Tree Sort simply uses in-order traversal to extract sorted elements. Beginners can see how traversal alone can organize data without focusing on insertion logic.
Program 4: Tree Sort With Duplicate Handling
Tree Sort can also handle duplicate elements gracefully. This program demonstrates how to maintain duplicates while sorting.
class Node {
int data;
int count = 1;
Node? left;
Node? right;
Node(this.data);
}
Node? insert(Node? root, int data) {
if (root == null) return Node(data);
if (data == root.data) {
root.count++;
} else if (data < root.data) {
root.left = insert(root.left, data);
} else {
root.right = insert(root.right, data);
}
return root;
}
void inorder(Node? root, List<int> result) {
if (root != null) {
inorder(root.left, result);
for (int i = 0; i < root.count; i++) {
result.add(root.data);
}
inorder(root.right, result);
}
}
void treeSort(List<int> arr) {
Node? root;
for (var num in arr) {
root = insert(root, num);
}
List<int> sorted = [];
inorder(root, sorted);
for (int i = 0; i < arr.length; i++) {
arr[i] = sorted[i];
}
}
void main() {
List<int> numbers = [5, 2, 5, 1, 9, 2];
print("Original List: $numbers");
treeSort(numbers);
print("Sorted List: $numbers");
}By adding a count property in each node, the program can handle duplicate elements efficiently. Beginners can see how BSTs can store not just structure but also additional data to manage duplicates elegantly.
Program 5: Tree Sort Using a Class-Based Approach
This version wraps the BST logic and sorting into a single class. It’s ideal for beginners learning object-oriented programming in Dart.
class Node {
int data;
Node? left;
Node? right;
Node(this.data);
}
class BST {
Node? root;
void insert(int data) {
root = _insert(root, data);
}
Node? _insert(Node? node, int data) {
if (node == null) return Node(data);
if (data < node.data) {
node.left = _insert(node.left, data);
} else {
node.right = _insert(node.right, data);
}
return node;
}
void inorderTraversal(Node? node, List<int> result) {
if (node != null) {
inorderTraversal(node.left, result);
result.add(node.data);
inorderTraversal(node.right, result);
}
}
List<int> treeSort(List<int> arr) {
for (var num in arr) {
insert(num);
}
List<int> sorted = [];
inorderTraversal(root, sorted);
return sorted;
}
}
void main() {
List<int> numbers = [7, 3, 5, 2, 9, 1];
print("Original List: $numbers");
BST bst = BST();
List<int> sorted = bst.treeSort(numbers);
print("Sorted List: $sorted");
}This approach introduces beginners to object-oriented programming in Dart while implementing Tree Sort. By encapsulating insertion and traversal within a class, the program becomes cleaner, modular, and easy to extend for more complex tree operations.
Frequently Asked Questions (FAQ)
Tree Sort often raises common questions for beginners, so here are some answers.
Q1: What is Tree Sort and why is it useful?
Tree Sort is a sorting algorithm that uses a Binary Search Tree to organize elements. It is useful because it sorts efficiently while maintaining the natural hierarchical structure of data.
Q2: Can Tree Sort handle duplicate elements?
Yes. By adding a count to each node or handling duplicates in insertion logic, Tree Sort can manage repeated elements while keeping them sorted.
Q3: Is Tree Sort faster than Quick Sort or Merge Sort?
Tree Sort has an average time complexity of O(n log n), similar to Quick Sort and Merge Sort. However, its performance depends on the balance of the tree; unbalanced trees can degrade to O(n²).
Q4: Can Tree Sort be implemented without recursion?
Yes. Iterative insertion and traversal can achieve Tree Sort without recursion, which is useful for very large datasets to avoid stack overflow.
Q5: Where is Tree Sort commonly used?
Tree Sort is commonly used when data naturally fits into a tree structure, when duplicates must be handled carefully, or when sorting while building a searchable tree simultaneously.
Conclusion
Tree Sort is a versatile and efficient sorting algorithm, especially when used with Binary Search Trees. In Dart, it can be implemented using recursion, iterative insertion, duplicate handling, and object-oriented principles. Each approach helps beginners understand the power of tree structures in organizing data.
By practicing these Tree Sort programs, beginners can strengthen their Dart programming skills, gain confidence with recursion and object-oriented concepts, and see how data structures directly influence algorithm efficiency. Tree Sort is more than a sorting algorithm; it’s an introduction to the world of trees, searching, and structured data handling.




