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

  • Home
  • SEARCH
  • 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 8752187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:11:11+00:00 2026-06-13T13:11:11+00:00

So I am traversing an BinaryTree and the nodes contain strings as well as

  • 0

So I am traversing an BinaryTree and the nodes contain strings as well as if the string appeared more than once while reading the file. I am only looking for the top 10 words that appeared most while reading the file, so essentially I am just comparing an int value.

My problem is I am trying to figure out a way to have an effective way to compare and insert a new node if their count value is greater. So say
I have a tree with…

   5
  /  \
 3    10
/       \

1 15

Say the array size is only size 3. I start at 1, since the array is empty till 5 it will look like after hitting 5…

[1],[3],[5]

When I reach 10 it is greater than everything but I would like to keep it sorted so I need to shift 3 to 1, 5 to 3, and 10 to 5. I want to know if there is a more effective way to do this then to shift after every higher number. It reads a text file with over 10k+ words, so I would like it to be as fast as possible.

If a different data structure would be better for this please let me know. I was thinking a queue or linkedlist but I figured array was less wasted space because it’s only size of 10. I am a student so be gentle too :-x.

  • 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-13T13:11:11+00:00Added an answer on June 13, 2026 at 1:11 pm

    One way would be to use a couple of data structures. A suggested way, using a Map and a Min-Heap would be as follows:

    • As you are reading words from the file, maintain its count in the hash map (word, word_count).
    • Maintain a min-heap of size 10. Whenever you update a word in the map, increase its count. Now compare this count with the top element of your min heap. If word_count > value_at_top, then replace the top element and heapify.
    • Once you’re done with reading from the file, this heap will contain the top 10 most frequent element in the heap
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While traversing from my current path , and searching for a file , I'm
Hi good people once again While busy traversing, iterating and manipulating my arrays from
I'm traversing an xml file as below: <?xml version=1.0 encoding=ISO-8859-1?> <wrapper> <site base=http://www.example1.co.uk/ name=example
I am traversing an XML file (that contains multiple tables) using XSLT. Part of
I'm traversing a text file line by line adjusting the text so that the
Is there a simple way (instead of traversing manually all the string, or loop
While there is a lot of good resources on traversing the DOM with jQuery
How to keep track of the level while traversing a binary tree in level
I have a recursive algorithm for traversing nodes in a document tree in tree
I need a file system walker that I could instruct to ignore traversing directories

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.