Const
  n = 8;

Type
  TType = Integer;
  arrType = Array[1 .. n] Of TType;

Const
  a: arrType =
       (44, 55, 12, 42, 94, 18, 6, 67);

(*
  Binary Tree sorting program
*)
Type
  PTTree = ^TTree;
  TTree =
    Record
      a: TType;
      left, right: PTTree;
    End;

Function AddToTree(root: PTTree;
         nValue: TType): PTTree;
  Begin
    (* If no child - create new item *)
    If root = nil Then
      Begin
        root := New(PTTree);
        root^.a := nValue;
        root^.left := nil;
        root^.right := nil;
        AddToTree := root; Exit
      End;

    If root^.a < nValue Then
      root^.right := AddToTree(root^.right, nValue)
    Else
      root^.left := AddToTree(root^.left, nValue);
    AddToTree := root
  End;


(*
  Filling the array
*)
Procedure TreeToArray(root: PTTree;
          Var a: arrType);
  Const
    maxTwo: Integer = 1;
  Begin
    (* Recursion stops when no more child *)
    If root = nil Then Exit;

    (* Left subtree *)
    TreeToArray(root^.left, a);
    a[maxTwo] := root^.a; Inc(maxTwo);

    (* Right subtree *)
    TreeToArray(root^.right, a);
    Dispose(root)
  End;

(* Sorting procedure *)
Procedure SortTree(Var a: arrType; n: Integer);
  Var
    root: PTTree;
    i: Integer;
  Begin
    root := nil;
    For i := 1 To n Do
      root := AddToTree(root, a[i]);
    TreeToArray(root, a)
  End;

Begin
  SortTree(a, n);
End.