Exact string matching with Suffix Trie, adding child nodes to the suffix trie

I’m working on an Exact string matching with Suffix Trie, but I have problems in adding child nodes to the suffix trie as it keep returning null. I can’t figure out why my code keeps returning no matter I do anything.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SuffixTrie {

private SuffixTrieNode root = new SuffixTrieNode();


public SuffixTrieNode insert(String str, int startPosition) {
    SuffixTrieNode current = root;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        SuffixTrieNode child = current.getChild(c);
        if (child == null) {
            child = new SuffixTrieNode();
            current.addChild(c, child);
        }
        current = child;

    }
    current.addData(startPosition, 0);

    if (current.data == null) {
        current.data = new SuffixTrieData();
    }

    return current;
}


 public SuffixTrieNode get(String str) {
    SuffixTrieNode current = root;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        current = current.getChild(c);

        // If current is null at any point, it means the substring does not exist
        if (current == null) {
            return null;
        }
    }
    return current;
}



public static SuffixTrie readInFromFile(String fileName) {
    SuffixTrie trie = new SuffixTrie();
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        String line;
        int sentencePosition = 0;
        while ((line = br.readLine()) != null) {
            trie.insert(line, sentencePosition);
            sentencePosition++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return trie;

}
 }

 import java.util.HashMap;

 public class SuffixTrieNode {

SuffixTrieData data = null;
int numChildren = 0;
private final HashMap<Character, SuffixTrieNode> children;

public SuffixTrieNode() {
    this.children = new HashMap<>();
}

public SuffixTrieNode getChild(char label) {
    return this.children.get(label);
}


public void addChild(char label, SuffixTrieNode node) {
    this.children.put(label, node);
    numChildren++;
}

public void addData(int sentencePos, int characterPos) {
    if (data == null) {
        data = new SuffixTrieData();
    }
    data.addStartIndex(new SuffixIndex(sentencePos, characterPos));
}

public String toString() {
    if (data != null) {
        return data.toString();
    } else {
        return null;
    }
}
}

import java.util.*;

public class SuffixTrieDriver {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    String fileName = in.nextLine();
    Queue<String> ss = new ArrayDeque<>();

    String suffix = in.nextLine();

    while (!suffix.equals(""))
    {
        ss.offer(suffix);
        suffix = in.nextLine();
    }

    SuffixTrie st = SuffixTrie.readInFromFile(fileName);

    while (!ss.isEmpty()) {
        String s = ss.poll();
        SuffixTrieNode sn = st.get(s);
        System.out.println("[" + s + "]: " + sn);
    }
}
 }

import java.util.ArrayList;

public class SuffixTrieData {

private final ArrayList<SuffixIndex> startIndexes = new ArrayList<>();

public void addStartIndex(SuffixIndex startIndex) {
    startIndexes.add(startIndex);
}

public String toString() {
    return startIndexes.toString();
}
}


public class SuffixIndex {

int sentence;
int character;

public SuffixIndex(int sentence, int character) {
    this.sentence = sentence;
    this.character = character;
}

public int getSentence() {
    return sentence;
}

public void setSentence(int sentence) {
    this.sentence = sentence;
}

public int getCharacter() {
    return character;
}

public void setCharacter(int character) {
    this.character = character;
}

/**
 * The sentence and character position in the format sentence.character notation
 * @return the position.
 */
@Override
public String toString() {
    return sentence + "." + character;
}

}`

The above is all the classes I’ve got.

the output I get is this

  `SuffixTrie/data/Frank02.txt

and
the
, the
onster
ngeuhhh
ing? This

 [and]: null
 [the]: null
 [, the]: null
 [onster]: null
 [ngeuhhh]: null
 [ing? This]: null`

But the output I want is

 `[and]: [1.27, 1.87, 3.34, 3.153, 4.15, 6.70, 7.66, 7.148, 8.88, 9.100, 9.124, 9.177,      9.203, 10.17, 12.67, 12.91, 13.97, 13.114, 14.27, 14.90]

[the]: [0.58, 1.116, 3.51, 3.96, 5.39, 7.36, 7.48, 7.127, 8.1, 8.18, 8.76, 9.1, 9.89, 9.232, 9.253, 10.57, 10.74, 12.7, 12.22, 12.56, 12.158, 13.42, 13.65, 13.144, 14.1, 14.31, 14.146, 15.19, 15.69, 15.133, 15.184, 15.288, 15.302]
[, the]: [8.16]
[onster]: null
[monst]: null`

 I tried making adjustments  to`your text` addChild(), getChild() methods but didn't work.

New contributor

Wenori Naoshika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật