Skip to content

Cover the failure behavior of printTree (RedBlackBST.java) - #78

Open
hotasourav8-sketch wants to merge 1 commit into
S-AI-Projects:masterfrom
hotasourav8-sketch:student/e26b07f0855/R45-T028
Open

hotasourav8-sketch wants to merge 1 commit into
S-AI-Projects:masterfrom
hotasourav8-sketch:student/e26b07f0855/R45-T028

Conversation

@hotasourav8-sketch

Copy link
Copy Markdown

Description
This PR hardens RedBlackBST.printTree() and printTreepre() against null pointers and corrupted node structures, and adds a dedicated failure test suite. Previously, calling printTree(null) or printing a node with a detached parent threw an unhandled NullPointerException.

Related Issue
Fixes: #28

Type of Change
● Bug Fix
● Tests

Changes Made

  • Added null-safety check node == null || node == nil to printTree() and printTreepre().
  • Guarded parent pointer access against null parents: node.p != null ? node.p.key : -1.
  • Made Node class package-visible and added printTree(), printTreepre(), and insert(int key) convenience methods.
  • Added RedBlackBSTPrintTreeFailureTest.java covering null input, nil sentinel, empty tree root, and corrupted pointer structures.

Screenshots / Recordings
N/A (Console output / Backend changes only)

Testing
Executed JUnit 5 test suite:
mvn test -Dtest=RedBlackBSTPrintTreeFailureTest

Checklist
● I have tested my changes locally.
● I have added or updated tests, if needed.
● I have updated documentation, if needed.
● I have followed the project's coding guidelines.
● I have run the relevant tests and checks.
● I have addressed previous review feedback, if applicable.

Additional Notes
All tree printing operations are now fully null-safe.

Harden RedBlackBST printTree and printTreepre against null references
and add dedicated failure coverage tests:
- Guard against null node references to avoid NullPointerException
- Guard against null parent pointer when printing node attributes
- Handle nil sentinel nodes and empty trees gracefully
- Test corrupted node links (null children and null parent)
- Add package-visible Node and public insert(int) / printTree() helpers

Fixes: S-AI-Projects#28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant