Skip to content

feat: add OptimalBinarySearchTree algorithm#7310

Open
oleksii-tumanov wants to merge 1 commit intoTheAlgorithms:masterfrom
oleksii-tumanov:optimal-binary-search-tree
Open

feat: add OptimalBinarySearchTree algorithm#7310
oleksii-tumanov wants to merge 1 commit intoTheAlgorithms:masterfrom
oleksii-tumanov:optimal-binary-search-tree

Conversation

@oleksii-tumanov
Copy link
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

Adds an implementation of Optimal Binary Search Tree using dynamic programming with Knuth's optimization.

The implementation:

  • sorts keys while preserving their frequencies
  • computes the minimum weighted search cost
  • validates null inputs, mismatched array lengths, duplicate keys, and negative frequencies

This pull request adds:

  • OptimalBinarySearchTree
  • OptimalBinarySearchTreeTest

Closes #7309

Test Case

Validated with:

  • empty input -> cost 0
  • single key -> cost equals its frequency
  • small known examples with fixed expected costs
  • reference example:
    • keys: [12, 10, 20, 42, 25, 37]
    • frequencies: [8, 34, 50, 3, 40, 30]
    • expected cost: 324
  • cross-check cases against a brute-force recursive solver
  • invalid input cases:
    • null arrays
    • mismatched lengths
    • duplicate keys
    • negative frequencies

Commands used:

  • mvn -Dtest=OptimalBinarySearchTreeTest test
  • mvn -DskipTests checkstyle:check

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.47%. Comparing base (5e06b15) to head (3eeb06a).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7310      +/-   ##
============================================
+ Coverage     79.44%   79.47%   +0.03%     
- Complexity     7064     7082      +18     
============================================
  Files           788      789       +1     
  Lines         23129    23176      +47     
  Branches       4548     4563      +15     
============================================
+ Hits          18375    18420      +45     
  Misses         4022     4022              
- Partials        732      734       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oleksii-tumanov oleksii-tumanov marked this pull request as ready for review March 14, 2026 01:40
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.

[FEATURE REQUEST] Add Optimal Binary Search Tree algorithm

2 participants