@前言
迎来了本学期的重头专业课——数据结构与算法分析。
这次我们的老师是来自英国威尔士亚伯大学(Aberystwyth University)的Fred Long教授!!!老师曾以访问学者的身份来到CMU!!!!!!有六本著作!!

@知识点随笔
【新思想】
java中操作的都是引用!!!
即便是实例化,其实也是新建了一个引用!
操作引用很方便,很少直接操作本体!
【LinkedList的AddToFront方法传递的变量】
应该是传递对象,而不是节点。传对象好一些。
【上下层之间没有大小关系】
【AVL是实现平衡树的一种原则,另外还有红黑树等等】
【平衡树的目的:避免因为不规则的插入,没有及时调整,导致二叉树的时间复杂度急剧上升】
【AVL的目的:原本的绝对平衡二叉树过于严格,应用价值不大,AVL树允许左右深度相差1】
【平衡因素的改变仅限于新节点到根节点的路径上】
【泛型】可能就是增强代码的重用性
【在格式基础上可以实现固定任意的T类】
【如何理解泛型的作用】
1.多用于集合,便于控制集合内的元素类型固定唯一
2.另,可以当做参数来理解,即一套体系可以让不同的类适用
【B树】
·牢记各个树的性质!
·对于任给的一串数字,有可能有多种B树
【对java泛型的补充理解】
泛型的操作对象是集合!Collection
为什么不规定泛型时输出需要强制转化~
因为不规定泛型时,编译器默认泛型是Object
1、泛型的类型参数只能是类类型(包括自定义类),不能是简单类型。
2、同一种泛型可以对应多个版本(因为参数类型是不确定的),不同版本的泛型类实例是不兼容的。
3、泛型的类型参数可以有多个。
4、泛型的参数类型可以使用extends语句,例如。习惯上成为“有界类型”。
5、泛型的参数类型还可以是通配符类型。例如Class


public class BoundedStack {private Person[] list;private int firstFree = 0;private static final int DEFAULT_MAXIMUM = 10;// 2 Constructorspublic BoundedStack() {list = new Person[DEFAULT_MAXIMUM];}public BoundedStack(int size) {list = new Person[size];}// push method, which can add a new element at the endpublic void push(Person element) {if (firstFree == list.length) {throw new StackOverflowError();}list[firstFree] = element;firstFree++;}// pop method to peek and delete the top itempublic Person pop() {Person temp;temp = peek();this.list[firstFree - 1] = null;this.firstFree--;return temp;}// isEmpty method to determine whether the stack is emptypublic boolean isEmpty() {return firstFree == 0;}// peek method to return the top itempublic Person peek() {return this.list[firstFree - 1];}// depth method to get the number of the items in the stackpublic int depth() {return this.firstFree;}// overwrite toStringpublic String toString(Person[] list) {String tempString = "";for (int i = 0; i < this.firstFree; i++)tempString += ("Name: " + list[i].getName() + " Number: " + list[i].getNum());return tempString;}// compare two stackspublic boolean equals(BoundedStack comparedStack) {if (this.firstFree != comparedStack.firstFree)return false;else {for (int i = 0; i < this.firstFree; i++) {if (this.list[i] != comparedStack.list[i])return false;}}return true;}// main method to test BoundedStack classpublic static void main(String args[]) {Person raven = new Person("Raven", 1);//Problem: using the same constructorPerson messi = new Person("Messi", 2);Person neymar = new Person("Neymar", 3);Person mars = new Person("Mars", 4);BoundedStack myStack = new BoundedStack();myStack.push(raven);myStack.push(messi);myStack.push(neymar);myStack.push(mars);System.out.println(myStack.toString(myStack.list));System.out.println("The depth is: " + myStack.depth());System.out.println("The top element is: " + myStack.peek().getName());myStack.pop();System.out.println(myStack.toString(myStack.list));System.out.println("Now, the depth is: " + myStack.depth());System.out.println("Now, The top element is: " + myStack.peek().getName());System.out.println("Equal Test");BoundedStack testStack=new BoundedStack();System.out.println(myStack.equals(testStack));testStack=myStack;System.out.println(myStack.equals(testStack));System.out.println("Over");}
}// Other classes
/*class Person {private String name;private int num;public Person(String name, int num) {this.name = name;this.num = num;}public String getName() {return this.name;}public int getNum() {return this.num;}
}
*/

