白话算法设计分析
上一文结束了排序算法和分治策略的一些基本操作,正所谓,工欲善其事,必先利其器。而这数据结构就是算法的工具了。本节内容包括有散列表,链表,栈,队列,树等一些基本的数据结构,而对于图,可合并堆(斐波那契堆),跳表,b树等一些高级数据结构,我们尽量在掌握贪心在来使用。话不多说,开整。
所有的数据据机构都是一种动态集合的概念,为了就是可以模拟数学上的集合操作。只是集合有一些特定的约束条件。
散列表
对于散列表来说,就是有一个超大集U,也就是全集。他的上下界是[0,n]
其中有一个S子集,表示数据,其中数据满足0<=ai<=n
现在需要将这些需要使用的数据存储起来,又必须要满足一部分集合的基本操作。这是时候就会用到散列表了。需要支持的操作右search,insert,delete;其他操作可按情况支持。
直接寻址散列表
这种最为简单,以集合中的一个数据为key,该数据的其他都为卫星数据,不影响散列表的工作。但是,如果是一个对象的话,一般是存放指向这个对象的指针,来避免数据的重复复制工作,仅需要改变指针就好。
现在了解了存放的是什么,这时候就要用什么来存放了,这里是直接寻址,又要满足insert操作,所以直接使用数组来存放数据就好,开辟一个n长度的数组,然后存放头指针,这里面就需要涉及链表这个数据结构了,假定现在并没有重复数据key,所以就是一个萝卜一个坑,直接读取存放就好,经需要让头指针指向这些数据就好,指向为空就表示没有数据。
Code
public class PointerTable<V> {
static class Node<V>{
V data;
Node<V> next;
}
private Node<V>[] nodes;
private int len;
public PointerTable() {
len = 0;
nodes = new Node[16];
for(int i = 0;i<nodes.length;i++){
nodes[i] = new Node<>();
}
}
private void insert(int key,V x){
if(key > 16){
throw new ArrayIndexOutOfBoundsException("越界");
}
Node temp = new Node();
temp.data = x;
nodes[key].next = temp;
}
private void delete(V x){
for(int i = 0;i<nodes.length;i++){
if(nodes[i].next == null){
continue;
}
if(nodes[i].next.data == x){
nodes[i].next = null;
}
}
}
private Node search(V x){
Node result = null;
for(int i = 0;i<nodes.length;i++){
if(nodes[i].next == null){
continue;
}
if(nodes[i].next.data == x){
result = nodes[i].next;
}
}
return result;
}
}
直接寻址的弊端一下就出现了,数据冲突,内存的高要求,这些都是弊端,所以就有扩容机制,链表来解决内存高的要求,而数据冲突则是通过散列函数来解决的,最后,你会发现,java中的hashMap就是一个很好的散列表,有很好的hash函数解决数据冲突问题,以及引用红黑树来查询数据,而不是链表。散列表的介绍就到此结束吧,后面也会右一些改进的代码出现。
tips: 集合的基本操作
包括有Insert(S,x) 表示可以插入数据
search(S,x) 查找数据并返回数据的指针
delete(S,x) 表示可以删除数据
minximum(S) 表示返回最小数据
Maximum(S) 表示返回最大数据
extract-max(S) 表示返回并删除最大数据
successor(S,x)一个查询操作,返回前缀码
predecessor(S,x)返回后缀码
散列表的使用 试题 算法提高 地图
炫炫发现了一张藏宝图,图是由一些点和一些连接两个点的边组成的,他知道了每一条边连接哪些点,现在他想知道每个点连了哪些边。点和边都从1开始编号。
输入格式
输入的第一行包含两个整数n, m,分别表示点的数量和边的数量
接下来m行,每行两个整数,第i行表示编号为i的边连接哪两个点。
输出格式
输出n行,每行若干个整数,第i行表示编号为i的点连接了哪些边。边的编号从大到小输出
样例输入
2 1
1 2
样例输出
1
1
这个就是散列表的典型例题,以n为数组,以边为链接,这样就可以知道那些点链接哪条边了,然后遍历数组,输出每个结点的链表就好,应为边本来就是升序出现的,所以在插入新的结点的时候就需要使用头插法,这样优先输出的就是后进入的,也就是模拟一个栈的操作
Code
import java.util.Scanner;
/**
* 地图问题 炫炫发现了一张藏宝图,图是由一些点和一些连接两个点的边组成的,他知道了每一条边连接哪些点,现在他想知道每个点连了哪些边。点和边都从1开始编号。
* }
* https://lx.lanqiao.cn/problem.page?gpid=T2663
*/
public class MapProblem {
static class Node{
int key;
Node next;
public Node(){
}
public Node(int key, Node next) {
this.key = key;
this.next = next;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
Node[] nodes = new Node[n+1];
for(int i = 1;i<nodes.length;i++){
nodes[i] = new Node(0,null);
}
for(int i = 1;i<=m;i++){
int prev = scanner.nextInt();
int next = scanner.nextInt();
insert(nodes[prev],i);
insert(nodes[next],i);
}
for(int i = 1;i< nodes.length;i++){
while(nodes[i].next!=null){
System.out.print(nodes[i].next.key+" ");
nodes[i] = nodes[i].next;
}
System.out.println();
}
}
private static void insert(Node head,int i){
Node temp = new Node(i,null);
temp.next = head.next;
head.next = temp;
}
}
时间复杂度还是渐进O(n^2)的,空间没有浪费也就是O(n+m)
链表
链表的解释我大致是忘了,满脑子都是怎么实现,这里我们就不使用其他简单的单链表,循环链表,还是选用常用的双向链表来实现这个数据结构,支持的操作还是上述的一些。
散列表的时候忘记了算个操作的时间复杂度,基本也都能看出,这里就跳过吧。
链表的指针在java中实现==>是一个对象,java中对象的传值就是传地址,也就是引用类型,所以,直接以对象最为指针实现。
Code
/**
* 双向链表
*/
public class PointerList {
public int len;
public Node head;
static class Node{
int key; // 关键字
Node prev; // 前驱
Node next; // 后继
public Node(int key, Node prev, Node next) {
this.key = key;
this.prev = prev;
this.next = next;
}
public Node(Node prev,Node next){
this.prev = prev;
this.next = next;
this.key = Integer.MIN_VALUE; // 默认是最小值
}
@Override
public String toString() {
return "Node{" +
"key=" + key +
'}';
}
}
public PointerList(){
this.len = 0;
this.head = new Node(null,null);
}
public Node getHead(){
return head;
}
public int getLength(){
return len;
}
public static Node createNode(int key,Node prev,Node next){
return new Node(key,prev,next);
}
/**
* 头插法
* @param x
* @return
*/
// 插人前结点
public boolean insert(int x){
return insert(1,x);
}
// 固定点插入
public boolean insert(int index, int x){
if(index>len+1 || index<=0){
return false;
}
Node node = createNode(x,null,null);
Node temp = getHead();
int i = 1;
while(i<index){
temp = temp.next;
i++;
}
if(temp.next!=null){
node.next = temp.next;
temp.next.prev = node;
temp.next = node;
node.prev = temp;
}else{
node.next = temp.next;
temp.next = node;
node.prev = temp;
}
len++;
return true;
}
/**
* 尾插法
* @param x
* @return
*/
public boolean insert_tail(int x){
return insert_tail(len+1,x);
}
public boolean insert_tail(int index,int x){
return true;
}
// 查询结点
public Node search(int x){
Node temp = getHead();
while(temp.next!=null && temp.key!=x){
temp = temp.next;
}
return temp.key == x?temp:null; // 如果找不到就是尾巴节点
}
// 删除结点
public void delete(int x){
Node search = search(x);
if(search == null){
return;
}
search.prev.next = search.next;
if(search.next!=null){
search.next.prev = search.prev;
}
len--;
}
public void readList(){
Node temp = getHead();
while(temp.next!=null){
System.out.print(temp.next+"->");
temp = temp.next;
}
System.out.println();
}
}
这里只解释插入结点,所以,其他的操作还得自己慢慢体会。
在一个新的结点进入之时,首先就是先调整新的结点,因为他不会破坏原来的链表,也就是让他的next指向插入点的后面,让他的prev指向插入点的前面。这样,新结点就处理好了。
之后就是让插入点后面的prev指向新的几点,让插入点前面的next指向新结点
这是有数据的情况,如果就一个头指针呢?这里就不说了,一些都在代码里面。伪代码的话好像更加特殊。。。
本来还想拿一个反转链表的题目来解释一下的,发现双向链表是多么苍白无力,就算了
栈
就是一种先进后出的结构,这里可以想想成弹夹,支持栈顶进栈顶出,线性栈我们这就不写了,使用链栈来模拟这个栈的数据结构。支持两个操作,一个是push也就是压栈或叫入栈,类似于insert操作,但是只能从栈顶操作。还有一个pop,也叫弹栈或是出栈,类似于delete操作,但是只能从栈定操作。
默认有图,脑补图
Code
/**
* 链栈
*/
public class PointerStack<T> {
public Node<T> top;
public int len;
static class Node<T>{
T key; // 关键字
Node<T> prev; // 前驱
Node<T> next; // 后继
public Node(T key, Node<T> prev, Node<T> next) {
this.key = key;
this.prev = prev;
this.next = next;
}
public Node(Node<T> prev, Node<T> next){
this.prev = prev;
this.next = next;
// this.key = new Object(); // 默认是最小值
}
@Override
public String toString() {
return "Node{" +
"key=" + key +
'}';
}
}
public PointerStack(){
this.len = 0;
this.top = new Node<T>(null,null);
}
public Node<T> getTop(){
return top;
}
public int getLength(){
return len;
}
public void push(T x){
Node<T> node = new Node<T>(x,null,null);
if(top.next!=null){
top.next.next = node;
}
node.prev = top.next;
top.next = node;
len++;
}
public T pop(){
if(top.next == null || len == 0){
throw new NullPointerException("空栈:栈下溢");
}
Node<T> result = top.next;
top.next.next = null;
top.next = top.next.prev;
len--;
return result.key;
}
public void readStack(){
while(top.next!=null){
System.out.print(pop()+"<-");
}
System.out.println();
}
}
这里的阅读栈是一个测试操作,当然,标准测试还是应该找好边界测试。
栈的运用地方可是无法想象的多,函数运行就是一个例子,这里自己脑补也就是函数中调函数,那么子函数一定在父函数的上面,将他们进栈考虑的话。题目的话还是后面补吧,太多了
队列
在栈的基础上可以改一下,进在尾巴进,而出在头出,这里一个标准的队列运用地方就是先来先服务调度算法,类似于收银操作,插入的是等待时间最短的,出去的是等待时间最长的,这才是对列的运用点,也就是说,让看见这种描述的时候,或者可以兑换成这种的时候,就可以直接使用队列了。这里的实现就不写了。给出了运用地方基本就是最好的记录。
树
树的运用地方那就更多了,文件中,函数中等等,什么是树呢,这个还是百度快,这里就不描述了。
这里展示树有多种存储状态,比如数组来实现树,堆就是一个满二叉树,而他的实现就是数组。还有链接的树,这里面也是需要使用结点的,结点包括有parent,left,right 三个指针,这里相对常用的树多加一个parent指针,也就是指向父节点的指针。
实现树是很简单的,只要分治和递归用的好,树就会变得很简单。
树节点定义
Code
static class Node{
int key;
Node parent;
Node left;
Node right;
}
这样就很简单了。
包括的基本操作可以有遍历,查高,创树等
像遍历就有好几种,这里介绍一下先序遍历,意思就是,先拿根节点然后左遍历,在右遍历。
Code
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
/**
* 二叉树
*/
public class PointerTree {
public int treeDeepth; // 树的深度
public Node root; // 树的根节点
static class Node{
int key;
Node parent;
Node left;
Node right;
@Override
public String toString() {
return "Node{" +
"key=" + key +
'}';
}
public Node(int key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
}
}
public void createTree(){
// 建树
Node n5 = new Node(2,null,null);
Node n9 = new Node(21,null,null);
Node n10 = new Node(5,null,null);
Node n3 = new Node(4,n10,null);
Node n7 = new Node(7,null,null);
Node n1 = new Node(12,n7,n3);
Node n4 = new Node(10,n5,n9);
root = new Node(18,n1,n4);
}
// 先序遍历
public void readTree(Node root){
// 遍历树
if(root == null){
return;
}
System.out.println(root);
readTree(root.left);
readTree(root.right);
}
public void readTreeWithStack(Node root){
// 深度优先遍历
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
Node pop = stack.pop();
System.out.println(pop);
if(pop.right!=null){
stack.push(pop.right);
}
if(pop.left!=null){
stack.push(pop.left);
}
}
}
public void readTreeWithQueue(Node root){
// 广度优先遍历
Queue<Node> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty()){
Node poll = queue.poll();
System.out.println(poll);
if(poll.left!=null){
queue.add(poll.left);
}
if(poll.right!=null){
queue.add(poll.right);
}
}
}
private int updateTreeDeepth(Node root){
if(root == null){
return 0;
}
return Math.max(updateTreeDeepth(root.left)+1,updateTreeDeepth(root.right)+1);
}
public int getTreeDeepth(){
treeDeepth = updateTreeDeepth(root);
return treeDeepth;
}
public Node getRoot(){
return root;
}
}
这里建树就没有使用中序遍历配合的建树方法,后面补齐,现在只是测试动态集合一些基本操作。上面的代码就是分治的一种表现,而得到书的高度无非就是左树的高度与右树的高度作比较+1.这个就不需要多讲解了。
时间有点匆忙,红黑树就后面在记录吧
在基本的数据结构掌握之后,就可以开始一些高级算法设计分析了。
后面的计划就是一整章的动态规划,从傻瓜式到原理解读,最后就是经典题型和经典动态规划的记录,明天好运。