diff --git a/DIRECTORY.md b/DIRECTORY.md index 3e2e35b..b76ce3f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -28,14 +28,20 @@ * [Bstree](./DataStructures/BinarySearchTree/BSTree.php) * [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php) * CompareBinaryTree - * [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php) - * [Node](./DataStructures/CompareBinaryTree/Node.php) + * [CompareBinaryTree](./DataStructures/CompareBinaryTree.php) + * [BinaryTreeNode](./DataStructures/BinaryTreeNode.php) * Disjointsets * [Disjointset](./DataStructures/DisjointSets/DisjointSet.php) * [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php) * [Doublylinkedlist](./DataStructures/DoublyLinkedList.php) + * InvertBinaryTree + * [InvertBinaryTree](./DataStructures/InvertBinaryTree.php) + * [BinaryTree](./DataStructures/BinaryTree.php) * [Node](./DataStructures/Node.php) * [Queue](./DataStructures/Queue.php) + * ReverseLinkedList + * [ReverseLinkedList.php](DataStructures/ReverseLinkedList.php) + * [LinkedListItem.php](DataStructures/LinkedListItem.php) * Segmenttree * [Segmenttree](./DataStructures/SegmentTree/SegmentTree.php) * [Segmenttreenode](./DataStructures/SegmentTree/SegmentTreeNode.php) diff --git a/DataStructures/BinaryTree.php b/DataStructures/BinaryTree.php new file mode 100644 index 0000000..099e889 --- /dev/null +++ b/DataStructures/BinaryTree.php @@ -0,0 +1,40 @@ +left = $left; + return $this; + } + + public function getLeft(): ?BinaryTree + { + return $this->left; + } + + public function setRight(?BinaryTree $right) + { + $this->right = $right; + return $this; + } + + public function getRight(): ?BinaryTree + { + return $this->right; + } + + public function setValue($value) + { + $this->value = $value; + return $this; + } + + public function getValue() + { + return $this->value; + } +} diff --git a/DataStructures/BinaryTreeNode.php b/DataStructures/BinaryTreeNode.php new file mode 100644 index 0000000..88ce68e --- /dev/null +++ b/DataStructures/BinaryTreeNode.php @@ -0,0 +1,16 @@ +value = $value; + $this->left = $left; + $this->right = $right; + } + + public $value; + public ?BinaryTreeNode $left; + public ?BinaryTreeNode $right; +} diff --git a/DataStructures/CompareBinaryTree/CompareBinaryTree.php b/DataStructures/CompareBinaryTree.php similarity index 78% rename from DataStructures/CompareBinaryTree/CompareBinaryTree.php rename to DataStructures/CompareBinaryTree.php index 787f4f2..53fed2a 100644 --- a/DataStructures/CompareBinaryTree/CompareBinaryTree.php +++ b/DataStructures/CompareBinaryTree.php @@ -1,5 +1,6 @@ value = $value; - $this->left = $left; - $this->right = $right; - } - - public $value; - public ?Node $left; - public ?Node $right; -} diff --git a/DataStructures/InvertBinaryTree.php b/DataStructures/InvertBinaryTree.php new file mode 100644 index 0000000..e400209 --- /dev/null +++ b/DataStructures/InvertBinaryTree.php @@ -0,0 +1,26 @@ +getLeft(); + $b->setLeft($b->getRight()); + $b->setRight($tmp); + $this->invert($b->getLeft()); + $this->invert($b->getRight()); + } +} diff --git a/DataStructures/LinkedListItem.php b/DataStructures/LinkedListItem.php new file mode 100644 index 0000000..02f91bd --- /dev/null +++ b/DataStructures/LinkedListItem.php @@ -0,0 +1,40 @@ +next = $next; + return $this; + } + + public function getNext(): ?LinkedListItem + { + return $this->next; + } + + public function setPrev(?LinkedListItem $prev) + { + $this->prev = $prev; + return $this; + } + + public function getPrev(): ?LinkedListItem + { + return $this->prev; + } + + public function setValue($value) + { + $this->value = $value; + return $this; + } + + public function getValue() + { + return $this->value; + } +} diff --git a/DataStructures/ReverseLinkedList.php b/DataStructures/ReverseLinkedList.php new file mode 100644 index 0000000..0457714 --- /dev/null +++ b/DataStructures/ReverseLinkedList.php @@ -0,0 +1,26 @@ +getNext(); + $item->setNext(null); + while (true) { + $item->setPrev($next); + if (! $next) { + return $item; + } + $nextNext = $next->getNext(); + $next->setNext($item); + $item = $next; + $next = $nextNext; + } + } +} diff --git a/tests/DataStructures/CompareBinaryTreeTest.php b/tests/DataStructures/CompareBinaryTreeTest.php index 5e680e7..a241bb8 100644 --- a/tests/DataStructures/CompareBinaryTreeTest.php +++ b/tests/DataStructures/CompareBinaryTreeTest.php @@ -3,35 +3,33 @@ namespace DataStructures; require_once __DIR__ . '/../../vendor/autoload.php'; -require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/Node.php'; -require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/CompareBinaryTree.php'; +require_once __DIR__ . '/../../DataStructures/BinaryTreeNode.php'; +require_once __DIR__ . '/../../DataStructures/CompareBinaryTree.php'; -use DataStructures\CompareBinaryTree\CompareBinaryTree; -use DataStructures\CompareBinaryTree\Node; use PHPUnit\Framework\TestCase; class CompareBinaryTreeTest extends TestCase { public function testBinaryTreesAreEqualWhenAreEqualInReality() { - $tree1 = new Node( + $tree1 = new BinaryTreeNode( 'A', - new Node( + new BinaryTreeNode( 'B', - new Node( + new BinaryTreeNode( 'D' ), - new Node( + new BinaryTreeNode( 'E', null, - new Node( + new BinaryTreeNode( 'F' ) ) ), - new Node( + new BinaryTreeNode( 'C', - new Node('G') + new BinaryTreeNode('G') ) ); @@ -44,43 +42,43 @@ class CompareBinaryTreeTest extends TestCase public function testBinaryTreesAreNotEqualWhenAreNotEqualInReality() { - $tree1 = new Node( + $tree1 = new BinaryTreeNode( 'A', - new Node( + new BinaryTreeNode( 'B', - new Node( + new BinaryTreeNode( 'F' ), - new Node( + new BinaryTreeNode( 'E', null, - new Node( + new BinaryTreeNode( 'D' ) ) ), - new Node( + new BinaryTreeNode( 'C', - new Node('G') + new BinaryTreeNode('G') ) ); - $tree2 = new Node( + $tree2 = new BinaryTreeNode( 'A', - new Node( + new BinaryTreeNode( 'B', - new Node( + new BinaryTreeNode( 'F' ), - new Node( + new BinaryTreeNode( 'E', null, - new Node( + new BinaryTreeNode( 'D' ) ) ), - new Node( + new BinaryTreeNode( 'C' ) ); diff --git a/tests/DataStructures/InvertBinaryTreeTest.php b/tests/DataStructures/InvertBinaryTreeTest.php new file mode 100644 index 0000000..18b90e0 --- /dev/null +++ b/tests/DataStructures/InvertBinaryTreeTest.php @@ -0,0 +1,36 @@ +setValue(1); + $bl = (new BinaryTree())->setValue(3); + $b->setLeft($bl); + $br = (new BinaryTree())->setValue(2); + $b->setRight($br); + $br->setLeft((new BinaryTree())->setValue(4)); + $br->setRight((new BinaryTree())->setValue(5)); + + $expected = (new BinaryTree())->setValue(1); + $expectedBr = (new BinaryTree())->setValue(3); + $expected->setRight($expectedBr); + $expectedBl = (new BinaryTree())->setValue(2); + $expected->setLeft($expectedBl); + $expectedBl->setRight((new BinaryTree())->setValue(4)); + $expectedBl->setLeft((new BinaryTree())->setValue(5)); + + (new InvertBinaryTree())->invert($b); + + $this->assertEquals($expected, $b); + } +} diff --git a/tests/DataStructures/ReverseLinkedListTest.php b/tests/DataStructures/ReverseLinkedListTest.php new file mode 100644 index 0000000..c22ffdc --- /dev/null +++ b/tests/DataStructures/ReverseLinkedListTest.php @@ -0,0 +1,36 @@ +setValue(0); + + $prevItem = $firstItem; + + foreach ($list as $value) { + $item = new LinkedListItem(); + $item->setValue($value); + $item->setPrev($prevItem); + $prevItem->setNext($item); + $prevItem = $item; + } + + $newFirstItem = (new ReverseLinkedList())->reverse($firstItem); + do { + $this->assertEquals($newFirstItem->getValue(), array_pop($list)); + } while ($newFirstItem = $newFirstItem->getNext()); + } +}