2.队列


@SuppressWarnings("hiding")
public class BoundedQueue<Person> {private Person[] list;private static final int DEFAULT_MAXIMUM = 10;private int front = 0;private int back = -1;private int length = 0;public BoundedQueue() {this(DEFAULT_MAXIMUM);}@SuppressWarnings("unchecked")public BoundedQueue(int size) {list = (Person[]) new Object[size];}public void insert(Person tempPerson) throws Exception {if (this.length > list.length)throw new Exception("Queue is full!");if (back == list.length - 1)back = -1;list[++back] = tempPerson;length++;}public Person remove() throws Exception {if (this.length == 0)throw new Exception("Queue is empty!");Person tmp = list[front++];if (front == list.length)front = 0;length--;return tmp;}public Person getFront() throws Exception {if (this.length == 0)throw new Exception("Queue is empty!");return list[front];}/*public boolean equals() {}*/public int length() {return this.length;}public boolean isEmpty() {return (length == 0);}public boolean isFull() {return (length == list.length);}
}

3.单向链表


/*** @author Raven Xu**/
public class MyLinkedList<E> {private Node<E> first = new Node<E>();//private int length;public boolean isEmpty() {return first.getNext() == null;}public void addToFront(E temp) {Node<E> addedNode = new Node<E>();addedNode.setData(temp);addedNode.setNext(first.getNext());first.setNext(addedNode);}public void removeFromFront() {try {first.setNext(first.getNext().getNext());} catch (NullPointerException e) {throw e;}}public String toString() {StringBuilder tempString = new StringBuilder();for (Node<E> current = first.getNext(); current != null; current = current.getNext()) {tempString.append(current.getData() + "\n");}return tempString.toString();}public int length() {int tempLength = 0;for (Node<E> current = first.getNext(); current != null; current = current.getNext()) {tempLength++;}return tempLength;}public void addToBack(E temp) {Node<E> addedNode = new Node<E>(temp);Node<E> tempNode = new Node<E>();tempNode.setNext(first.getNext());while (tempNode.getNext() != null)tempNode=tempNode.getNext();tempNode.setNext(addedNode);}public void removeFromBack() {Node<E> tempNode = new Node<E>();tempNode.setNext(first.getNext());while (tempNode.getNext().getNext() != null)tempNode=tempNode.getNext();tempNode.setNext(null);}public Node<E> find(E element) {Node<E> current = first.getNext();while ((current != null) && !current.getData().equals(element)) {current = current.getNext();}return current;}public boolean equals(Object obj) {// Deal with the "obvious" casesif (obj == null)return false;if (obj == this)return true;if (!(obj instanceof MyLinkedList))return false;@SuppressWarnings("unchecked")MyLinkedList<E> otherList = (MyLinkedList<E>) obj;if (this.length() != otherList.length())return false;Node<E> current1 = this.getFront(); // the front node of this listNode<E> current2 = otherList.getFront();while (current1 != null) {if (!(current1.getData().equals(current2.getData()))) // assume equalsreturn false; // is definedcurrent1 = current1.getNext(); // method in Node classcurrent2 = current2.getNext(); // method in Node class}return true;}public Node<E> getFront() {return first.getNext();}public Node<E> getBack() {Node<E> tempNode = new Node<E>();tempNode.setNext(first.getNext());while (tempNode.getNext() != null)tempNode=tempNode.getNext();return tempNode;}@SuppressWarnings("hiding")private class Node<E> {private E data;private Node<E> next;public Node() {}public Node(E theData) {data=theData;}// @return the datapublic E getData() {return data;}// @param data the data to setpublic void setData(E data) {this.data = data;}// @return the nextpublic Node<E> getNext() {return next;}// @param next the next to setpublic void setNext(Node<E> next) {this.next = next;}public String toString() {return data.toString();}}//Test my LinkedListpublic static void main(String args[]) {MyLinkedList<Person> personList = new MyLinkedList<Person>();Person raven = new Person("Raven", 1);Person messi = new Person("Messi", 2);Person neymar = new Person("Neymar", 3);Person mars = new Person("Mars", 4);System.out.println("*****addtoFront Test*****");personList.addToFront(raven);personList.addToFront(messi);System.out.println(personList);System.out.println("*****addtoBack Test*****");personList.addToBack(neymar);personList.addToBack(mars);System.out.println(personList);System.out.println("*****getFront and getBack Test*****");System.out.println("Front: "+personList.getFront().toString()+"\n"+"Back: "+personList.getBack().toString());System.out.println("*****equals Test*****");MyLinkedList<Person> list = personList;System.out.println(list.equals(personList));System.out.println("*****length Test*****");System.out.println("List length: "+personList.length());System.out.println("*****find Test*****");System.out.println(personList.find(raven));System.out.println("*****removeFromFront and removeFromBack Test*****");personList.removeFromFront();personList.removeFromBack();System.out.println(personList);}
}/*class LinkedListItr<E> implements java.util.Iterator<E> {private Node<E> currentPosition;public LinkedListItr(Node<E> firstNode) {currentPosition = firstNode.getNext();}public boolean hasNext() {return currentPosition != null;}public E next() {E returnedObject = currentPosition.getData();currentPosition = currentPosition.getNext();return returnedObject;}public void remove() {throw new UnsupportedOperationException("The linked list has no remove method.");}
}
*/

