Giter Site home page Giter Site logo

manasesjesus / word-counter-binary-tree Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 254 KB

Read text from a file, split it into words and build a binary tree where each leaf node represents a unique word.

License: MIT License

Java 100.00%
java binary-tree code-challenge

word-counter-binary-tree's Introduction

Word Counter Binary Tree - Code Challenge

Write a Java-based program (command line) that reads text from a file, splits it into words at spaces and newline characters and constructs an (unbalanced) binary tree where each leaf node represents a unique word.

The tree construction shall start by creating a node for each unique word, where a node has a field to keep track of the occurrence count. The algorithm starts with the two least occurring nodes and creates a parent node. The parent node gets assigned an occurrence count that is the sum of the word occurrences. The process then repeats, i.e., it locates the two nodes with the least occurrence count, creates a parent node, and so on, until all nodes are part of the tree.

Finally, print the tree to the console (very basic output is sufficient).

For example, the text “She had had to address address problems” results in this tree (note that there are multiple variants):

Word Counter Binary Tree

Solution

The application receives a filename as a parameter. It is then parsed to get the words and number of occurrences stored in a map which is used to build the tree. Optionally, elements to be ignored can be passed as parameters, e.g. ( ) . ,

By default it is not case sensitive, but it is possible to call the WordSplitter ignoreCase method and set it to false.

Execution:
WordCounterBinaryTree filename [elements to be ignored]

Examples

Execution and output 1:

$ WordCounterBinaryTree  resources/test01.txt  

7
├──4
│  ├──1  [ address ]
│  └––3
│     ├──1  [ (had) ]
│     └––2
│        ├──1  [ she ]
│        └––1  [ had, ]
└––3
   ├──1  [ (address); ]
   └––2
      ├──1  [ problems... ]
      └––1  [ to ]

Execution and output 2:

$ WordCounterBinaryTree  resources/test01.txt ( ) . , ; 

7 
├──3 
│  ├──1  [ problems ] 
│  └––2 
│     ├──1  [ to ] 
│     └––1  [ she ] 
└––4 
   ├──2  [ address ] 
   └––2  [ had ] 

word-counter-binary-tree's People

Contributors

codacy-badger avatar manasesjesus avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.