文章目录

  • 1. 栈
    • - 基本用法
    • - 括号匹配
    • - 四则运算器
  • 2. 优先队列
    • - 基本用法
    • - 哈夫曼树
  • 3. 二叉树
    • - 数据结构
    • - 前序遍历
    • - 中序遍历
    • - 后序遍历
    • - 根据前中序生成二叉树
  • 4. 二叉排序树
    • - 插入
  • 5. 列表List
    • - 定义
    • - 函数

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);

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

  1. 王道论坛机试指南学习笔记(五)搜索

    文章目录 1. 枚举 - 简述 - 百鸡问题 2.2 BFS - 算法 - 迷宫问题 - 可乐问题 - 解题关键 3. 递归 - 思想 - 汉诺塔 - 素数环问题 - 图遍历 4. DFS - 算法 ...

  2. 王道论坛机试指南学习笔记(一)经典入门

    文章目录 1. 排序 2.1 快速排序 - 思想 - 复杂度 - 代码 2. 日期问题 2.1 数据结构 2.2 判断闰年 2.3 获取下一天 2.4 计算间隔日期 3. HashMap 3.1 内部 ...

  3. 王道论坛机试指南学习笔记(三)数学问题

    文章目录 1. 2的n次方 - 移位 - power 2. 特殊值 - 最大值 - 绝对值 3. 取整 4. 数位拆解 - 数学方法 - 代码方法 5. 进制转换 - n转10 - 10转n 6. G ...

  4. 王道论坛机试指南学习笔记(四)图论

    文章目录 1. 并查集 - 基本表示 - 集合的合并 - 查找根节点 - 查找连通分量个数 2. 最小生成树MST - 定义 - Kruskal算法 - java代码 3. 最短路径 - 定义 - F ...

  5. 考研机试准备--《王道论坛机试指南》学习笔记

    一.代码能力培养的层次结构 1.会编写(默写)经典程序的代码. 2.将自己的想法和抽象的问题转换为代码实现. 3.编写出的代码在大量的,多种多样的测试用例之前仍然具有健壮性. 二.常见概念 1.特殊判 ...

  6. 王道考研机试指南代码合集

    王道考研机试指南代码合集 github链接 王道考研机试指南的代码合集,附有一些笔记和感悟 文件夹中包括机试指南的pdf,笔记部分,和代码对应题目的列表 如发现任何问题欢迎在下面留言 更新: 最短路题 ...

  7. 王道考研机试指南第2版——题目提交链接

    王道考研机试指南第2版--题目链接 文章来自https://github.com/zguolee/WDAlgorithmsNote/blob/master/README.md 王道考研机试指南第2版- ...

  8. Istio服务网格实践指南 学习笔记(二) Istio架构

    个人学习Istio系列  学习笔记 Istio架构篇 本篇部分参考原书 https://jimmysong.io/istio-handbook/ 仅为个人学习笔记 这幅图中描述了以下内容: 1.Ist ...

  9. 王道出版的机试指南_《王道论坛计算机考研机试指南》试读版.pdf

    <王道论坛计算机考研机试指南>试读版 王道论坛 王道论坛计算机考研机试指南 王道论坛 2013.01.06 写在前面的话 各位王道的小崽子们,今天你们考完初试了,感觉解放了吧?轻松了吧?无 ...

最新文章

  1. 后序遍历的非递归算法python_二叉树后序遍历(递归与非递归)算法C语言实现...
  2. backdrop-filter 和filter 写出高斯模糊效果 以及两者区别
  3. 执行sql语句提示[Err] 1055 - Expression #2 of SELECT list is not in GROUP BY......错误的解决办法
  4. Taro+react开发(99):问答模块06实现加减
  5. Hibernate Criteria示例教程
  6. 1月全球Web服务器市场:Apache份额回升至41.64%
  7. 鸟瞰 Java 并发框架
  8. block(块元素)、inline(内联元素)的差别是什么?
  9. 2019-4-25 数组操作方法和练习
  10. Unhandled exception in script的解决方法
  11. KBL406-ASEMI整流桥KBL406
  12. uniapp取消ios软键盘上白色导航条
  13. hive自定义函数过滤emoj表情符
  14. Gitlab备份和恢复操作记录
  15. omap 3530 SPI配置(MCSPI发送数据正常,读取数据全为0)
  16. eclipse源码中文注释乱码问题解决方法
  17. 各浏览器User-Agent用户代理字符串整理
  18. vue组件化的理解与定义
  19. Python多线程下调用win32com包相关问题:pywintypes.com_error: (-2147221008, ‘尚未调用 CoInitialize。‘, None, None)问题处理
  20. 利用docker+雨巷云打造私有网盘之安装mysql5.6(1)

热门文章

  1. 基于tensorflow的mnist数据集手写字体分类level-1
  2. 数据库分表分页实现方案
  3. 1179 -- N!
  4. Cyanine5.5 phosphoramidite,5‘-terminal可用于在寡核苷酸合成器中直接标记
  5. [渝粤教育] 西南科技大学 土力学基础工程 在线考试复习资料(1)
  6. CodeForces - 1000D Yet Another Problem On a Subsequence
  7. Matlab Simulink RS-485 Modbus RTU协议串口通信实录
  8. netty框架及原理解析
  9. 投资理财 - 电影大空头观后感
  10. TPAMI 2022|基于最优传输理论的无监督图像重建学习