4.双向链表


/** @author Raven**/
public class DoubleLinkedList<E> {private Node<E> first = new Node<E>();private Node<E> end = new Node<E>();private int theSize;private int modCount = 0;public DoubleLinkedList() {doClear();}public void Clear() {doClear();}private void doClear() {first = new Node<E>(null, null, null);end = new Node<E>(null, first, null);first.next = end;theSize = 0;modCount++;}public int size() {return theSize;}public boolean isEmpty() {return size()==0;}public boolean add(E x) {add(size(), x);return true;}public void add(int idx, E x) {addBefore(getNode(idx, 0, size()),x);}public E get(int idx) {return getNode(idx).data;}public E set(int idx, E newVal) {Node<E> p = getNode(idx);E oldVal= p.data;p.data=newVal;return oldVal;}public E remove(int idx) {return remove(getNode(idx));}private void addBefore(Node<E> p, E x) {Node<E> newNode = new Node<>(x, p.prev, p);newNode.prev.next = newNode;p.prev=newNode;theSize++;modCount++;}private E remove(Node<E> p) {p.next.prev=p.prev;p.prev.next=p.next;theSize--;modCount++;return p.data;}private Node<E> getNode(int idx){return getNode(idx, 0 ,size()-1);}private Node<E> getNode(int idx, int lower, int upper){Node<E> p;if(idx<lower||idx>upper)throw new IndexOutOfBoundsException();if(idx<size()/2) {p=first.next;for(int i=0;i<idx;i++)p=p.next;}else {p=end;for(int i=size();i>idx;i--)p=p.prev;}return p;}public String toString() {StringBuilder tempString = new StringBuilder();for (Node<E> current = first.getNext(); current != null; current = current.getNext()) {tempString.append(current.getData() + "\n");}return tempString.toString();}private static class Node<E> {private E data;private Node<E> prev;private Node<E> next;public Node() {}@SuppressWarnings("unused")public Node(E theData) {data = theData;}public Node(E theData, Node<E> thePrev, Node<E> theNext) {data = theData;prev = thePrev;next = theNext;}// @return the datapublic E getData() {return data;}// @param data the data to set@SuppressWarnings("unused")public void setData(E data) {this.data = data;}// @return the nextpublic Node<E> getNext() {return next;}// @param next the next to set@SuppressWarnings("unused")public void setNext(Node<E> next) {this.next = next;}// @return the previous node@SuppressWarnings("unused")public Node<E> getPrev() {return prev;}// @param next the next to set@SuppressWarnings("unused")public void setPrev(Node<E> prev) {this.prev = prev;}public String toString() {return data.toString();}}public static void main(String args[]) {DoubleLinkedList<Person> personList2 = new DoubleLinkedList<Person>();Person raven = new Person("Raven", 1);Person messi = new Person("Messi", 2);Person neymar = new Person("Neymar", 3);Person mars = new Person("Mars", 4);personList2.add(raven);personList2.add(messi);personList2.add(neymar);personList2.add(mars);System.out.println("Add Test");System.out.println(personList2.toString());}
}

5.二叉搜索树


/*** @author Raven**/
public class BinarySearchTree<E extends Comparable<E>> {/** The Binary root */private BinaryNode<E> root = null;// interfacespublic BinarySearchTree() {root = null;}public void makeEmpty() {root = null;}public boolean isEmpty() {return root == null;}public boolean contains(E x) {return contains(x, root);}/*public E findMin() {if (isEmpty())throw new UnderflowException();return findMin(root).element;}public E findMax() {if (isEmpty())throw new UnderflowException();return findMax(root).element;}*/public void insert(E x) {root = insert(x, root);}public void remove(E x) {root = remove(x, root);}// real methodspublic void printTree() {}private boolean contains(E x, BinaryNode<E> t) {if (t == null)return false;int compareResult = x.compareTo(t.element);if (compareResult < 0)return contains(x, t.left);else if (compareResult > 0)return contains(x, t.right);elsereturn true; // Match}/*** Internal method to find the smallest item in a subtree.(We use two methods to implement findMin and findMax)* @param t the node that roots the subtree.* @return node containing the smallest item.*/public BinaryNode<E> findMin(BinaryNode<E> t) {if (t == null)return null;else if (t.left==null) //it means that t has a left subtree, and there are some nodes are smaller than it.return t;return findMin(t.left);}/*** Internal method to find the largest item in a subtree.(We use two methods to implement findMin and findMax)* @param t the node that roots the subtree.* @return node containing the largest item.*/public BinaryNode<E> findMax(BinaryNode<E> t) {if (t != null)while (t.getRight() != null)t = t.getRight();return t;}/*** Internal method to insert a subtree.* @param x the item to insert.* @param t the node that roots the subtree.* @return the new root of the subtree.*/private BinaryNode<E> insert(E x, BinaryNode<E> t) {if (t == null)return new BinaryNode<>(x, null, null);int compareResult = x.compareTo(t.getElement());if (compareResult < 0)t.left = insert(x, t.left);else if (compareResult > 0)t.right = insert(x, t.right);else;return t;}/*** Internal method to remove from a subtree.* @param x the item to remove.* @param t the node that roots the subtree.* @return the new root of the subtree.*/private BinaryNode<E> remove(E x, BinaryNode<E> t) {if (t == null)return t; // Item not found; do nothingint compareResult = x.compareTo(t.getElement());if (compareResult < 0)t.left = remove(x, t.left);else if (compareResult > 0)t.right = remove(x, t.right);else if (t.left != null && t.right != null) {t.element = findMin(t.right).element;t.right = remove(t.element, t.right);} elset = (t.left != null) ? t.left : t.right;return t;}private static class BinaryNode<E> {private E element;private BinaryNode<E> left;private BinaryNode<E> right;// Constructors@SuppressWarnings("unused")public BinaryNode(E theElement) {this(theElement, null, null);}public BinaryNode(E theElement, BinaryNode<E> lt, BinaryNode<E> rt) {element = theElement;left = lt;right = rt;}public E getElement() {return element;}@SuppressWarnings("unused")public BinaryNode<E> getLeft() {return left;}public BinaryNode<E> getRight() {return right;}}public static void main(String args[]) {}
}

数据结构与算法分析学习笔记相关推荐

