首页 > 其他分享 >数据结构 玩转数据结构 7-7 基于二分搜索树的映射实现

数据结构 玩转数据结构 7-7 基于二分搜索树的映射实现

时间:2023-01-02 11:55:17浏览次数:56  
标签:map word 映射 System 玩转 words println 数据结构 out

0    课程地址

https://coding.imooc.com/lesson/207.html#mid=13709

 

1    重点关注

1.1    使用二叉树实现映射Map

详见3.1用二叉树实现映射Map

 

 

2    课程内容


 

3    Coding

3.1    使用二叉树实现映射Map

  • 需求

使用二叉树实现的映射统计 傲慢与偏见 书的英文词汇量

 

  • Map接口
package com.company;

public interface Map<K,V> {

    /**
     *  是否为空
     * @author weidoudou
     * @date 2022/12/26 17:50
     * @return boolean
     **/
    boolean isEmpty();

    /**
     * 获取元素个数
     * @author weidoudou
     * @date 2022/12/26 17:51
     * @return int
     **/
    int getSize();

    /**
     * 是否包含
     * @author weidoudou
     * @date 2022/12/26 17:52
     * @param key 请添加参数描述
     * @return boolean
     **/
    boolean contains(K key);

    /**
     * 获取元素
     * @author weidoudou
     * @date 2022/12/26 17:59
     * @param key 请添加参数描述
     * @return V
     **/
    V get(K key);

    /**
     * 新增元素
     * @author weidoudou
     * @date 2022/12/26 17:53
     * @param key 请添加参数描述
     * @param  value 请添加参数描述
     * @return void
     **/
    void add(K key,V value);

    /**
     * 修改元素
     * @author weidoudou
     * @date 2022/12/26 17:54
     * @param key 请添加参数描述
     * @param  value 请添加参数描述
     * @return void
     **/
    void set(K key,V value);

    /**
     * 删除元素
     * @author weidoudou
     * @date 2022/12/26 17:55
     * @param key 请添加参数描述
     * @return V
     **/
    V remove(K key);
}

 

  • BSTMap
package com.company;

import java.util.ArrayList;

public class Main {

    /*public static void main(String[] args) {
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt",words)){
            System.out.println("total words is "+words.size());
        }

        Map<String,Integer> map = new LinkedListMap<>();
        for(String word : words){
            if(map.contains(word)){
                map.set(word,map.get(word)+1);
            }else{
                map.add(word,1);
            }
        }

        System.out.println("different words is "+map.getSize());
        System.out.println("pride size is"+map.get("pride"));
        System.out.println("prejudice size is"+map.get("prejudice"));


        // write your code here
    }*/

    public static void main(String[] args) {
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt",words)){
            System.out.println("total words is "+words.size());
        }

        Map<String,Integer> map = new BSTMap<>();
        for(String word : words){
            if(map.contains(word)){
                map.set(word,map.get(word)+1);
            }else{
                map.add(word,1);
            }
        }

        System.out.println("different words is "+map.getSize());
        System.out.println("pride size is"+map.get("pride"));
        System.out.println("prejudice size is"+map.get("prejudice"));


        // write your code here
    }
}

 

 

  • 文件处理类:
package com.company;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Locale;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.IOException;

// 文件相关操作
public class FileOperation {

    // 读取文件名称为filename中的内容,并将其中包含的所有词语放进words中
    public static boolean readFile(String filename, ArrayList<String> words){

        if (filename == null || words == null){
            System.out.println("filename is null or words is null");
            return false;
        }

        // 文件读取
        Scanner scanner;

        try {
            File file = new File(filename);
            if(file.exists()){
                FileInputStream fis = new FileInputStream(file);
                scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
                scanner.useLocale(Locale.ENGLISH);
            }
            else
                return false;
        }
        catch(IOException ioe){
            System.out.println("Cannot open " + filename);
            return false;
        }

        // 简单分词
        // 这个分词方式相对简陋, 没有考虑很多文本处理中的特殊问题
        // 在这里只做demo展示用
        if (scanner.hasNextLine()) {

            String contents = scanner.useDelimiter("\\A").next();

            int start = firstCharacterIndex(contents, 0);
            for (int i = start + 1; i <= contents.length(); )
                if (i == contents.length() || !Character.isLetter(contents.charAt(i))) {
                    String word = contents.substring(start, i).toLowerCase();
                    words.add(word);
                    start = firstCharacterIndex(contents, i);
                    i = start + 1;
                } else
                    i++;
        }

        return true;
    }

