resource BinaryNode op fromParent(value: int) op receiveValueFromParentFromParent() op sendValueToChildren() op setLinks(Left, Right: cap BinaryNode) body BinaryNode(ID: int) var LeftChild:cap BinaryNode var RightChild:cap BinaryNode var MyValue := 0 proc setLinks(Left, Right ) LeftChild := Left RightChild := Right end proc receiveValueFromParent() receive fromParent(MyValue) end proc sendValueToChildren() send LeftChild.fromParent(MyValue) send RightChild.fromParent(MyValue) end final write(ID, MyValue); end end
resource Main() import BinaryNode var N: int; writes("Size "); read(N) var StartValue: int; writes("Start "); read(StartValue) var Tree[N]: cap BinaryNode fa i := 1 to N -> Tree[i] := create BinaryNode(i) af #Simulate Tree Structure, N must be odd fa i := 1 to floor(N/2) Tree[i].setLinks(Tree[2*i],Tree[2*i+1]) af #Now we can broadcast send Tree[1].fromParent(StartValue) fa J := 0 to floor(log(N,2)) -> co (I := 2**J to 2**(J+1) -1 st I <= N) Tree[I].receiveValueFromParent() oc co (I := 2**J to 2**(J+1) -1 st I <= N) Tree[I].sendValueToChildren() oc af fa i := 1 to N -> destroy Tree[i] af end
Use BinaryNode, Root and Leaf resources
resource BinaryNode op broadcast(senderID, Value: int) op setLinks(Mom, Left, Right: cap BinaryNode) body BinaryNode(ID: int) var LeftChild: cap BinaryNode var RightChild: cap BinaryNode var Parent: cap BinaryNode var MyValue := 0 proc setLinks(Mom, Left, Right) LeftChild := Left RightChild := Right Parent := Mom end proc broadcast(senderID, Value) MyValue := Value if senderID = floor(ID/2) -> send LeftChild.broadcast(ID,MyValue) send RightChild.broadcast(ID,MyValue) [] senderID = 2*ID -> send RightChild.broadcast(ID,MyValue) send Parent.broadcast(ID,MyValue) [] senderID = 2*ID + 1 -> send LeftChild.broadcast(ID,MyValue) send Parent.broadcast(ID,MyValue) [] senderID = -1 -> send LeftChild.broadcast(ID,MyValue) send RightChild.broadcast(ID,MyValue) send Parent.broadcast(ID,MyValue) fi end broadcast final write(ID, MyValue); end end
resource Root extend BinaryNode body BinaryNode(ID: int) var LeftChild: cap BinaryNode var RightChild: cap BinaryNode var Parent: cap BinaryNode var MyValue := 0 proc setLinks(Mom, Left, Right) LeftChild := Left RightChild := Right end proc broadcast(senderID, Value) MyValue := Value if senderID = floor(ID/2) -> send LeftChild.broadcast(ID,MyValue) send RightChild.broadcast(ID,MyValue) [] senderID = 2*ID -> send RightChild.broadcast(ID,MyValue) [] senderID = 2*ID + 1 -> send LeftChild.broadcast(ID,MyValue) [] senderID = -1 -> send LeftChild.broadcast(ID,MyValue) send RightChild.broadcast(ID,MyValue) fi end final write(ID, MyValue) end end
resource Leaf extend BinaryNode body Leaf(ID: int) var LeftChild: cap BinaryNode var RightChild: cap BinaryNode var Parent: cap BinaryNode var MyValue := 0 proc setLinks(Mom, Left, Right) Parent := Mom end proc broadcast(senderID, Value) MyValue := Value if senderID = floor(ID/2) -> noop [] else -> send Parent.broadcast(ID,MyValue) fi end final write(ID, MyValue) end end
resource Main() import BinaryNode, Root, Leaf var N: int; writes("Size "); read(N) var Tree[N]: cap BinaryNode #Create Nodes Tree[1] := create Root(1) #Root fa i := 2 to int(N/2) -> #Internal Tree[i] := create BinaryNode(i) af fa i := int(N/2)+ 1 to N -> #Leaves Tree[i] := create Leaf(i) af #Simulate Tree Structure, N must be odd Tree[1].setLinks(noop,Tree[2],Tree[3]) fa i := 2 to int(N/2) Tree[i].setLinks(Tree[int(i/2)], Tree[2*i],Tree[2*i+1]) af fa i := int(N/2)+ 1 to N -> Tree[i].setLinks(Tree[int(i/2)],noop,noop) af var StartValue: int; writes("Start ") read(StartValue) var StartIndex: int; writes("Start Index ") read(StartIndex) Tree[StartIndex].broadcast(-1, StartValue) fa i := 1 to N -> destroy Tree[i] af end
write("My ID ", ID, "From ",senderID, "Value ", Value)Size 15
resource BinaryNode op broadcast(senderID, Value: int) body BinaryNode(ID, N: int; Parent: cap BinaryNode) var LeftChild: cap BinaryNode := noop var RightChild: cap BinaryNode := noop var MyValue := 0 if 2*ID <= int(N/2) -> LeftChild := create BinaryNode(2*ID, N, myresource()) RightChild:= create BinaryNode(2*ID+1, N,myresource()) [] 2*ID > int(N/2) and 2*ID < N -> LeftChild := create Leaf(2*ID, N, myresource()) RightChild:= create Leaf(2*ID+1, N,myresource()) [] 2*ID > int(N/2) and 2*ID = N -> LeftChild := create Leaf(2*ID, N, myresource()) fi proc broadcast(senderID, Value) # as before final destroy LeftChild destroy RightChild end end
resource Leaf extend BinaryNode body Leaf(ID, N: int; Parent: cap BinaryNode) var LeftChild: cap BinaryNode := noop var RightChild: cap BinaryNode := noop var MyValue := 0 proc broadcast(senderID, Value) # as before end
resource Main() import BinaryNode, Root, Leaf var N: int; writes("Size "); read(N) var Tree: cap BinaryNode #Create Nodes Tree := create Root(1, N, noop) #Root var StartValue: int; writes("Start ") read(StartValue) Tree[StartIndex].broadcast(-1, StartValue) destroy Tree end
resource SortNode op find( Value: int) op add(Value: int) body BinaryNode(MyValue: int; Parent: cap BinaryNode) var LeftChild: cap BinaryNode := noop var RightChild: cap BinaryNode := noop LeftChild := noop RightChild := noop proc find(Value) if Value = MyValue -> #found it [] Value < MyValue and LeftChild =! noop -> LeftChild.find(Value) [] Value > MyValue and RightChild=! noop -> RightChild.find(Value) fi end proc add(Value) if Value = MyValue -> noop [] Value < MyValue and LeftChild =! noop -> LeftChild.add(Value) [] Value > MyValue and RightChild=! noop -> RightChild.add(Value) [] Value < MyValue and LeftChild == noop -> LeftChild := create BinaryNode(Value, myresource()) [] Value > MyValue and RightChild == noop -> RightChild := create BinaryNode(Value, myresource()) fi end end
resource Main() import SortNode var SearchTree: cap SortNode SearchTree := create SortNode(10, noop) SearchTree.add(3) SearchTree.add(9) SearchTree.add(15) SearchTree.add(4) send SearchTree.add(76) send SearchTree.add(53) send SearchTree.add(2) send SearchTree.add(8) send SearchTree.find(76) end