Skip to content

Fix: structural integrity during copy#8

Merged
jordanmontt merged 1 commit intopharo-containers:mainfrom
HossamSaberr:main
Mar 24, 2026
Merged

Fix: structural integrity during copy#8
jordanmontt merged 1 commit intopharo-containers:mainfrom
HossamSaberr:main

Conversation

@HossamSaberr
Copy link
Copy Markdown
Contributor

This PR fixes a critical structural bug in the copy method that caused the copied tree to degrade into a right-skewed linked list.

The Issue:
The previous implementation used inOrderDo: to populate the new tree. Because inOrderDo: yields elements in strictly ascending order, inserting them sequentially into a new BST causes it to build a straight line of right-child nodes, ruining the O(log N) performance.

The Fix:
Changed the traversal to preOrderDo:. By extracting and inserting the root first, then the left and right subtrees, the copied tree perfectly reconstructs the original shape.

Changes made:

  • Updated CTBinarySearchTree >> copy to use preOrderDo:.
  • Added CTBinarySearchTreeTest >> testCopyPreservesStructure to assert that tree height and node placements remain identical after a copy operation.

and all tests Green now :)

image

Closes #7

@jordanmontt
Copy link
Copy Markdown
Member

Great catch!!!!

@jordanmontt jordanmontt merged commit c0450ed into pharo-containers:main Mar 24, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: copy degrades tree structure into a linked list

2 participants