Write a method countNodes() of class RedBlackTree (from http://users.cis.fiu.edu
ID: 3630713 • Letter: W
Question
Write a method countNodes() of class RedBlackTree(from http://users.cis.fiu.edu/~weiss/dsaajava2/code/RedBlackTree.java)
that runs in O(n) time and counts the number of red and black nodes in the tree.
Explanation / Answer
See the code below where the method is added using a recursive implementation. The method still runs in O(n) time. // RedBlackTree class // // CONSTRUCTION: with no parameters // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x (unimplemented) // boolean contains( x ) --> Return true if x is found // Comparable findMin( ) --> Return smallest item // Comparable findMax( ) --> Return largest item // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // void printTree( ) --> Print all items // ******************ERRORS******************************** // Throws UnderflowException as appropriate /** * Implements a red-black tree. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */ public class RedBlackTreeRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.