Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:00:37+00:00 2026-06-15T11:00:37+00:00

This is a homework assignment. I try to find the number of nodes in

  • 0

This is a homework assignment. I try to find the number of nodes in a tree structure that contains even number data values. The following is my failed attempt in a Java class named LinkedTree.

public int numEvenKeys() {
        return numEvenKeysTree(root);     
}

private static int numEvenKeysTree(Node root) {
        int num = 0;

        if (root == null)
            return 0;        
        else if (root.left != null || root.right != null)
            return num + numEvenKeysTree(root.left) 
                    + numEvenKeysTree(root.right);
        else if (root.key % 2 == 0)
            num = 1;

        return num;
}

Here is part of my main class:

public static void main(String[] args) {
                Scanner in = new Scanner(System.in);

                LinkedTree tree = new LinkedTree();
                tree.insert(7, "root node");
                tree.insert(9, "7's right child");
                tree.insert(5, "7's left child");
                tree.insert(2, "5's left child");
                tree.insert(8, "9's left child");
                tree.insert(6, "5's right child");
                tree.insert(4, "2's right child");
                   ...
                *** remove a node of your choice ***
                   ...
                System.out.print("number of nodes with even keys in this tree: ");
                System.out.println(tree.numEvenKeys());
                   ...
    } 

As a reference, here is the inner class node and class constructor:

private class Node {
    private int key;         // the key field
    private LLList data;     // the data items associated with this key
    private Node left;       // reference to the left child/subtree
    private Node right;      // reference to the right child/subtree
    private Node parent;     // reference to the parent

    private Node(int key, Object data, Node left, Node right, Node parent){
        this.key = key;
        this.data = new LLList();
        this.data.addItem(data, 0);
        this.left = left;
        this.right = right;
        this.parent = parent;
    }

    private Node(int key, Object data) {
        this(key, data, null, null, null);
    }
}

// the root of the tree as a whole
private Node root;

public LinkedTree() {
    root = null;
}

The tree has a structure of:

      7
     / \
    5   9
   / \ / 
  2  6 8
   \
    4

If I choose to remove node 7, the method should return 4. However, it returns 1 with my implementation. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:00:38+00:00Added an answer on June 15, 2026 at 11:00 am

    You got your conditions wrong.

    If the node is null, then the answer is 0.

    If the node is even, it should be 1 + the number of even nodes in the left subtree + the number of even nodes in the right subtree.

    If the node is odd, it should be 0 + the number of even nodes in the left subtree + the number of even nodes in the right subtree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is part of a homework assignment. I've got several questions asking find the
For homework I was assigned to make an AVL Tree data structure. I am
This is a homework assignment, I have to write a program that reads a
This is a homework assignment, just for all that want to know. I'm writing
So this is a homework assignment, there might be come constraints that are ridiculous
First, I would like to say that this is for a homework assignment and
I find myself oddly perplexed on this homework assignment. The idea is to create
I'm still not very good with data structures, but I have this homework assignment
I want to preface this by saying that this is a homework assignment. I
This is an homework assignment. To change the following recursive deep copy method into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.