From e997408bb6a1cc6e26765ce9a7fa120f65fba8c4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 15 Oct 2023 18:14:27 +0000 Subject: [PATCH] null checks --- Stdlib/Tree/BinarySearchTree.php | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Stdlib/Tree/BinarySearchTree.php b/Stdlib/Tree/BinarySearchTree.php index 044e217c2..6fb6392b0 100644 --- a/Stdlib/Tree/BinarySearchTree.php +++ b/Stdlib/Tree/BinarySearchTree.php @@ -180,7 +180,7 @@ class BinarySearchTree } $current = $this->root; - while (true) { + while ($current !== null) { $comparison = $node->compare($current->data); if ($comparison < 0) { @@ -226,9 +226,13 @@ class BinarySearchTree { if ($node->left === null && $node->right === null) { if ($node->parent !== null) { - if ($node->parent->left !== null && $node->parent->left->root->compare($node->data) === 0) { + if ($node->parent->left !== null + && $node->parent->left->root?->compare($node->data) === 0 + ) { $node->parent->left = null; - } elseif ($node->parent->right !== null && $node->parent->right->root->compare($node) === 0) { + } elseif ($node->parent->right !== null + && $node->parent->right->root->compare($node) === 0 + ) { $node->parent->right = null; } } @@ -242,9 +246,13 @@ class BinarySearchTree if ($node->left === null) { $temp = $node->right->root; if ($node->parent !== null) { - if ($node->parent->left !== null && $node->parent->left->root->compare($node->data) === 0) { + if ($node->parent->left !== null + && $node->parent->left->root?->compare($node->data) === 0 + ) { $node->parent->left = $temp->tree; - } elseif ($node->parent->right !== null && $node->parent->right->root->compare($node->data) === 0) { + } elseif ($node->parent->right !== null + && $node->parent->right->root?->compare($node->data) === 0 + ) { $node->parent->right = $temp->tree; } } @@ -259,9 +267,13 @@ class BinarySearchTree if ($node->right === null) { $temp = $node->left->root; if ($node->parent !== null) { - if ($node->parent->left !== null && $node->parent->left->root->compare($node->data) === 0) { + if ($node->parent->left !== null + && $node->parent->left->root?->compare($node->data) === 0 + ) { $node->parent->left = $temp->tree; - } elseif ($node->parent->right !== null && $node->parent->right->root->compare($node->data) === 0) { + } elseif ($node->parent->right !== null + && $node->parent->right->root?->compare($node->data) === 0 + ) { $node->parent->right = $temp->tree; } }