Could someone help me with this part of the assignment? Write a program that tak
ID: 3717555 • Letter: C
Question
Could someone help me with this part of the assignment?
Write a program that takes an integer N from the command line, builds a random BST(Binary Search Tree) of size N, then enters into a loop where it deletes a random key (using the code delete(select(StdRandom.uniform(N)))) and then inserts a random key, iterating the loop N2 times.
?I just need the main method edited so it does this. Here is my current main method:
public static void main (String [] args)
{
BST <String, Integer> st = new BST <String, Integer> ();
File fd = new File (args [0]);
System.out.println("Enter Integer N: ");
In StdIn = new In (fd);
for (int i = 0 ; !StdIn.isEmpty () ; i++ )
{
String key = StdIn.readString ();
st.put (key, i);
}
for (String s : st.levelOrder ())
StdOut.println (s + " " + st.get (s));
StdOut.println ();
for (String s : st.keys ())
StdOut.println (s + " " + st.get (s));
}
Explanation / Answer
Please find the modified code below.
public static void main (String [] args)
{
BST <String, Integer> st = new BST <String, Integer> ();
File fd = new File (args [0]);
System.out.println("Enter Integer N: ");
In StdIn = new In (fd);
for (int i = 0 ; !StdIn.isEmpty () ; i++ )
{
String key = StdIn.readString ();
st.put (key, i);
}
// delete a random key from BST
st.delete(select(StdRandom.uniform(N))));
// inserting a random key
st.insert(select(StdRandom.uniform(N))))
for (String s : st.levelOrder ())
StdOut.println (s + " " + st.get (s));
StdOut.println ();
for (String s : st.keys ())
StdOut.println (s + " " + st.get (s));
}