SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  * node.value
  4.  * node.left
  5.  * node.right
  6.  */
  7.  
  8. class Node {
  9.     public $value;
  10.     public $left;
  11.     public $right;
  12.  
  13.     public function __construct($value)
  14.     {
  15.         $this->value = $value;
  16.     }
  17.  
  18.     public function add(Node $node, $position = null)
  19.     {
  20.         if ($position == 'l') {
  21.             $this->left = &$node;
  22.             return $node;
  23.         } else {
  24.             $this->right = &$node;
  25.             return $node;
  26.         }
  27.     }
  28. }
  29.  
  30. $node = new Node(1);
  31. $left = $node->add(new Node(2), 'l');
  32. $right = $node->add(new Node(3));
  33. $right->add(new Node(4), 'l');
  34. $right->add(new Node(5));
  35. $left->add(new Node(6), 'l');
  36. $left->add(new Node(7));
  37.  
  38.  
  39. inorder($node);
  40.  
  41. function inorder(&$node)
  42. {
  43.     if($node == null) return null;
  44.  
  45.     inorder($node->left);
  46.     echo $node->value . "<br />";
  47.     inorder($node->right);
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top