Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone translate this code to C: import java.io.*; import java.lang.*; public class trie { public class node { char letter; // stores the letter

Could someone translate this code to C:

import java.io.*; import java.lang.*; public class trie { public class node { char letter; // stores the letter for each node boolean endsWord; // true if the letter ends a word node down; // points to the left most child of the node node right; // points to the right sibling of the node // constructor for node class public node(char c) { letter = c; endsWord = false; down = null; right = null; } // overloaded constructor needed for some declarations public node(char c, node r) { letter = c; endsWord = false; down = null; right = r; } } node root; // keep track of the root node position; // keep track of the position public int[] amountList = new int[26]; // keep track of the amount of each letter public trie() { root = position = null; } /* receives: a String str returns: a new string able to be stored in the tree gets rid of all spaces, punctuation, and numbers in str, converts it to lower case, and stores only words longer than three */ public String fixString(String str) { int index = 0; //starting index is 0 // convert the string to lower case str=str.toLowerCase(); // convert the String to an array of chars to easily // manipulate each char char[] myChars = str.toCharArray(); // holds the old String char[] newChars = new char[str.length()]; // will make up the new String // loop until every char in myChars is tested for( int x=0 ; x='a' && myChars[x]<='z') { newChars[index++]=myChars[x]; } } //return a String consisting of the characters in newChars return (new String(newChars)); } /* receives: a String str returns: nothing - inserts insert the String str into the tree starting at the end of the word. each letter is inserted, and based on the other elements in the list a new child is added to the list, or an existing child is used. */ public void insert(String str) { // call fixString to prepare it for insertion str = fixString(str); // return if str in null if(str==null) return; // keep track of each index in the String int index=0; // if root equals null, create an empty node (this will only happen the first time) if(root==null) { root = new node(' '); } // declare pointers needed node temp,tempBack,pred; temp = tempBack = pred = root; // loop until every index in str is inserted while(index temp.letter) { //find the correct spot to insert the letter based on //alphabetical order while(temp.letter != ' ' && str.charAt(index) > temp.letter) { tempBack = temp; temp = temp.right; } //if the letter is not present, make a new path //otherwise, do nothing, simply follow the correct path if(str.charAt(index) != temp.letter) { temp = new node(str.charAt(index), temp); tempBack.right = temp; } } } //keep track of each letter, to easily calculate probabilities later if(str.charAt(index)>=97) amountList[str.charAt(index)-97]++; index++; } //at this point it will always be at the start of a word temp.endsWord = true; } /* receives: a String str (the name of a file) returns: nothing - builds the tree function builds a tree by reading the file named fileName one string at a time, and calls insert() to add that word to the tree. */ public void setupTrie(String fileName) { //stores the current word from the file String word = new String(); try { //create a FileReader of the file name fileName FileReader read = new FileReader(fileName); //create a BufferedReader using FileReader read //of size 50 BufferedReader in = new BufferedReader(read,50); //while there are String left to read, read them //and insert them one by one while(in.ready()) { word=in.readLine(); insert(word); } } catch (Exception e) { //catch exception cause by reading the file e.printStackTrace(); return; } } //recursively print each node, and under that print each child of the node //if the node starts a word, a '^' is printed after it. this is used just //for testing. public void print() { node t=root; System.out.println(root.letter + " *"); print(t.down); } void print(node t) { if(t==null) return; //System.out.println(t.letter + " children"); node temp=t; while(t!=null) { System.out.print(t.letter + " "); if(t.endsWord)System.out.print("^"); t=t.right; } System.out.println("*"); t=temp; while(t!=null) { if(t.down!=null && t.right!=null) { System.out.println(t.letter + " children"); print(t.down); } t=t.right; } } //reset the positition by pointing it to root public void resetPosition() { position = root; } /* receives: a String str returns: true if str is found, otherwise false follows the tree until the start until the prefix is found. it doesn't matter if str is a full word or not. */ public boolean stringInTrie(String str) { //node to traverse the tree node t = root; //loop until ever index in str is searched for(int index=0;index                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

Students also viewed these Databases questions

Question

What are the benefits of a business plan for a company?

Answered: 1 week ago