A: Perform a toss: determine whether the current node will have at least one descendent. If Head, there is at least one descendent. Then go to B. If Tail, there is no descendent. Then go to D. B: Perform a toss: determine whether the current node will have one or two children. If Head, the current node will have one child. Then go to C. If Tail, the current node will have two children. Assign two random integer values to the two children. Then go to D. C: Perform a toss: determine whether the current node will have left or right child. If Head, the current node will have a left child. Assign a random integer value to the child. Then go to D. If Tail, the current node will have a right child. Assign a random integer value to the child. Then go to D. D: move the current node to the next node (using breadth first traversal) and go to A: The random integer is selected from 0 to 100. Create 20 trees for this experiment. Please provide code in python.
Part 1: create random binary trees
For each tree, create a tree according to the following procedure. Toss a fair coin up to 100 tosses
Start from the root node as current node.
-
A: Perform a toss: determine whether the current node will have at least one descendent.
If Head, there is at least one descendent. Then go to B.
If Tail, there is no descendent. Then go to D.
-
B: Perform a toss: determine whether the current node will have one or two children.
If Head, the current node will have one child. Then go to C.
If Tail, the current node will have two children. Assign two random integer values to the two children. Then go to D. -
C: Perform a toss: determine whether the current node will have left or right child.
If Head, the current node will have a left child. Assign a random integer value to the child. Then go to D.
If Tail, the current node will have a right child. Assign a random integer value to the child. Then go to D. -
D: move the current node to the next node (using breadth first traversal) and go to A:
The random integer is selected from 0 to 100. Create 20 trees for this experiment.
Please provide code in python.
Step by step
Solved in 2 steps