有没有办法使用进程在 Erlang 中创建二叉树?

Is there a way to create a binary tree in Erlang using processes?

我正在尝试使用进程创建二叉树。

-module(binarytree).

-export([start/1, loop/2]).

start(N) ->
    P = spawn(?MODULE, loop, [N, self(), self(), self()]),
    P ! create_nodes,
    receive
    end.

loop(0, _, _, _) ->
    done;

loop(N, Root, LeftNode, RightNode) ->
    receive
        create_nodes ->
            MyPid = self(),
            LeftNode = spawn(?MODULE, loop, [N-1, MyPid, ???, ???]),
            RightNode = spawn(?MODULE, loop, [N-1, MyPid, ???, ???]),
            LeftNode ! create_nodes,
            RightNode ! create_nodes;
        _ ->
            ok
    end,
    loop(N, Root, LeftNode, RightNode).

没有 LeftNodeRightNode 我正在成功创建树结构,但我想在循环状态中保存 LeftNodeRightNode。创建节点时应该传入什么函数参数?

更新: 感谢 Nalin Ranjan。 更改这些行:

MyPid = self(),
LeftNode = spawn(?MODULE, loop, [N-1, MyPid, ???, ???]),
RightNode = spawn(?MODULE, loop, [N-1, MyPid, ???, ???]),
LeftNode ! create_nodes,
RightNode ! create_nodes;

收件人:

MyPid = self(),
NewLeftNode = spawn(?MODULE, loop, [N-1, MyPid, "", ""]),
NewRightNode = spawn(?MODULE, loop, [N-1, MyPid, "", ""]),
NewLeftNode ! create_nodes,
NewRightNode ! create_nodes,
loop(N, Root, NewLeftNode, NewRightNode);
loop(N, Root, LeftNode, RightNode) -> 
 receive 
  create_nodes -> 
    MyPid = self(),
    NewLeftNode = spawn(?MODULE, loop, [N-1, MyPid, "empty-child-node", "empty-child-node"),
    NewRightNode = spawn(?MODULE, loop, [N-1, MyPid, "empty-child-node", "empty-child-node"]), 
    NewLeftNode ! create_nodes, 
    NewRightNode ! create_nodes, 
    loop(N, Root, NewLeftNode, NewRightNode); 

  _ -> ok 
end, 
loop(N, Root, LeftNode, RightNode). 

试试这个。