王道论坛机试指南学习笔记(二)数据结构

1. 栈

- 基本用法

  • 声明

    //栈的元素类型必须是Object的子类
    //如Integer, Boolean,不可以是int, boolean等
    Stack<Integer> s = new Stack<>();
    
  • 使用

    s.push(x);
    s.pop();
    //获取栈顶元素,不出栈
    x = s.peek();
    boolean isEmpty = s.empty();
    //获取靠近栈顶的值为x的元素下标
    index = s.search(x);
    

- 括号匹配

  • 思想

    • 将左括号入栈
    • 遇到右括号则将栈顶元素弹出,与左括号进行匹配
  • 代码

    public void match(String str){
        Stack<Integer> s = new Stack<>();
        char blank = ' ';
        char[] chlist = str.toCharArray();
        char[] result = new char[chlist.length];
        for (int i = 0; i < chlist.length; i++) {
            if (chlist[i]=='(') {
                s.push(i);
                result[i] = blank;
            }
            else if (chlist[i]==')'){
                if(s.empty()) result[i] = '?';
                else {
                    s.pop();
                    result[i] = blank;
                }
            }
            else result[i] = blank;
        }
        while (!s.empty()){
            result[s.peek()] = '$';
            s.pop();
        }
        System.out.println(s);
        System.out.println(str);
        System.out.println(result);
    }
    

- 四则运算器

  public float Calculate(){
      int index = 0, count = 0;
      char ch = ' ';
      s_opt.push('#');
      for (int i = 0; i < expression.length; i++) {
          ch = expression[i];
          System.out.println("# 当前字符为"+ch);
          // 如果当前字符为运算符
          if(map.containsKey(ch)){
              String n = new String(expression, index, count);
              System.out.println("--运算符,前一个数字为"+n);
              s_num.push(Float.parseFloat(n));
              count = 0;
              index = i+1;
              if(s_opt.empty() || map.get(ch)>=map.get(s_opt.peek())){
                  System.out.println("运算符优先级大");
                  s_opt.push(ch);
                  System.out.println("将运算符压栈,栈内算符:"+s_opt+"栈内数字:"+s_num);
              }
              // 如果当前运算符优先级较小,将栈内运算符取出,进行运算
              else{
                  System.out.println("运算符优先级小");
                  while (map.get(ch)<map.get(s_opt.peek())){
                      System.out.println("栈内算符:"+s_opt+"栈内数字:"+s_num);
                      char opt = s_opt.pop();
                      float x = s_num.pop();
                      float y = s_num.pop();
                      switch (opt){
                          case '*':
                              s_num.push(x*y);
                              System.out.println("执行乘法操作,当前栈内算符"+s_opt+"栈内数字:"+s_num);
                              break;
                          case '/':
                              s_num.push(y/x);
                              System.out.println("执行除法操作,当前栈内算符"+s_opt+"栈内数字:"+s_num);
                              break;
                          case'+':
                              s_num.push(x+y);
                              System.out.println("执行加法操作,当前栈内算符"+s_opt+"栈内数字:"+s_num);
                              break;
                          case '-':
                              s_num.push(y-x);
                              System.out.println("执行减法操作,当前栈内算符"+s_opt+"栈内数字:"+s_num);
                              break;
                          case '#':
                              break;
                      }
                  }
                  if(map.get(ch)>=map.get(s_opt.peek())){
                      s_opt.push(ch);
                  }
              }
          }
          else {
              System.out.println("--数字");
              count += 1;
          }
      }
      return s_num.peek();
  }

2. 优先队列

- 基本用法

  • 定义

    PriorityQueue<Integer> pQ = new PriorityQueue<>(3);
    
  • 操作

    pQ.add(1);
    //poll和peek都是取优先队列的队首元素
    //调用poll时将元素从队列中删除,peek只取值
    int a = pQ.poll();
    int b = pQ.peek();
    int size = pQ.size();
    pQ.remove(1);
    boolean b = pQ.contains(1);
    pQ.clear();
    //将队列转换为数组
    int[] arr = pQ.toArray();
    

- 哈夫曼树

public void calculateHuffman(){
    int[] arr = {1,2,2,5,9};
    int result = 0;
    PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(3);
    for (int i = 0; i < 5; i++) {
        priorityQueue.add(arr[i]);
    }
    while (priorityQueue.size()>1){
        int a = priorityQueue.poll();
        int b = priorityQueue.poll();
        priorityQueue.add(a+b);
        result += a+b;
    }
    System.out.println(result);
}

3. 二叉树

- 数据结构

public class BinaryTree {
    private char value;
    private int val;

    public BinaryTree left;
    public BinaryTree right;

    public BinaryTree(int n){
        this.value = '#';
        this.val = n;
        this.left = null;
        this.right = null;
    }
    public BinaryTree(char v){
        this.value = v;
        this.val = 0;
        this.left = null;
        this.right = null;
    }
}

- 前序遍历

public static void preOrder(BinaryTree t){
    System.out.print(t.getValue());
    if(t.left!=null){
        preOrder(t.left);
    }
    if(t.right!=null){
        preOrder(t.right);
    }
}

- 中序遍历

public static void inOrder(BinaryTree t){
    if(t.left!=null){
        inOrder(t.left);
    }
    System.out.print(t.getValue());
    if(t.right!=null){
        inOrder(t.right);
    }
}

- 后序遍历

public static void postOrder(BinaryTree t){
    if(t.left!=null){
        postOrder(t.left);
    }
    if(t.right!=null){
        postOrder(t.right);
    }
    System.out.print(t.getValue());
}

- 根据前中序生成二叉树

public static boolean transPreAndIn2Post(char[] pre, char[] in){
    if(pre.length!=in.length) return false;
    BinaryTree root = buildTree(pre, 0, pre.length-1, in, 0, in.length-1);
    if(root!=null){
        System.out.println("post order:");
        postOrder(root);
        return true;
    }
    return false;
}

private static BinaryTree buildTree(char[] pre, int l1, int r1, char[] in, int l2, int r2){
    BinaryTree bt = new BinaryTree(pre[l1]);
    int rootIndex = -1;
    for (int i = l2; i <= r2; i++) {
        if(in[i]==bt.getValue()){
            rootIndex = i;
            break;
        }
    }
    // 若左子树不为空
    if(rootIndex!=l2){
        bt.left = buildTree(pre, l1+1, l1+rootIndex-l2, in, l2, rootIndex-1);
    }
    // 若右子树不为空
    if(rootIndex!=r2){
        bt.right = buildTree(pre, l1+rootIndex-l2+1, r1, in, rootIndex+1, r2);
    }
    return bt;
}

4. 二叉排序树

- 插入

public static BinaryTree insertIntoBST(BinaryTree t, char ch){
        if(t == null){
            t = new BinaryTree(ch);
        }
        else if(t.getValue()>ch){
            t.left = insertIntoBST(t.left, ch);
        }
        else if(t.getValue()<ch){
            t.right = insertIntoBST(t.right, ch);
        }
        return t;
    }

5. 列表List

- 定义

//左侧必须声明数据类型,右侧可省略
List<String> list = new ArrayList<>();

- 函数

list.add("hello");
//获取下标为1的元素
list.get(1);
int size = list.size();
boolean b = list.contains("hello");
list.remove("hello");
//获取值为"hello"的元素下标
int index = list.indexOf("hello");
//截取下标0~2的子列表
List<String> list2 = list.subList(0, 2);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值