Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1 Text Tree In this assignment you will create a tree that can store se

ID: 3705823 • Letter: E

Question

Exercise 1 Text Tree In this assignment you will create a tree that can store sentences. Each node stores a single word and may have 0 to N child nodes. The root node is empty and does not store any value. The add(.) method accepts a single sentence as String and breaks it up into words at each space. These words are then inserted into the tree such that they define a path starting at level 2. There is no limit to the number of levels a tree can have. In more detail: The method add(.) splits the sentence up by space into N words. These words are then inserted into the tree consisting of potentially existing nodes and newly created nodes. That is, the algorithm will match a node's value at level 1 to word 0, then match a child of the existing node to word 1, etc. until no match is found. At this point, new nodes are created as a descendant path for each following word in the sentence a word in this case consists of any characters other than space and may include symbols Example Insertion of "Good Morning!", "Good Night!", "Today is Wednesday!", "Today is Thursday!", "Today, it's raining. " will result in the following tree: Today, Good Today it's Morning! Night! is Wednesday. Thursday raining

Explanation / Answer

here is your code : --------->>>>>>>>>>>.

import java.util.*;

public class WordNode{
private String word;
private boolean leaf;
private List<WordNode> children;

public WordNode(String W){
  word = W;
  children = new ArrayList<>();
  leaf = true;
}

public boolean isLeaf(){
  return leaf;
}

public String getWord(){
  return word;
}

public List<WordNode> getChildren(){
  return children;
}

public void addChildren(WordNode node){
  for(int i = 0;i<children.size();i++){
   if(node.getWord().equals(children.get(i).getWord())){
    return;
   }
  }
  children.add(node);
  leaf = false;
}

public void setChildren(List<WordNode> child){
  children = child;
  if(children.size() > 0){
   leaf = false;
  }else{
   leaf = true;
  }
}
}

TextTree.java : --------------->>>>>>>

import java.util.*;

public class TextTree{
private WordNode root;
private int size_ = 0;
private int height_ = 0;

public TextTree(){
  root = new WordNode("    ");
}
public boolean isEmpty(){
  return root == null;
}
public void add(String sentence){
  String[] words = sentence.split(" ");
  root.addChildren(new WordNode(words[0]));
  WordNode temp = root;
  size_++;
  for(int i = 1;i<words.length;i++){
   List<WordNode> tchild = temp.getChildren();
   for(int j = 0;j<tchild.size();j++){
    if(words[i-1].equals(tchild.get(j).getWord())){
     temp = tchild.get(j);
     break;
    }
   }
   if(height_ < (i+1)){
    height_ = i+1;
   }
   temp.addChildren(new WordNode(words[i]));
   size_++;
  }
}
public boolean contains(String sentence){
  String[] words = sentence.split(" ");
  List<WordNode> prev = null;
  List<WordNode> temp = root.getChildren();
  for(int i = 0;i<words.length;i++){
   prev = temp;
   for(int j = 0;j<temp.size();j++){
    if(temp.get(i).getWord().equals(words[i])){
     temp = temp.get(i).getChildren();
     break;
    }
   }
   if(prev == temp){
    return false;
   }
  }

  return true;
}

public int height(){
  return height_;
}
public int size(){
  return size_;
}

public LeafIterator iterator(){
  return new LeafIterator(root);
}

public class LeafIterator implements java.util.Iterator<String>{
  private WordNode next;
  private int i = -1;
  private ArrayList<WordNode> qu;

  public LeafIterator(WordNode root){
   qu = new ArrayList<WordNode>();
   addQueue(root);
  }

  public void addQueue(WordNode root){
   List<WordNode> temp = root.getChildren();
   for(int i = 0;i<temp.size();i++){
    if(temp.get(i).isLeaf()){
     qu.add(temp.get(i));
    }else{
     addQueue(temp.get(i));
    }
   }
  }

  public boolean hasNext(){
   return (i+1) != qu.size();
  }

  public String next(){
   i++;
   next = qu.get(i);
   return next.getWord();
  }
}

//you can eleminate this main method i used it for testing
public static void main(String[] args) {
  String s1= "Good Morning";
  String s2= "Good Night";
  String s3= "Very Good after Noon brother";
  TextTree t = new TextTree();
  t.add(s1);
  t.add(s2);
  t.add(s3);
  System.out.println(" Height "+t.height());
  System.out.println(" Size "+t.size());
  LeafIterator it = t.iterator();
  while(it.hasNext()){
   System.out.println(" "+it.next());
  }
}
}