Skip to content

Fix Tree Sort algorithm for duplicate values and input types #12786

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update tree_sort.py
  • Loading branch information
lighting9999 authored Jun 7, 2025
commit 1ceb911b0ae6a9005b626637e36ad69b7cae3d8a
27 changes: 13 additions & 14 deletions sorts/tree_sort.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

from collections.abc import Iterator
from dataclasses import dataclass


@dataclass
class Node:
"""Node of a Binary Search Tree (BST) for sorting."""

val: int
left: Node | None = None
right: Node | None = None
Expand All @@ -16,10 +16,10 @@
# Traverse left subtree first (smaller values)
if self.left:
yield from self.left

Check failure on line 19 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:19:1: W293 Blank line contains whitespace
# Current node value
yield self.val

Check failure on line 22 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:22:1: W293 Blank line contains whitespace
# Traverse right subtree last (larger values)
if self.right:
yield from self.right
Expand All @@ -33,23 +33,22 @@
else:
self.left.insert(val)
# Values > current go to right subtree
elif self.right is None:
self.right = Node(val)
else:
if self.right is None:
self.right = Node(val)
else:
self.right.insert(val)
self.right.insert(val)


def tree_sort(arr: list[int] | tuple[int, ...]) -> tuple[int, ...]:
"""
Sort sequence using Binary Search Tree (BST) traversal.

Check failure on line 45 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:45:1: W293 Blank line contains whitespace
Args:
arr: Input sequence (list or tuple of integers)

Check failure on line 48 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:48:1: W293 Blank line contains whitespace
Returns:
Tuple of sorted integers

Check failure on line 51 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:51:1: W293 Blank line contains whitespace
Examples:
>>> tree_sort([])
()
Expand All @@ -69,16 +68,16 @@
# Handle empty input immediately
if not arr:
return ()

Check failure on line 71 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:71:1: W293 Blank line contains whitespace
# Convert to list for uniform processing
items = list(arr)

Check failure on line 74 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:74:1: W293 Blank line contains whitespace
# Initialize BST root with first element
root = Node(items[0])

Check failure on line 77 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:77:1: W293 Blank line contains whitespace
# Insert remaining items into BST
for item in items[1:]:
root.insert(item)

Check failure on line 81 in sorts/tree_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/tree_sort.py:81:1: W293 Blank line contains whitespace
# Convert BST traversal to sorted tuple
return tuple(root)
Loading