  1. 数据结构和算法分析学习笔记(三)--二叉查找树的懒惰删除(lazy deletion)

    这次的问题来自<数据结构与算法分析(C++描述)>的习题4.16,如下: -------------------------- 4.16  重做二叉查找树类以实现懒惰删除.注意,这将影响所 ...

  2. 算法分析学习笔记(二) - 栈和队列(上)

    一. 写在前面的话 本篇为"算法分析学习笔记"系列的第二篇,我们来聊一些跟基础数据结构有关的知识.对于网上无数的算法高手来说,这里讨论的东西都太小儿科了,但是作为一个系列的文章,我 ...

  3. 数据结构与算法学习笔记之 从0编号的数组

    数据结构与算法学习笔记之 从0编号的数组 前言 数组看似简单,但掌握精髓的却没有多少:他既是编程语言中的数据类型,又是最基础的数据结构: 一个小问题: 为什么数据要从0开始编号,而不是 从1开始呢? ...

  4. 数据结构与算法学习笔记之 提高读取性能的链表(上)

    数据结构与算法学习笔记之 提高读取性能的链表(上) 前言 链表(Linked list)比数组稍微复杂一点,在我们生活中用到最常见的应该是缓存,它是一种提高数据读取性能的技术,常见的如cpu缓存,浏览 ...

