Create a class called Sample that supports the following two operations and has a constructor that accepts an array p[] of double values as an input. Change(i, v) changes the value of p[i] to v and random() returns an index i with a probability of p[i]/T (where T is the total of the numbers in p[]). Use a full binary tree with assumed weights of p[i] at each node. The total weight of all the nodes in a subtree should be stored in each node. Choose a random integer between 0 and T to create a random index, and then
Create a class called Sample that supports the following two operations and has a constructor that accepts an array p[] of double values as an input. Change(i, v) changes the value of p[i] to v and random() returns an index i with a probability of p[i]/T (where T is the total of the numbers in p[]). Use a full binary tree with assumed weights of p[i] at each node. The total weight of all the nodes in a subtree should be stored in each node. Choose a random integer between 0 and T to create a random index, and then use the cumulative weights to decide which branch of the subtree to investigate. Change each node's weight along the route from the root to i when updating p[i]. Avoid explicit pointers, as we do for heaps.
Step by step
Solved in 4 steps with 2 images