    // 寻找字符串s中,从start的位置开始的第一个字母字符的位置
    private static int firstCharacterIndex(String s, int start){

        for( int i = start ; i < s.length() ; i ++ )
            if( Character.isLetter(s.charAt(i)) )
                return i;
        return s.length();
    }
}

 

  • 测试类:
package com.company;

import java.util.ArrayList;

public class Main {

    /*public static void main(String[] args) {
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt",words)){
            System.out.println("total words is "+words.size());
        }

        Map<String,Integer> map = new LinkedListMap<>();
        for(String word : words){
            if(map.contains(word)){
                map.set(word,map.get(word)+1);
            }else{
                map.add(word,1);
            }
        }

        System.out.println("different words is "+map.getSize());
        System.out.println("pride size is"+map.get("pride"));
        System.out.println("prejudice size is"+map.get("prejudice"));


        // write your code here
    }*/

    public static void main(String[] args) {
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt",words)){
            System.out.println("total words is "+words.size());
        }

        Map<String,Integer> map = new BSTMap<>();
        for(String word : words){
            if(map.contains(word)){
                map.set(word,map.get(word)+1);
            }else{
                map.add(word,1);
            }
        }

        System.out.println("different words is "+map.getSize());
        System.out.println("pride size is"+map.get("pride"));
        System.out.println("prejudice size is"+map.get("prejudice"));


        // write your code here
    }
}

 

  • 测试结果:
total words is 125901
different words is 6530
pride size is53
prejudice size is11

Process finished with exit code 0

 速度快很多

标签:map,word,映射,System,玩转,words,println,数据结构,out
From: https://www.cnblogs.com/1446358788-qq/p/17019673.html

相关文章

  • rmap反向映射
    数据结构AV&AVC&VMAstructanon_vma{//AV是perVMA的structanon_vma*root;//指向祖宗(root)进程的anon_vmastructanon_vma*parent;//指向父进程的......
  • 数据结构-绪论
    一.数据结构在学什么1.如何用程序代码把现实世界的问题信息化2.如何用计算机高效的处理这些信息从而创造价值二.数据结构的基本概念1.数据数据是信息的载体,是描述事物......
  • 为什么要学数据结构?
    什么是数据结构?根据我看的课程,总结的讲数据结构,就是对数据一种预处理,仅用于解决一个问题“数据要选用怎样的排序方法”。线性结构简洁明了,但却太过笼统,后续不好处理树......
  • 对象到对象映射-AutoMapper
    AutoMapper是一个对象-对象映射器,可以将一个对象映射到另一个对象。用来解决一个看似复杂的问题,这种类型的代码编写起来相当枯燥乏味,官网地址:​​http://automapper.org/​......
  • Algorithm 3 - 数据结构
    数据结构是该好好补补拉。1.线段树2.平衡树3.莫队3.1普通莫队莫队解决的问题一般是区间查询,并且可以离线。利用一种排序区间的方式,保证暴力移动最有指针的复杂度......
  • 数据结构-堆
    手写堆堆排序问题描述:输入一个长度为n的整数数列,从小到大输出前m小的数。解决思路:与上面的写的思路一样实现对应的代码即可代码:#include<iostream>using......
  • 手把手教你玩转 Excel 数据透视表
    1. 什么是数据透视表数据透视表是一种可以快速汇总、分析大量数据表格的交互式分析工具。使用数据透视表可以按照数据表格的不同字段从多个角度进行透视,并建立交叉表格,用......
  • 数据结构与算法(C语言 严蔚敏)二
    前言有误的地方还请大家指出来,我会一一改正,也会在评论区置顶更正的记录;如果是因为不同的教材导致的错误,请把教材名、著作者、版次一并提供,供大家一起督促一起学习,本篇参考......
  • 数据结构之队列
    1.队列介绍队列是一个有序列表,可以用数组或者链表来实现。遵循先入先出的原则。即先存入队列的数据,要先取出。后存入的要后取出。2.数组模拟队列思路队列本身是有......
  • 华为路由器映射内部服务器到公网
    网络描述:客户内部有一台OA服务器,及提供外部使用,又供内部使用。在AR路由器上配置NATServer配置。拓扑如下:关键配置:AR1配置aclnumber2000 rule5permit interface......