  5. 数据结构与算法-java笔记一 更新中

    数据结构与算法-java笔记一 更新中 数据结构与算法 什么是数据结构.算法 数据结构学了有什么用: 线性结构 数组 特点 应用 链表 存储结构 链表类型 单链表 双向链表 双向循环链表 链表与数组的 ...

  6. 数据结构与算法学习笔记——链栈

    数据结构与算法学习笔记(C语言) 链栈 在开始链栈的学习之前,我们先实现一下上一篇文章中提到的检查括号匹配的小程序,鉴于水平有限,本人就随便写一下代码好了,目标仅限于对功能的实现. /*用顺序栈这种数 ...

  7. 数据结构与算法学习笔记4:递归+分治法

    数据结构与算法学习笔记4 递归 斐波那契数列 青蛙跳台阶问题 链表倒序打印 分治法 二分查找/折半查找 Binary Search 题目1:快速幂 题目2:如何判断一个数是否为2的次幂 递归 指在函数 ...

  8. 数据结构与算法学习笔记15:最大流问题 / 二分图 / 有权无权二分图的匹配 / 匈牙利算法 / 银行家算法 / 稳定婚配

    数据结构与算法学习笔记15:最大流问题 / 二分图 / 有权无权二分图的匹配 / 匈牙利算法 / 银行家算法 / 稳定婚配 引入小题:最短路径 最大流问题(maximum flow problem) ...

  9. 数据结构与算法 学习笔记(5):字符串

    数据结构与算法 学习笔记(5)- 字符串 本次笔记记录了LeetCode中关于字符串的一些问题,并给出了相应的思路说明和代码.题目编号与LeetCode对应,方便查找. 题目1:LeetCode 13 ...

最新文章

  1. 分享Silverlight/WPF/Windows Phone一周学习导读(07月18日-07月24日)
  2. 再次“重新定义” 华为争做数据基础设施领航者
  3. android OEM unlocking分析
  4. 创业有很多种方式,方法,而且是形式多样
  5. python学习链接
  6. def python语言对照表_如何用python,华丽实现字典树?
  7. 使用 FUSE 开发自己的文件系统
  8. nginx源码阅读 ---- Event模块和配置的初始化
  9. 组建Forefront TMG独立陈列(上)-案例介绍与服务器准备
  10. 小车自动往返工作原理_自动化控制灌溉工作原理
  11. 超级详细的手把手教你使用Lighthouse更好推动项目性能优化,性能指标详解,优化方法,需要关注指标分析
  12. html 卫星地图显示地名,卫星图看:河南10个名字非常好听的县(区),你认识几个?...
  13. 威漫哨兵机器人_漫威:哨兵机器人天下无敌?这几位变种人就不把他们放在眼里...
  14. 郭盛华动真格了!新公司获百亿融资,网友:还招人不
  15. SAP 发出商品业务实操和配置(开具销售发票时确认成本)
  16. 为什么域名还会被DNS污染?域名被污染清洗方法!
  17. ads1256模块模拟测试_使用模拟进行测试
  18. 快速入门丨篇五:如何进行运动控制器输入/输出IO的应用?
  19. 对iis写权限的利用
  20. 2015年哈工大数理逻辑A期末考试参考答案(2)

热门文章

  1. 安卓手机玩游戏卡顿怎么解决_手机卡顿反应慢怎么解决?让手机保持流畅的几个小妙招...
  2. Angular Directive 详解
  3. MySQL入门篇-MySQL配置文件简介
  4. Delphi 10.2.3 精简版自动激活Embarcadero Delphi 10.2.3 v25.0.29899.2631 Lite v14.4
  5. Maya_动画渲染出每帧动画的设置
  6. 利用一根网线实现新电脑一键迁移文件和软件
  7. python pandas的统计方法
  8. windows7怎么设置鼠标灵敏度
  9. Delphi2007中的Label也可以透明显示的
  10. python 获取word文档页数