Logo

Programming-Idioms

  • Lisp
  • Dart
  • Fortran

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 binary_tree
    integer :: value
    type(binary_tree), allocatable :: left
    type(binary_tree), allocatable :: right
end type binary_tree
(defclass node ()
  ((value 
      :initarg :value :initform nil 
      :accessor node-value)
   (left-child 
      :initarg :left-child :initform nil 
      :accessor node-left-child)
   (right-child 
      :initarg :right-child :initform nil 
      :accessor node-right-child)))
class Node<T> {
  final T value;
  Node<T> left, right;
  Node(this.value, {this.left, this.right});
}
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;

New implementation...