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;
        }
    }
  • 相关阅读:
    linux下文件的复制、移动与删除
    Hbase万亿级存储性能优化总结-配置
    hbase 读写和优化
    hive数据倾斜定位及处理
    flink初识及安装flink standalone集群
    【Linux】用less查看日志文件
    sqoop的详细使用及原理
    HBase删除数据的原理
    hbase数据加盐(Salting)存储与协处理器查询数据的方法
    Hbase内存磁盘大致关系
  • 原文地址:https://www.cnblogs.com/jpit/p/8309774.html
Copyright © 2011-2022 走看看