## CS 662: Theory of Parallel Algorithms Tree Broadcast

San Diego State University -- This page last updated March 7, 1995

### Contents of Tree Broadcast Lecture

```resource BinaryNode

op fromParent(value: int)
op sendValueToChildren()

body BinaryNode(ID: int)
var LeftChild:cap BinaryNode
var RightChild:cap BinaryNode

var MyValue := 0

LeftChild := Left
RightChild := Right
end

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;

var StartValue: int;

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)

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)

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
```

## Tree Broadcast No Global Control

```
```
Use BinaryNode, Root and Leaf resources

### BinaryNode - The Parent Resource

```resource BinaryNode

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

LeftChild := Left
RightChild := Right
Parent := Mom
end

MyValue := Value

if senderID = floor(ID/2) ->

[] senderID = 2*ID ->

[] senderID = 2*ID + 1 ->

[] senderID = -1 ->

fi

final
write(ID, MyValue);
end

end

```

### Root Resource, Child of BinaryNode

```
resource Root
extend BinaryNode

body BinaryNode(ID: int)
var LeftChild: 	cap BinaryNode
var RightChild: 	cap BinaryNode
var Parent: 	cap BinaryNode
var MyValue := 0

LeftChild := Left
RightChild := Right
end

MyValue := Value

if senderID = floor(ID/2) ->

[] senderID = 2*ID ->

[] senderID = 2*ID + 1 ->

[] senderID = -1 ->

fi
end

final
write(ID, MyValue)
end
end
```

### Leaf, Child of BinaryNode

```resource Leaf
extend BinaryNode

body Leaf(ID: int)
var LeftChild: 	cap BinaryNode
var RightChild: 	cap BinaryNode
var Parent: 	cap BinaryNode
var MyValue := 0

Parent := Mom
end

MyValue := Value

if senderID = floor(ID/2) ->

noop

[] else ->

fi
end

final
write(ID, MyValue)
end
end

```

### Sample Program Using BinaryNode, Root & Leaf

```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

fa i := 2 to int(N/2)

Tree[2*i],Tree[2*i+1])

af

fa i := int(N/2)+ 1 to N ->

af

var StartValue: int;
writes("Start ")

var StartIndex: int;
writes("Start Index ")

fa i := 1 to N ->
destroy Tree[i]
af

end
```

### Execution Order of Sample Program

```	write("My ID ", ID, "From ",senderID,
"Value ", Value)

```
Size 15
Start 5
Start Index 15

My ID 15 From -1 Value 5
My ID 7 From 15 Value 5
My ID 14 From 7 Value 5
My ID 3 From 7 Value 5
My ID 6 From 3 Value 5
My ID 1 From 3 Value 5
My ID 12 From 6 Value 5
My ID 13 From 6 Value 5
My ID 2 From 1 Value 5
My ID 4 From 2 Value 5
My ID 5 From 2 Value 5
My ID 8 From 4 Value 5
My ID 9 From 4 Value 5
My ID 10 From 5 Value 5
My ID 11 From 5 Value 5
```
```

Size 15
Start 5
Start Index 2

My ID 2 From -1 Value 5
My ID 4 From 2 Value 5
My ID 5 From 2 Value 5
My ID 1 From 2 Value 5
My ID 8 From 4 Value 5
My ID 9 From 4 Value 5
My ID 10 From 5 Value 5
My ID 11 From 5 Value 5
My ID 3 From 1 Value 5
My ID 6 From 3 Value 5
My ID 7 From 3 Value 5
My ID 12 From 6 Value 5
My ID 13 From 6 Value 5
My ID 14 From 7 Value 5
My ID 15 From 7 Value 5

## Tree Broadcast, No Global Create

### BinaryNode

```
resource BinaryNode

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

```

### Leaf

```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

```

### Root

Add BinaryNode's startup code to the Root resource

Add BinaryNode's final code to the Root resource

### Using New BinaryNode

```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 ")

destroy Tree

end

```

## Binary Search Tree

(No Root or Leaf Nodes)
```resource SortNode

op find( 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

if Value = MyValue ->
noop

[] Value < MyValue and LeftChild =! noop ->

[] Value > MyValue and RightChild=! noop ->

[] Value < MyValue and LeftChild == noop ->
LeftChild := create BinaryNode(Value, myresource())

[] Value > MyValue and RightChild == noop ->
RightChild := create BinaryNode(Value, myresource())

fi

end

end
```

### Using SortNode

```resource Main()

import SortNode

var SearchTree: cap SortNode

SearchTree := create SortNode(10, noop)