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 7756355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:48:02+00:00 2026-06-01T12:48:02+00:00

I’m having trouble with a method I wrote to insert string words into a

  • 0

I’m having trouble with a method I wrote to insert string words into a binary tree. The below code is the method in question. Basically, the word is inserted if it is not yet in the tree (as a BinaryTreeNode), and if it is in the tree, its frequency (count variable within BinaryTreeNode) is increased by one. My problem is with the temporary variable searchWord. Defining it as a String creates a type mismatch and a statement that says getFrequency() is not defined for type String. The generic type T is only there as a placeholder – it too does not work. What should it therefore be defined as?

buildBinaryTree method:

public static void buildBinaryTree(String word) {
    //if word is already in tree
    if(wordTree.contains(word)) {
        //find existing word node
        T searchWord = wordTree.find(word);  //problem here

        //increment frequency by 1
        searchWord.setFrequency(searchWord.getFrequency() + 1);
    } else {
        //add word to tree
        System.out.println(word);
        wordTree.addElement(word);
    }
}

BinaryTreeNode constructor:

/**
 * Creates a new tree node with the specified data.
 * @param obj the element that will become a part of the new tree node
 */
BinaryTreeNode(T obj) {
   element = obj;
   left = null;
   right = null;
   frequency = 1;
}

Frequency get/set methods:

/**
 * Gets the frequency.
 * @return the frequency
 */
public int getFrequency() {
   return frequency;
}

/**
 * Sets the frequency.
 * @param frequency the frequency to set
 */
public void setFrequency(int frequency) {
   this.frequency = frequency;
}
  • 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-01T12:48:04+00:00Added an answer on June 1, 2026 at 12:48 pm

    After talking in chat, you should define a class that has both a String and int that you use as the type to place in the binary tree, to replace the type variable T. Then, you can define methods such as getString() to return the String, incrementFrequency() to add one to the frequency, etc. When you get an object out of the binary tree, it will be the right type to call these methods.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.