mardi 6 janvier 2015

Binary Tree - Random Generator

Suppose I have a Binary tree like this -



5
/ \
/ \
/ \
/ \
2 8
/ \ / \
/ \ / \
1 3 6 9
\ \ \
4 11 10


Now I have a random generator which will generate a number between 1 to size of tree (10 in this case). Based on the random value generated by the random generator I have to return the node from the tree(Suppose, generator given 7, so I return the 7th node(value 11), doing inorder traversal). Tomorrow I add 4 more nodes to the tree. How do I maintain the consistency? As in, the same node from the tree is returned for the random value. The inorder traversal will create a different array and the value of the index will change.





Aucun commentaire:

Enregistrer un commentaire