zoukankan      html  css  js  c++  java
  • MapNode

    package example.test.google;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TFK {
    
        public static void main(String[] args) {
    
            TFK test = new TFK();
            test.test();
        }
    
        public void test() {
            String sentence = "this is a test";
            Path path = Path.sentenceToPath(sentence);
    
            Node root = new Node();
    
            NodeOperate.addNode(root, path);
    
            sentence = "this is another test";
            path = Path.sentenceToPath(sentence);
            NodeOperate.addNode(root, path);
        }
    }
    
    class Node {
        String key;
        Node value;
        Map<String, Node> pair;
    }
    
    class NodeOperate {
    
        public static void addNode(Node node, Path path) {
    
            if (node == null || path == null) {
                return;
            }
    
            if (contains(node, path)) {
    
                Node next = findNode(node, path);
                if (next != null) {
                    updateNode(node);
                    addNode(next, path.next);
                }
            } else {
                Node next = new Node();
    
                saveNode(node, path, next);
    
                addNode(next, path.next);
            }
        }
    
        public static void addNode0(Node node, Path path) {
    
            if (node == null || path == null) {
                return;
            }
    
            if (contains(node, path)) {
                Node next = findNode(node, path);
                addNode(next, path.next);
            } else {
                Node next = new Node();
    
                if (node.key != null) {
                    updateNode(node);
                }
                saveNode(node, path, next);
    
                addNode(next, path.next);
            }
        }
    
        public static Node findNode(Node node, Path path) {
            if (node.key != null) {
                if (node.key.equals(path.word)) {
                    return node.value;
                }
            }
    
            return node.pair.get(path.word);
        }
    
        public static void saveNode(Node node, Path path, Node next) {
            if (node.pair != null) {
                node.pair.put(path.word, next);
            } else {
                node.key = path.word;
                node.value = next;
            }
        }
    
        public static void updateNode(Node node) {
            if (node.pair == null) {
                node.pair = new HashMap<>();
            }
    
            node.pair.put(node.key, node.value);
    
            node.key = null;
            node.value = null;
        }
    
        public static boolean contains(Node node, Path path) {
    
            if (node.pair != null) {
                return node.pair.containsKey(path.word);
            }
    
            if (node.key != null) {
                return node.key.equals(path.word);
            }
            return false;
        }
    }
  • 相关阅读:
    2020年目标检测大盘点 | ECCV大盘点(附论文&代码下载)
    Transformer再下一城!low-level多个任务榜首被占领,北大华为等联合提出预训练模型IPT
    opencv------->>>>>>打印点
    生信工具
    生物信息学练习1-综合使用软件-2
    生物信息学练习1-综合使用软件
    操作指南之下载数据
    安装生物信息学软件-HUMAnN2
    多样性指数介绍
    统计学基础知识-欧式距离与其他
  • 原文地址:https://www.cnblogs.com/jpit/p/8309774.html
Copyright © 2011-2022 走看看