Answer to Question #101552 in Algorithms for Abu

Question #101552
Count Red Nodes. Write a program that computes the percentage of red nodes in a given red-black BST. Test your program by running at least 100 trials of the experiment of inserting N random keys into an initially empty tree, for N = 10^4, 10^5, 10^6, and formulate an hypothesis.

*It is experiment from textbook Algorithms, 3.3.42
1
Expert's answer
2020-01-21T03:45:05-0500

It wouldn't be too rude if one specifies the programming language and the edition next time.


> It is is experiment from

The most primitive way to do that is perhaps to download the official sample Java code, review the isBalanced() method, and slightly modify the unit loop.


private int count() {
    return count(root);
}

private int count(Node x) {
    if (x == null) return 0;
    return (!isRed(x) ? 1 : 0) + count(x.left) + count(x.right);
}

RedBlackBST<Integer, Integer> st = new RedBlackBST<Integer, Integer>();
for (int i = 1; i <= 1000000; i++) {
    Integer key = StdRandom.uniform(Integer.MAX_VALUE);
    while (st.contains(key)) {
        key = StdRandom.uniform(Integer.MAX_VALUE);
    }
    st.put(key, i);
    if (i == 10000 || i == 100000 || i == 1000000) {
        StdOut.printf("%8d %8.2f\n", i, 100.0 * st.count() / i);
    }
}
StdOut.println();

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
21.01.20, 13:54

Dear Abu, Questions in this section are answered for free. We can't fulfill them all and there is no guarantee of answering certain question but we are doing our best. And if answer is published it means it was attentively checked by experts. You can try it yourself by publishing your question. Although if you have serious assignment that requires large amount of work and hence cannot be done for free you can submit it as assignment and our experts will surely assist you.

Abu
21.01.20, 11:52

1.Ginny downloads and modifies an open source software program, then uploads the program with a different name. which type of software license is she most likely to be working under? 2. which of the following is an example of financial management software? 3. image-editing software allows you to? 4. digital audio software can be used to? 5. when you subscribe to software, no license is necessary. true or false.

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS