public function testAfterRemove() { $nodes = [10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55]; $tree = new Tree(); foreach ($nodes as $id) { $node = new Node($id, $id); $tree->insert($node); } $removeNode = [30, 70, 60, 15]; foreach ($removeNode as $id) { $tree->remove($tree->find($id)); } $expect = [['i' => 5, 'l' => null, 'r' => null, 'p' => 10, 'c' => NodeInterface::COLOR_RED], ['i' => 10, 'l' => 5, 'r' => null, 'p' => 40, 'c' => NodeInterface::COLOR_BLACK], ['i' => 40, 'l' => 10, 'r' => 50, 'p' => 55, 'c' => NodeInterface::COLOR_BLACK], ['i' => 50, 'l' => null, 'r' => null, 'p' => 40, 'c' => NodeInterface::COLOR_BLACK], ['i' => 55, 'l' => 40, 'r' => 80, 'p' => null, 'c' => NodeInterface::COLOR_BLACK], ['i' => 65, 'l' => null, 'r' => null, 'p' => 80, 'c' => NodeInterface::COLOR_BLACK], ['i' => 80, 'l' => 65, 'r' => 85, 'p' => 55, 'c' => NodeInterface::COLOR_BLACK], ['i' => 85, 'l' => null, 'r' => 90, 'p' => 80, 'c' => NodeInterface::COLOR_BLACK], ['i' => 90, 'l' => null, 'r' => null, 'p' => 85, 'c' => NodeInterface::COLOR_RED]]; foreach ($tree->infixeList() as $row => $node) { $this->assertEquals($node->getId(), $expect[$row]['i']); $this->assertEquals($node->getChild(NodeInterface::POSITION_LEFT) ? $node->getChild(NodeInterface::POSITION_LEFT)->getId() : null, $expect[$row]['l']); $this->assertEquals($node->getChild(NodeInterface::POSITION_RIGHT) ? $node->getChild(NodeInterface::POSITION_RIGHT)->getId() : null, $expect[$row]['r']); $this->assertEquals($node->getParent() ? $node->getParent()->getId() : null, $expect[$row]['p']); $this->assertEquals($node->getColor(), $expect[$row]['c']); } }
/** * Simple recursive visualisation. * * @param Tree $tree * @return string */ public function render(Tree $tree) { echo '<!doctype html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <style type="text/css"> .node { border: 1px solid #0f0; border-radius: 50px; height: 90px; width: 90px; padding: 10px; text-align: center; } .spacer { height: 100px; } .black { border-color: #000; } .red { border-color: #F00; } .leaf { background: #eee2c1; } .top, .bottom { height: 20px; } .left, .right { display: inline-block; } .parent, .root { border: 1px solid; height: 24px; width: 24px; padding: 2px; border-radius: 12px; text-align: center; display: inline-block; } .parent, .root, .glyphicon-remove { color: #999; } .parent { border-color: #ccc; background: #eee; } .root { border-color: #aad; background: #ccf; color: #aad; } </style> </head> <body> <table class="table"> <tr> '; $this->infixeRender($tree->getRoot()); echo ' </tr> </table> </body> </html> '; }