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;
        }
    }
  • 相关阅读:
    SharePoint Framework (SPFx) 开发入门教程
    SharePoint 2013 Designer 入门教程
    SharePoint 2013 开发教程
    SharePoint 2013 入门教程
    SharePoint Online 部署SPFx Web部件
    SharePoint Online SPFx Web部件绑定数据
    SharePoint Online 创建SPFx客户端Web部件
    SharePoint Online 配置框架(SPFx)开发环境
    SharePoint Online 创建应用程序目录
    SharePoint Online 启用 IRM
  • 原文地址:https://www.cnblogs.com/jpit/p/8309774.html
Copyright © 2011-2022 走看看