Logo

Programming-Idioms

  • C++
  • PHP
  • Ada

Idiom #9 Create a Binary Tree data structure

The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.

type Tree_Node;
type Tree_Node_Access is access Tree_Node;
type Tree_Node is record
   Value: Integer;
   Left, Right: Tree_Node_Access;
end record;
struct binary_tree
{
 int data;
 binary_tree *left = nullptr, *right = nullptr;
};
class BNode
{
    public $data;
    public $lNode;
    public $rNode;

    public function __construct($item)
    {
        $this->data = $item;
        $this->lNode = null;
        $this->rNode = null;
    }
}
struct treenode{
  int value;
  struct treenode* left;
  struct treenode* right;
}

New implementation...
< >
programming-idioms.org