@前言
迎来了本学期的重头专业课——数据结构与算法分析。
这次我们的老师是来自英国威尔士亚伯大学(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 Constructors
public BoundedStack() {
list = new Person[DEFAULT_MAXIMUM];
}
public BoundedStack(int size) {
list = new Person[size];
}
// push method, which can add a new element at the end
public void push(Person element) {
if (firstFree == list.length) {
throw new StackOverflowError();
}
list[firstFree] = element;
firstFree++;
}
// pop method to peek and delete the top item
public Person pop() {
Person temp;
temp = peek();
this.list[firstFree - 1] = null;
this.firstFree--;
return temp;
}
// isEmpty method to determine whether the stack is empty
public boolean isEmpty() {
return firstFree == 0;
}
// peek method to return the top item
public Person peek() {
return this.list[firstFree - 1];
}
// depth method to get the number of the items in the stack
public int depth() {
return this.firstFree;
}
// overwrite toString
public 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 stacks
public 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 class
public static void main(String args[]) {
Person raven = new Person("Raven", 1);//Problem: using the same constructor
Person 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