Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

purebredOdds

Language/Type: Java binary trees implementing IntTree
Related Links:
Author: Stuart Reges (on 2020/11/12)

Write a method called purebredOdds that returns the number of purebred odd values in a binary tree of integers. A purebred odd is an odd number all of whose ancestors in the tree are also odd. For example, suppose that a variable t refers to the following tree:

                                  +----+
                                  | 15 |
                                  +----+
                               /          \
                       +----+                +----+
                       | 27 |                | 34 |
                       +----+                +----+
                      /      \                     \
                 +----+      +----+                +----+
                 | 29 |      | 75 |                | 83 |
                 +----+      +----+                +----+
                            /      \              /      \
                       +----+      +----+    +----+      +----+
                       | 18 |      | 33 |    | 46 |      | 51 |
                       +----+      +----+    +----+      +----+

Then the call t.purebredOdds() should return 5 because this tree has a total of 5 purebred odd values: 15, 27, 29, 75, and 33. The values 83 and 51 are odd numbers, but not purebred odd numbers because they have an even number as an ancestor in the tree (the value 34). Notice that if the overall root is an even number, then the tree will have no purebred odd numbers at all because every other node has the overall root as an ancestor.

You are writing a public method for a binary tree class defined as follows:

        public class IntTreeNode {
            public int data;          // data stored in this node
            public IntTreeNode left;  // reference to left subtree
            public IntTreeNode right; // reference to right subtree

            
        }

        public class IntTree {
            private IntTreeNode overallRoot;

            
        }

You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class. You may not define any auxiliary data structures to solve this problem.

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.