一、符号表

  1. 符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据元素是一个键和一个值共同组成的**键值对**数据,我们可以根据键来查找对应的值。
  2. ![image-20200803223322922.png](https://cdn.nlark.com/yuque/0/2021/png/22217876/1631113238729-986131b7-8795-4c42-add4-047d264d08f5.png#clientId=ubed315e1-1b74-4&from=drop&id=u12e9d2aa&margin=%5Bobject%20Object%5D&name=image-20200803223322922.png&originHeight=577&originWidth=371&originalType=binary&ratio=1&size=45732&status=done&style=none&taskId=uea23342d-d17d-4cd3-8d76-223397409f6)<br />符号表中,**键具有唯一性**。

符号表在实际生活中的使用场景是非常广泛的,见下表:

应用 查找目的 键 值
字典 找出单词的释义 单词 释义
图书索引 找出某个术语相关的页码 术语 一串页码
网络搜索 找出某个关键字对应的网页 关键字 网页名称

1.1 符号表API设计

结点类:

类名 Node
构造方法 Node(Key key,Value value,Node next):创建Node对象
成员变量 1. public Key key: 存储键
2. public Value value: 存储值
3. public Node next:存储下一个结点

符号表:

类名 SymbolTable
构造方法 SymbolTable():创建SymbolTable对象
成员方法 1. public Value get(Key key):根据键key,找对应的值
2. public void put(Key key,Value val): 向符号表中插入一个键值对
3. public void delete(Key key): 删除键为key的键值对
4. public int size():获取符号表的大小
成员变量 1. private Node head: 记录首结点
2. private int N: 记录符号表中键值对的个数

1.2 符号表实现

package cn.itcast.algorithm.symbol;

public class SymbolTable<Key,Value> {
  //记录首结点
  private Node head;
  //记录符号表中元素的个数
  private int N;

  private class Node{
    //键
    public Key key;
    //值
    public Value value;
    //下一个结点
    public Node next;

    public Node(Key key, Value value, Node next) {
      this.key = key;
      this.value = value;
      this.next = next;
    }
  }

  public SymbolTable() {
    this.head = new Node(null,null,null);
    this.N=0;
  }

  //获取符号表中键值对的个数
  public int size(){
    return N;
  }

  //往符号表中插入键值对
  public void put(Key key,Value value){
    //符号表中已经存在了键为key的键值对,那么只需要找到该结点,替换值为value即可
    Node n = head;
    while(n.next!=null){
      //变换n
      n = n.next;
      //判断n结点存储的键是否为key,如果是,则替换n结点的值
      if (n.key.equals(key)){
        n.value = value;
        return;
      }
    }

    //如果符号表中不存在键为key的键值对,只需要创建新的结点,保存要插入的键值对,把新结点插入到链表的头部  head.next=新结点即可
    Node newNode = new Node(key, value, null);
    Node oldFirst = head.next;
    newNode.next = oldFirst;
    head.next = newNode;
    //元素个数+1;
    N++;

  }
  //删除符号表中键为key的键值对
  public void delete(Key key){
    //找到键为key的结点,把该结点从链表中删除
    Node n = head;
    while(n.next!=null){
      //判断n结点的下一个结点的键是否为key,如果是,就删除该结点
      if (n.next.key.equals(key)){
        n.next = n.next.next;
        N--;
        return;
      }
      //变换n
      n = n.next;
    }
  }

  //从符号表中获取key对应的值
  public Value get(Key key){
    //找到键为key的结点
    Node n = head;
    while(n.next!=null){
      //变换n
      n = n.next;
      if (n.key.equals(key)){
        return n.value;
      }
    }
    return null;
  }
}

测试符号表

package cn.itcast.algorithm.test;

import cn.itcast.algorithm.symbol.SymbolTable;

public class SymbolTableTest {
    public static void main(String[] args) {
        //创建符号表对象
        SymbolTable<Integer, String> symbolTable = new SymbolTable<>();

        //测试put方法(插入,替换)
        symbolTable.put(1,"乔峰");
        symbolTable.put(2,"虚竹");
        symbolTable.put(3,"段誉");

        System.out.println("插入完毕后,元素的个数为:"+symbolTable.size());

        symbolTable.put(2, "慕容复");
        System.out.println("替换完毕后的元素的个数为:"+symbolTable.size());

        //测试get方法
        System.out.println("替换完毕后,键2对应的值为:"+symbolTable.get(2));

        //测试删除方法
        symbolTable.delete(2);
        System.out.println("删除完毕后,元素的个数:"+symbolTable.size());
    }
}

1.3 有序符号表

刚才实现的符号表,我们可以称之为无序符号表,因为在插入的时候,并没有考虑键值对的顺序,而在实际生活中,有时候我们需要根据键的大小进行排序,插入数据时要考虑顺序,那么接下来我们就实现一下有序符号表。

代码实现:只需修改put方法

package cn.itcast.algorithm.symbol;

public class OrderSymbolTable<Key extends Comparable<Key>,Value> {
  //记录首结点
  private Node head;
  //记录符号表中元素的个数
  private int N;

  private class Node{
    //键
    public Key key;
    //值
    public Value value;
    //下一个结点
    public Node next;

    public Node(Key key, Value value, Node next) {
      this.key = key;
      this.value = value;
      this.next = next;
    }
  }

  public OrderSymbolTable() {
    this.head = new Node(null,null,null);
    this.N=0;
  }

  //获取符号表中键值对的个数
  public int size(){
    return N;
  }

  //往符号表中插入键值对
  public void put(Key key,Value value){
    //定义两个Node变量,分别记录当前结点和当前结点的上一个结点
    Node curr = head.next;
    Node pre = head;
    while(curr!=null && key.compareTo(curr.key)>0){
      //变换当前结点和前一个结点即可
      pre = curr;
      curr = curr.next;
    }
    //如果当前结点curr的键和要插入的key一样,则替换
    if (curr!=null && key.compareTo(curr.key)==0){
      curr.value = value;
      return;
    }
    //如果当前结点curr的键和要插入的key不一样,把新的结点插入到curr之前
    Node newNode = new Node(key, value, curr);
    pre.next = newNode;
    //元素的个数+1;
    N++;
  }

  //删除符号表中键为key的键值对
  public void delete(Key key){
    //找到键为key的结点,把该结点从链表中删除

    Node n = head;
    while(n.next!=null){
      //判断n结点的下一个结点的键是否为key,如果是,就删除该结点
      if (n.next.key.equals(key)){
        n.next = n.next.next;
        N--;
        return;
      }
      //变换n
      n = n.next;
    }
  }

  //从符号表中获取key对应的值
  public Value get(Key key){
    //找到键为key的结点
    Node n = head;
    while(n.next!=null){
      //变换n
      n = n.next;
      if (n.key.equals(key)){
        return n.value;
      }
    }
    return null;
  }
}

测试有序符号表:

package cn.itcast.algorithm.test;

import cn.itcast.algorithm.symbol.OrderSymbolTable;

public class OrderSymbolTableTest {

    public static void main(String[] args) {
        //创建有序符号表对象
        OrderSymbolTable<Integer, String> table = new OrderSymbolTable<>();

        table.put(1,"张三");
        table.put(2,"李四");
        table.put(4,"赵六");
        table.put(7,"田七");
 //在下面这行打断点,用debug模式,通过查看next属性,来确定元素顺序
        table.put(3,"王五");

    }
}