diff --git a/.idea/modules.xml b/.idea/modules.xml index 5080dff..d4dbc2f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,10 +2,15 @@ + + + + + \ No newline at end of file diff --git a/Discussion/.idea/$PROJECT_FILE$ b/Discussion/.idea/$PROJECT_FILE$ deleted file mode 100644 index 58b7e3e..0000000 --- a/Discussion/.idea/$PROJECT_FILE$ +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/Discussion/.idea/.gitignore b/Discussion/.idea/.gitignore deleted file mode 100644 index 2786094..0000000 --- a/Discussion/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/../../../../../../../../:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\Discussion\.idea/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/Discussion/.idea/checkstyle-idea.xml b/Discussion/.idea/checkstyle-idea.xml deleted file mode 100644 index 121d4a2..0000000 --- a/Discussion/.idea/checkstyle-idea.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Discussion/.idea/misc.xml b/Discussion/.idea/misc.xml deleted file mode 100644 index 881809e..0000000 --- a/Discussion/.idea/misc.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/Discussion/.idea/modules.xml b/Discussion/.idea/modules.xml deleted file mode 100644 index 8d3adfd..0000000 --- a/Discussion/.idea/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Discussion/.idea/qaplug_profiles.xml b/Discussion/.idea/qaplug_profiles.xml deleted file mode 100644 index 3dfd21f..0000000 --- a/Discussion/.idea/qaplug_profiles.xml +++ /dev/null @@ -1,465 +0,0 @@ - - - - - \ No newline at end of file diff --git a/Discussion/.idea/vcs.xml b/Discussion/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/Discussion/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Discussion/Discussion_01/Discussion_01.iml b/Discussion/Discussion.iml similarity index 100% rename from Discussion/Discussion_01/Discussion_01.iml rename to Discussion/Discussion.iml diff --git a/Discussion/Discussion_01/src/Discussion_01.java b/Discussion/Discussion_01/src/Discussion_01.java deleted file mode 100644 index e7df5b6..0000000 --- a/Discussion/Discussion_01/src/Discussion_01.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @author Li Zezhong - * @create 2021-12-02 15:29 - */ - - -public class Discussion_01 { - public static void main(String[] args) { - int[] inputArray = new int [] {3, 0, 4, 6, 3}; - System.out.println(mystery(inputArray,2)); - int [] inputArray2 =new int[]{3, 0, 4, 6, 3}; - mystery2(inputArray2); - System.out.println(java.util.Arrays.toString(inputArray2)); - System.out.println(Fibonacci.fib(3)); - } - public static int mystery(int[] inputArray, int k){ - int x = inputArray[k]; - int answer = k; - int index = k+1; - while(index - - - - - - \ No newline at end of file diff --git a/Homework/.idea/.gitignore b/Homework/.idea/.gitignore deleted file mode 100644 index 4f9af22..0000000 --- a/Homework/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/../../../../../../../../:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\Homework\.idea/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/Homework/.idea/checkstyle-idea.xml b/Homework/.idea/checkstyle-idea.xml deleted file mode 100644 index 121d4a2..0000000 --- a/Homework/.idea/checkstyle-idea.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Homework/.idea/misc.xml b/Homework/.idea/misc.xml deleted file mode 100644 index 881809e..0000000 --- a/Homework/.idea/misc.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/Homework/.idea/modules.xml b/Homework/.idea/modules.xml deleted file mode 100644 index 1313959..0000000 --- a/Homework/.idea/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Homework/.idea/qaplug_profiles.xml b/Homework/.idea/qaplug_profiles.xml deleted file mode 100644 index 3dfd21f..0000000 --- a/Homework/.idea/qaplug_profiles.xml +++ /dev/null @@ -1,465 +0,0 @@ - - - - - \ No newline at end of file diff --git a/Homework/.idea/vcs.xml b/Homework/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/Homework/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Homework/.idea/Homework.iml b/Leetcode_practice/Leetcode_practice.iml similarity index 76% rename from Homework/.idea/Homework.iml rename to Leetcode_practice/Leetcode_practice.iml index 6b648a4..28ac7fd 100644 --- a/Homework/.idea/Homework.iml +++ b/Leetcode_practice/Leetcode_practice.iml @@ -7,7 +7,9 @@ - + + + diff --git a/Leetcode_practice/src/KMP_practive.java b/Leetcode_practice/src/KMP_practive.java new file mode 100644 index 0000000..6fb1954 --- /dev/null +++ b/Leetcode_practice/src/KMP_practive.java @@ -0,0 +1,84 @@ +import java.sql.SQLOutput; + +/** + * @author Li Zezhong + * @create 2022-06-29 19:29 + */ +public class KMP_practive { + public static void main(String[] args) { + String txt = "ABABDABACDABABCABABCABAB"; + String pat = "ABABCABAB"; + KMPSearch(pat, txt); + } + + static void KMPSearch(String pat, String txt) { + int M = pat.length(); + int N = txt.length(); + + // create lps[] that will hold the longest + // prefix suffix values for pattern + int lps[] = new int[M]; + int j = 0; // index for pat[] + + // Preprocess the pattern (calculate lps[] + // array) + computeLPSArray(pat, M, lps); + + int i = 0; // index for txt[] + while (i < N) { + if (pat.charAt(j) == txt.charAt(i)) { + j++; + i++; + } + if (j == M) { + System.out.println("Found pattern " + + "at index " + (i - j)); + j = lps[j - 1]; + } + + // mismatch after j matches + else if (i < N && pat.charAt(j) != txt.charAt(i)) { + // Do not match lps[0..lps[j-1]] characters, + // they will match anyway + if (j != 0) + j = lps[j - 1]; + else + i = i + 1; + } + } + } + + static void computeLPSArray(String pat, int M, int lps[]) + { + // length of the previous longest prefix suffix + int len = 0; + int i = 1; + lps[0] = 0; // lps[0] is always 0 + + // the loop calculates lps[i] for i = 1 to M-1 + while (i < M) { + if (pat.charAt(i) == pat.charAt(len)) { + len++; + lps[i] = len; + i++; + } + else // (pat[i] != pat[len]) + { + // This is tricky. Consider the example. + // AAACAAAA and i = 7. The idea is similar + // to search step. + if (len != 0) { + len = lps[len - 1]; + + // Also, note that we do not increment + // i here + } + else // if (len == 0) + { + lps[i] = len; + i++; + } + } + } + } +} \ No newline at end of file diff --git a/Leetcode_practice/src/ListNode.java b/Leetcode_practice/src/ListNode.java new file mode 100644 index 0000000..d81e293 --- /dev/null +++ b/Leetcode_practice/src/ListNode.java @@ -0,0 +1,57 @@ +import java.util.Arrays; + +/** + * @author Li Zezhong + * @create 2022-05-14 9:44 + */ +public class ListNode { + public int val; + public ListNode next; + public ListNode last; + public int size; + public ListNode(int val){ + this.next = null; + this.val = val; + this.last = this; + this.size = 1; + } + + public ListNode(int val,ListNode next){ + this.next =next; + this.val = val; + this.last = this; + this.size = 1; + } + + + public void add(int x){ + this.last.next = new ListNode(x); + this.last = this.last.next; + this.size ++; + } + + public static ListNode ArrayToListNode(int [] list){ + ListNode result = new ListNode(list[0]); + for(int i=1;i=size||index<0){return -1;} + int i=0; + ListNODE p1 = this.first.next; + while(isize){ + return; + } + if(index<=0){ + this.addAtHead(val); + return ; + } + int i=0; + ListNODE p1 = this.first; + while(i=size){return ;} + int i=0; + ListNODE p1 = this.first; + while(i map = new HashMap<>(); + for(int i:nums){ + map.put(i,map.getOrDefault(i,0)+1); + } + PriorityQueue> pq = new PriorityQueue<>(new Comparator>(){ + public int compare(Map.Entry entry1,Map.Entry entry2){ + return entry1.getValue()-entry2.getValue(); + } + }); + for(Map.Entry entry:map.entrySet()){ + if(pq.size() entry:pq){ + result[i] = entry.getKey(); + } + return result; + } +} + + diff --git a/lecture13_Priority queue_Heap.md b/lecture13_Priority queue_Heap.md new file mode 100644 index 0000000..9b32d4d --- /dev/null +++ b/lecture13_Priority queue_Heap.md @@ -0,0 +1,111 @@ +# Heap and Priority Queue + +### PQ(Priority Queue)的各种method,以及该数据结构有何优势 + +优先队列(Priority Queue)的方法包括; + +![Heap1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\Heap1.PNG) + +优先队列的特点是:你可以把它当成一袋东西,然后你可以向里面添加item,或者得知它共有多少个item,此外,供你交互的只有队列里的最小的item。 + +以如下的例子说明应该在何时使用优先队列: + +![heap2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap2.PNG) + +假设你需要收集24小时的消息,然后挑出m条最不和谐(unharmonious)的信息出来。一种通常的解法是: + +![heap3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap3.PNG) + +即收集24小时的全部消息,然后对其进行排序,这会使得占用巨大的内存(O(N)).而使用PQ(Priority queue)则可将内存消耗降低为O(M).其解法如下: + +![heap4](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap4.PNG) + +首先开始添加消息,并使用优先队列,当添加到m个消息时,每再添加一个,就将优先队列中的最小消息删去,这样就可一直保持内存消耗,并且最终留下的m条消息即为所求。 + +接下来我们将利用Heap(堆)去实现优先队列,以下是目前所有学过的数据结构对于实现优先队列的各种操作的时间消耗: + +![heap5](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap5.PNG) + +### Heap + +堆(heap)的定义强调两种属性: + +1. min—heap,即堆的最小属性。这指的是若用二叉树实现堆,则每个节点的值必须小于或者等于其所有子节点的值 +2. Complete,即完整性。这指的是若用二叉树实现堆,则若有item缺失的情况,只能发生在bottom level(即只能是每个子树的倒数第二层的节点对应的子节点没有满),并且所有节点尽可能地向左进行倾斜 + +一些heap的例子: + +![heap6](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap6.PNG) + +![heap7](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap7.PNG) + +利用这种heap(最小堆)实现PQ具有天然的优势: + +1. 对于getSmallest(),直接返回heap的根节点即可 +2. 对于add() ,以下展示了两种解决情况: + +![heap8](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap8.PNG) + +首先,上述堆是合法的,既满足min性质,也满足compelete性质,我们要添加3,则需要添加在首先保持满足compelete性质的位置(如下图所示): + +![heap9](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap9.PNG) + +![heap10](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap10.PNG) + +![heap11](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap11.PNG) + +![heap12](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap12.PNG) + +此时,再举一个添加"5"的例子: + +![heap13](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap13.PNG) + +![heap14](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap14.PNG) + +3. 对于removesmallest() ,以下同样展示了两种解决情况: + +![heap15](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap15.PNG) + +![heap16](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap16.PNG) + +![heap17](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap17.PNG) + +![heap18](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap18.PNG) + +![heap19](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap19.PNG) + +![heap20](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap20.PNG) + +![heap21](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap21.PNG) + +利用堆(heap)去实现优先队列的所有操作方法: + +![heap22](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\heap22.PNG) + +这里面着重注意一下`add`操作中的`last position`以及`removeSmallest`操作中的`the rightmost person`指的是哪个位置!!!!!!!!!! + +### Tree represantation + +在各种编程语言中,java普遍有这几种实现形式: + +![image-20220611124722337](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611124722337.png) + +![image-20220611125022106](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611125022106.png) + +![image-20220611125028365](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611125028365.png) + +不同于上述三种方法,以下这种方法通过维护两个Array(key数组和parents数组)来表示树结构: + +![image-20220611125204024](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611125204024.png) + +而事实上,我们可以不去维护parents数组,而只去维护keys数组,以表示整个树结构,如下图所示: + +![image-20220611125604174](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611125604174.png) + +那么在去掉了parents数组后,我们如何得知某一节点的父节点是谁呢(譬如我输入1或者2,需要返回0;输入3或者4,需要返回1.......),答案如下图; + +![image-20220611125944567](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611125944567.png) + +事实上,我们稍微将key数组的定义方式再小小的改变一下,就可以方便的检索任意item的左,右子节点以及父节点; + +![image-20220611130303280](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220611130303280.png) \ No newline at end of file diff --git a/lecture14_Traversal Tree_Traversal Graph_DFS.md b/lecture14_Traversal Tree_Traversal Graph_DFS.md new file mode 100644 index 0000000..2795309 --- /dev/null +++ b/lecture14_Traversal Tree_Traversal Graph_DFS.md @@ -0,0 +1,101 @@ +# DFS(深度优先搜索) + +### 树的定义 + +![image-20220613141350877](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613141350877.png) + +### 树的遍历顺序(Tree Traversal) + +* Preorder(前序遍历) + +![image-20220613141713277](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613141713277.png) + +* Inorder(中序遍历) + +![image-20220613141749324](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613141749324.png) + +* Postorder(后序遍历) + +![image-20220613141832498](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613141832498.png) + +* Levelorder + +![image-20220613141924495](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613141924495.png) + +### Graphs(图)的基本概念和定义 + +![image-20220613144634545](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613144634545.png) + +树的定义是:一系列的节点,并且每个节点必须有至少一条边进行连接,其限制条件是:两个节点之间只允许有一种路径通过。 + +上述中红色的图都符合图的定义: + +![image-20220613144741621](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613144741621.png) + +![image-20220613144812536](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613144812536.png) + +simple Graph(简单图)定义的限制有:1) no loops 2) no parallel edges + +以下显示了一些有关Graph的术语: + +![image-20220613145258899](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613145258899.png) + +![image-20220613145309630](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613145309630.png) + +### Graph Problems + +![image-20220613150100602](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613150100602.png) + +### DFS + +DFS可用来解决s-t Path问题以及连通性问题(Connectivity) + +DFS的核心主旨思想为: + +![image-20220613152535247](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613152535247.png) + +以上图右下角的图为例,以DepthFirstPaths问题为背景,阐释DFS的具体流程如下: + +![image-20220613152839541](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613152839541.png) + +在mark(1)之前首先 set degeTo(1)=0 + +![image-20220613152945458](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613152945458.png) + +以此类推 + +![image-20220613152959450](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613152959450.png) + +![image-20220613153007635](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153007635.png) + +![image-20220613153011356](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153011356.png) + +![image-20220613153025684](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153025684.png) + +![image-20220613153029373](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153029373.png) + +![image-20220613153040950](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153040950.png) + +![image-20220613153055394](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153055394.png) + +![image-20220613153100311](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153100311.png) + +![image-20220613153107654](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153107654.png) + +![image-20220613153112342](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153112342.png) + +![image-20220613153123130](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153123130.png) + +![image-20220613153130570](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153130570.png) + +![image-20220613153136509](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153136509.png) + +![image-20220613153152110](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153152110.png) + +![image-20220613153159103](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153159103.png) + +![image-20220613153205306](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153205306.png) + +### DFS的preorder以及postorder + +![image-20220613153308582](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613153308582.png) \ No newline at end of file diff --git "a/lecture15_BFS_Graph API_implementations - \345\211\257\346\234\254.md" "b/lecture15_BFS_Graph API_implementations - \345\211\257\346\234\254.md" new file mode 100644 index 0000000..ee41df5 --- /dev/null +++ "b/lecture15_BFS_Graph API_implementations - \345\211\257\346\234\254.md" @@ -0,0 +1,104 @@ +# BFS(广度优先搜索) + +不同于典型的DFS,典型的BFS并不基于递归,并且其中运用了数据结构Queue(队列,后进 addLast() 先出 removefirst())。BFS的主要原理如下所示: + +![image-20220613193354778](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613193354778.png) + +基于与DFS上一章同样的图,对BFS的整个算法流程进行阐述: + +![image-20220613195133275](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195133275.png) + +这里讨论的是s——t的无权重最短路问题,即找出每个vertex距离source的最短路径有多长。为解决这一问题,BFS(BreadthFirstPaths)本身需要维护3个Array,即marked,edgeTo以及distTo(相比之下s-t connect问题中,DFS只需要维护两个Array,即marked以及edgeTo) + +![image-20220613195858339](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195858339.png) + +![image-20220613195913588](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195913588.png) + +![image-20220613195918024](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195918024.png) + +![image-20220613195923212](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195923212.png) + +![image-20220613195927331](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195927331.png) + +![image-20220613195931639](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195931639.png) + +![image-20220613195935719](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195935719.png) + +![image-20220613195939886](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195939886.png) + +![image-20220613195944032](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195944032.png) + +![image-20220613195951948](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195951948.png) + +![image-20220613195956025](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195956025.png) + +![image-20220613200000093](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200000093.png) + +![image-20220613200004108](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200004108.png) + +![image-20220613200008948](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200008948.png) + +![image-20220613200012822](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200012822.png) + +![image-20220613200016905](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200016905.png) + +![image-20220613200020603](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200020603.png) + +![image-20220613200024397](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200024397.png) + +![image-20220613200028672](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200028672.png) + +## Graph API + +如果我们需要切实的implement我们的DFS和BFS算法,我们需要以下两个东西: + +![image-20220613201105548](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613201105548.png) + +* Graph API +* 某种用于代表Graph的数据结构 + +事实上,几乎所有的Graph API都会对所有的vertices进行编号,如下图所示: + +![image-20220613201603471](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613201603471.png) + +下图为普林斯顿提供的Graph API: + +![image-20220613203855831](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613203855831.png) + +计算每个vertex的degree的函数: + +![image-20220613203958711](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613203958711.png) + +打印出图的所有边的关系的函数: + +![image-20220613204250027](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613204250027.png) + +## The data structure to represent Graph + +* 邻接矩阵(Adjacency Matrix): + +![image-20220613204555321](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613204555321.png) + +利用这种邻接矩阵实现求解每个节点的邻居,需要Θ(V²)的时间复杂度: + +![image-20220613210038270](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210038270.png) + +* HashSet 即利用Hash集合表示所有边: + +![image-20220613210150027](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210150027.png) + +* Adjacency Lists(邻接列表,最为流行的一种表示方法) + +![image-20220613210257295](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210257295.png) + +采用邻接列表完成求解每个节点的邻居,需要Θ(V+E)的时间复杂度: + +![image-20220613210504976](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210504976.png) + +上述三种data structure对于Graph API中大部分操作的时间复杂度需要: + +![image-20220613211243997](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613211243997.png) + +最后是普林斯顿对于Graph API的implementation: + +![image-20220613211353250](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613211353250.png) \ No newline at end of file diff --git a/lecture16_Dijkstra_A star algorithm for shortest path problem.md b/lecture16_Dijkstra_A star algorithm for shortest path problem.md new file mode 100644 index 0000000..4ebb5d3 --- /dev/null +++ b/lecture16_Dijkstra_A star algorithm for shortest path problem.md @@ -0,0 +1,3 @@ +# Dijkstra算法用于求解Shortest Path problem + +**对于Shortest Path problem来说,无向图就相当于是两个节点中连接两条相反通路的有向图,可以等效进行求解!从这一点上来讲,Shortest Path problem对于图是cyclic还是acyclic(图中有无cycle)并没有要求!** \ No newline at end of file diff --git a/lecture17_Minimum Spanning tree.md b/lecture17_Minimum Spanning tree.md new file mode 100644 index 0000000..ee41df5 --- /dev/null +++ b/lecture17_Minimum Spanning tree.md @@ -0,0 +1,104 @@ +# BFS(广度优先搜索) + +不同于典型的DFS,典型的BFS并不基于递归,并且其中运用了数据结构Queue(队列,后进 addLast() 先出 removefirst())。BFS的主要原理如下所示: + +![image-20220613193354778](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613193354778.png) + +基于与DFS上一章同样的图,对BFS的整个算法流程进行阐述: + +![image-20220613195133275](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195133275.png) + +这里讨论的是s——t的无权重最短路问题,即找出每个vertex距离source的最短路径有多长。为解决这一问题,BFS(BreadthFirstPaths)本身需要维护3个Array,即marked,edgeTo以及distTo(相比之下s-t connect问题中,DFS只需要维护两个Array,即marked以及edgeTo) + +![image-20220613195858339](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195858339.png) + +![image-20220613195913588](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195913588.png) + +![image-20220613195918024](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195918024.png) + +![image-20220613195923212](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195923212.png) + +![image-20220613195927331](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195927331.png) + +![image-20220613195931639](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195931639.png) + +![image-20220613195935719](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195935719.png) + +![image-20220613195939886](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195939886.png) + +![image-20220613195944032](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195944032.png) + +![image-20220613195951948](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195951948.png) + +![image-20220613195956025](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613195956025.png) + +![image-20220613200000093](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200000093.png) + +![image-20220613200004108](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200004108.png) + +![image-20220613200008948](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200008948.png) + +![image-20220613200012822](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200012822.png) + +![image-20220613200016905](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200016905.png) + +![image-20220613200020603](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200020603.png) + +![image-20220613200024397](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200024397.png) + +![image-20220613200028672](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613200028672.png) + +## Graph API + +如果我们需要切实的implement我们的DFS和BFS算法,我们需要以下两个东西: + +![image-20220613201105548](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613201105548.png) + +* Graph API +* 某种用于代表Graph的数据结构 + +事实上,几乎所有的Graph API都会对所有的vertices进行编号,如下图所示: + +![image-20220613201603471](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613201603471.png) + +下图为普林斯顿提供的Graph API: + +![image-20220613203855831](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613203855831.png) + +计算每个vertex的degree的函数: + +![image-20220613203958711](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613203958711.png) + +打印出图的所有边的关系的函数: + +![image-20220613204250027](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613204250027.png) + +## The data structure to represent Graph + +* 邻接矩阵(Adjacency Matrix): + +![image-20220613204555321](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613204555321.png) + +利用这种邻接矩阵实现求解每个节点的邻居,需要Θ(V²)的时间复杂度: + +![image-20220613210038270](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210038270.png) + +* HashSet 即利用Hash集合表示所有边: + +![image-20220613210150027](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210150027.png) + +* Adjacency Lists(邻接列表,最为流行的一种表示方法) + +![image-20220613210257295](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210257295.png) + +采用邻接列表完成求解每个节点的邻居,需要Θ(V+E)的时间复杂度: + +![image-20220613210504976](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613210504976.png) + +上述三种data structure对于Graph API中大部分操作的时间复杂度需要: + +![image-20220613211243997](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613211243997.png) + +最后是普林斯顿对于Graph API的implementation: + +![image-20220613211353250](C:\Users\lizi2\AppData\Roaming\Typora\typora-user-images\image-20220613211353250.png) \ No newline at end of file diff --git a/lecture_5.md b/lecture18_Quad Tree_Kd Tree.md similarity index 100% rename from lecture_5.md rename to lecture18_Quad Tree_Kd Tree.md diff --git a/lecture19_Tries Tree.md b/lecture19_Tries Tree.md new file mode 100644 index 0000000..e69de29 diff --git "a/lecture_10_\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/lecture_10_\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" new file mode 100644 index 0000000..7406bc6 --- /dev/null +++ "b/lecture_10_\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -0,0 +1,124 @@ +# Binary Search Tree(BST) + +## BST的由来 + +![2022-05-23_201104](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_201104.PNG) + +对于上述这种ordered Linkedlist这一类ADT而言,`contains`以及`add`都需要O(N)的线性时间,因此我们考虑优化其结构,以加快其查找时间,因此引入BST。BST与Binary Search有着很强的关联关系,考虑设定一指针指向上述ADT中的中位数(即“D”),并将“D”以前的指针反向,于是有: + +![2022-05-23_201459](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_201459.PNG) + +这样我们再查找某个key时,遍历的时间就会缩减为O(N/2),此时继续进行优化,即对于“E——>F——>G”与“C——>B——>A”继续定义中位数指针,并且使一半的指针反向,再将其按不同的高度略微进行调整,于是就得到了所谓的二叉搜索树(BST) + +![2022-05-23_201640](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_201640.PNG) + +## BST定义 + +首先考虑树的定义: + +1. A set of nodes + +2. A set of edges connect those nodes + + **constrains: There is exactly one path between any two nodes.** + +![2022-05-23_201953](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_201953.PNG) + +上图中,红色两张张图中个别结点对不满足constrains,因此红色的图都不属于是树! + +BST则是Binary Tree中的一种(一个节点只能够有最多两个子节点),其满足:某个节点左边的子树所有节点的key都小于该节点的key,而某个节点右边的子树所有的节点的key都大于该节点的key。 + +## Search,Insert以及Delete操作 + +**lecture中仅仅提供了pseudo code,正式java代码在对应的lab中给出** + +```java +class BST { + public Key key; + public BST left; + public BST right; + + public BST(Key key){ + this.key = key; + } + + public BST(BST left,BST right,Key key){ + this.left = left; + this.right = right; + this.key = key; + } + + /* 如果树非常浓密,则该操作时间复杂度为Θ(log(N)) (log(N)为树高) + * 注意:这里应该为课上写的pseudo code,实现方式为naked recursion,正式的代码需要你在对应的lab中完成! */ + public static BST search(BST T,Key key){ + if(T==null){ + return null; + } + if(T.key.equals(key)){ + return T; + } else if(key>T.key){ + return search(T.right,key); + } else{ + return search(T.left,key); + } + } + + + /* 利用递归实现BST的insert操作! + * 注意:这里应该为课上写的pseudo code,实现方式为naked recursion,正式的代码需要你在对应的lab中完成! */ + public static BST insert(BST T, Key ik){ + if(T==null){ + return new BST(ik); + } + if(ikT.key){ + T.right = insert(T.right,ik); + } + return T; + } +} +``` + +对于BST的delete操作相对而言比较复杂,需要考虑三种情况,即要删除的节点分别没有节点,仅有1个节点,有两个节点,这三种情况的删除操作的复杂程度依次递增: + +- **需要删除的节点没有子节点:** + +![2022-05-23_203359](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_203359.PNG) + +**只要把flat.right赋值为null,glut node将会被garbage collector自动回收** + +- **需要删除的节点有一个子节点:** + +![2022-05-23_203828](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_203828.PNG) + +**我们需要让flat的parent node,即dog指向flat的child node,即elf,则flat会被garbage collector自动回收,如下图:** + +![2022-05-23_204057](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_204057.PNG) + +**需要删除的节点有两个子节点:** + +这种情况比较复杂,请看如下的例子: + +![2022-05-23_204207](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_204207.PNG) + +这种情况下,典型的思路是:选取k的左子树中最靠右的节点,且该节点至多只能有一个子节点;或选择k的右子树中最靠左的节点,且该节点同样至多只能有一个子节点,将选择的该节点替代掉k,成为新的根节点,同时删除掉选择节点原来在树结构中的位置,如下图: + +![2022-05-23_204207](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_204207.PNG) + +![2022-05-23_204657](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_204657.PNG) + +## 利用BST对Set和Map进行加速 + +BST的search搜索效率可达O(logN),因此可运用该数据结构实现Set或Map,从而实现search操作的加速: + +- 利用BST实现Set: + +![2022-05-23_205109](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_205109.PNG) + +- 利用BST实现Map(仅需要除了key属性,再多定义一个value属性,然后search的时候通过key查询,返回value即可) + +![2022-05-23_205212](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_205212.PNG) + +**这已经非常接近`TreeMap`和`TreeSet`的实现了!** \ No newline at end of file diff --git a/lecture_11_B Tree_Red Black Tree.md b/lecture_11_B Tree_Red Black Tree.md new file mode 100644 index 0000000..4abb9ba --- /dev/null +++ b/lecture_11_B Tree_Red Black Tree.md @@ -0,0 +1,116 @@ +# B Tree + +## Tree height,Tree depth以及Balance的定义 + +![2022-05-23_214756](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_214756.PNG) + +“spindly Tree”(细长的树)拥有更高的height(O(N))和Average Depth,而“bushy Tree”(浓密的树)拥有相对较短的height(O(log(N)))和Average Depth: + +![2022-05-23_215101](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_215101.PNG) + +![2022-05-23_214930](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_214930.PNG) + +**balance的定义:如果我们讲一个树是balance的,那么就是在讲这棵树的所有叶子节点距离其根节点的距离(历经的边的个数)全部相等!** + +## 为什么要引进B Tree + +在现实情况中,对BST进行randomly insert操作,得到的树一般是较为浓密的,然而许多场景下,我们需要连续为BST添加顺序数据,这就会造成下面这种情况: + +![2022-05-23_220807](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-23_220807.PNG) + +当原本浓密的BST变得细长时,search效率将会下降,为了避免这一缺点,我们将引入B树结构: + +## B Tree的定义以及基本操作 + +要想使树变得不再浓密,一个自然而然的想法就是像下面这样: + +![2022-06-03_201110](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-06-03_201110.PNG) + +上面这棵树的height是balance了,但当数据量达到一定规模时,`search`仍然是O(N),因此我们需要设置让每个节点所包含的元素数量不能超过一定的限制,以防止上述情况。考虑如下的解决方案: + +![B](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\B.PNG) + +上图中,我们规定了每个节点包含的元素不能够超过阈值`L=3`,这样,虽然搜索同一节点所消耗的时间复杂度会从O(1)变为O(L),但这是可以接受的。上述情况仅仅是当叶子节点包含的元素超限时,所做的更改。当非叶子节点所包含的元素超限时,修改方式如下: + +![B1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\B1.PNG) + +若根包含的元素数量超限,则如下图所示: + +![B2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\B2.PNG) + +这种数据结构可以完美地保证树是balanced,如上面3种情况所示,如果根节点超限,则调整树的结构会使树整个的height增加1,而除根结点外的非叶子节点或叶子节点超限,则不会对height有影响。该种数据结构的search操作可完美保证O(logN),这种数据结构称之为B Tree. B Tree的不变量(invariants)定义如下: + +![B3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\B3.PNG) + +事实上,B tree将保证其height永远log(N),`contains`以及`add`的操作的时间复杂度都为O(log(N)) + +**最后,记住一条铁律:B树永远是balanced!** + +## 有关树的Rotate操作 + +B树同样是存在缺点的,它的缺点一个是较难实现,并且经常需要忍受含有2个元素或者3个元素的节点之间进行互相转换,且在添加节点是需要进行节点的分裂操作,因此这里首先介绍一下树的Rotate操作.考虑以下的树结构,操作`rotateLeft(G)`是将G变成目前G右子节点的左子节点,操作过程如下: + +![rotate1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\rotate1.PNG) + +既然P是G的右子节点,那么G就是新P的左子节点,此时G缺一个右子节点,而P存在3个节点,那么最终把k作为新G的右子节点即可: + +![rotate2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\rotate2.PNG) + +**所以有关于`rotate(x)`的口诀是:如果需要`rotateright(x)`,考虑x和其左子树的关系;需要`rotateleft(x)`,则考虑x和其右子树的关系。**掌握了rotate操作,可以不使用B树那么复杂的结构,就使得细长的树变得bushy,如下图所示: + +![rotate3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\rotate3.PNG) + +`rotateright(x)`和`rotateleft(x)`的操作: + +![rotate4](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\rotate4.PNG) + +# 左倾红黑树(Left leaning red black tree, LLRB) + +先前,我们先后介绍了B树和对于BST的rotate操作,它们都可以使得树由细长而变得bushy。这里我们进一步定义一种利用二叉树结构模拟B树的数据结构,称为红黑树(red black tree),如下图所示,每当出现包含两个元素的节点时,我们采用左倾操作将其拆分(其实同样可以右倾,只是普林斯顿算法读本这样进行规定,因此也有了我们标题的名称:左倾红黑树,LLRB): + +![read_black_2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\read_black_2.PNG) + +红黑指的是啥呢?请看下图: + +![read_black_1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\read_black_1.PNG) + +“红”色链接将原B树含有多个元素节点的元素通过二叉树的结构进行“glue”,而“黑”色链接则是链接其余的普通节点,因此称之为红黑树。 + +### LLRB的一些性质 + +1. **判断一个LLRB是否合法,一个快速方法是将其逆映射,画出唯一与之对应的B tree,之后判断该B tree是否合法(需满足:1.是否balanced 2.是否每一个拥有k个元素的非叶子节点都拥有k+1个子节点)** + +![red_black_3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\red_black_3.PNG) + +2. 每个子节点到根节点所经过的黑边数必须相等(“黑边”平衡性),并且不允许有两条红边都经过某一节点(与B树中“每一个拥有k个元素的非叶子节点都拥有k+1个子节点”的概念相对应) +2. 某一红黑树对应的B树高度为H,则该红黑树的最大高度应等于2H+1。因此如果B树的树高之于输入数据是O(log(N))的话,红黑树的树高也是一样的。 + +### LLRB的构建过程 + +1. 当只有一个节点s,且需要添加一个节点E时,应该使用红色链接,如下图: + +![LLRB_build_1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_1.PNG) + +2. 当时下面这种情况时,就需要用到旋转操作:`rotate(E)` + +![LLRB_build_2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_2.PNG) + +3. 当增加累计增加两个节点到子节点时,我们首先假设可以有暂时违规的中间态(一个子节点被两个红边相连):`rotate(Z)` + +![LLRB_build_3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_3.PNG) + +4. 最后,我们将过渡态转换至正确的红黑树形态,此操作不涉及任何旋转操作,只需要单纯的将经过B点的红边变成黑边,黑边变成红边即可,可记录此操作方法为`flip(B)` + +![LLRB_build_4](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_4.PNG) + +5. 偶尔的,我们在构建红黑树时会发生如下情况:红黑树不符合左倾特性(s应该在B上面),因此我们需要再对B进行`rotateleft(B)`操作 + +![LLRB_build_5](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_5.PNG) + +![LLRB_build_6](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_build_6.PNG) + +**LLRB的运行时间分析** + +![LLRB_runtime](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\LLRB_runtime.PNG) + +**最后,java中的`TreeMap`就是使用了Red Black Tree(只是不是左倾的)** diff --git a/lecture_12_Hashing.md b/lecture_12_Hashing.md new file mode 100644 index 0000000..2fc7ab6 --- /dev/null +++ b/lecture_12_Hashing.md @@ -0,0 +1,70 @@ +# Hashing + +### 为什么要研究哈希 + +为了实现Set或者Map,我们目前已经学了这么多的数据结构: + +![Hash_motivate1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\Hash_motivate1.PNG) + +它们每一个都有些许缺点,对基于BST的树模型(BST,2-3 Tree,LLRB)来讲,其有如下显著缺点: + +1. 上述树模型只适合Comparable的数据类型,当数据类型无法进行比较时,其并不能进行有效检索与添加 +2. O(log(N))已经足够好了,但有没有可能做的更好呢? + +![Hash_motivate2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\Hash_motivate2.PNG) + +首先,我们将考虑正整数的数据类型。为了解决问题2,我们可以将数据作为Index并设计出如下的数据结构: + +![Hash_motivate3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\Hash_motivate3.PNG) + +这种数据类型无论`contains()`还是`add()`的复杂度都是O(1)的,但是该种数据结构及其耗费计算机内存。我们研究Hash就是基于上述数据结构,找一个折中的办法,使之在保持O(1)复杂度的同时,还能够解决数据`Comparable`的问题。 + +### Generalizing the DataIndexedIntegerSet idea + +不同于只储存数字,我们如何运用上述数据结构去存储“英文单词”呢? + +一种方法是:我们采用类似于数学中的10进制去对英文中的26个字母进行表示(只要基大于等于26即可避免任意两种英文单词发生碰撞(collisions),下图中采用了基=27) + +![avoid_collisions1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\avoid_collisions1.PNG) + +![avoid_collisions2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\avoid_collisions2.PNG) + +在成功表示完英文单词之后,我们可能有更大的字符集需要表示,诸如"2Pac",甚至是一些汉字,而这时就需要将我们的基数进行较大的扩充,然而在Java中,最大的Integer的值为2147,483,647.我们很容易就会使整数陷入用尽的地步,这个时候就会产生Overflow(溢出)的问题。 + +我们最初试图将近似无限的空间(英文单词,长度任意)去映射至有限的空间(java最大的Integer是有限的),以方便利用DataIndexedIntegerSet这种数据结构去实现O(1)的`add()`以及`contains`操作。然而只要存在溢出,就一定会产生溢出所带来的问题:collision。为了解决棘手的collision问题,提出了`HashTable`: + +### Hash Table + +事实上,java的Integer本身是有限的,因此我们试图将近似无限的空间一一映射至有限的空间就一定会发生collision。因此我们只能允许这样的collision产生,但是产生collision并不代表最终的`add()`和`contains()`操作结果就不能唯一了。HashTable就对上述情况做了如下的定义: + +![HashTable1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\HashTable1.PNG) + +“moo”和“Nep”都可计算出718的hashcode,则在718后定义一个Bucket,里面再装上“moo”和“Nep”即可。Bucket的实现可以是一个链表, etc. + +![hashTable2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\hashTable2.PNG) + +在这种数据结构情况下,两种操作的最坏时间复杂度都为O(Q),其中Q代表最大的Bucket链表长度。而实际上,在定义完上述数据结构后,我们其实还可以极大的缩减灰色链表的长度,方法是计算出hashcode后,我们通过某种函数(譬如取模函数,模=10,mod)即仅仅关注hashcode计算结果的最后一位,这样灰色链表的长度即可缩减至0~9: + +![hashTable](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\hashTable.PNG) + +### Improve the Hash Table performance + +![HashTable_performance1](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\HashTable_performance1.PNG) + +上图中,如果数据分布不均匀,则HashTable的`add`以及`contains`效率最差为O(N),如果理想情况下,数据均匀的分布至5个bucket内,则HashTable的效率为O(N/M)(M为bucket的数量,即灰色链表的长度)。现假设我们拥有足够好的hash函数,这个函数可将数据均匀分布至每个bucket内,那么我们如何保证O(N/M)=O(1)呢? + +答案是:我们同样令M随着N的规模变大而变大,只要保证M=O(N),则可保证O(N/M)=O(1)。 + +![HashTable_performance2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\HashTable_performance2.PNG) + +如上图,我们通常称N/M为负载因子(load factor). + +![HashTable_performance3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\HashTable_performance3.PNG) + +上图显示了当N=6时,M/N=1.5,因此将M进行了翻倍,此时之前所有的item都需要重新进行bucket的分配,而该操作是需要消耗大量时间的,即O(N).总的来说,上述HashTable需要的时间复杂度包括经常性的O(1),而偶尔需要resize,即O(N)的平均时间复杂度决定。 + +### java的Hash函数 + +显而易见,好的Hash函数能够令不同的item均匀分布地放入bucket内。 + +java中hash函数以31作为基底,至于为什么不以126为基底,请观看cs61C. diff --git a/lecutre_2.md "b/lecture_2_\346\265\267\350\261\241\344\271\213\350\260\234_IntList.md" similarity index 63% rename from lecutre_2.md rename to "lecture_2_\346\265\267\350\261\241\344\271\213\350\260\234_IntList.md" index d6248db..6730ed2 100644 --- a/lecutre_2.md +++ "b/lecture_2_\346\265\267\350\261\241\344\271\213\350\260\234_IntList.md" @@ -106,6 +106,8 @@ class Warlus{ } ``` +**有个问题一定要搞懂,即:你是选择构建静态方法还是动态方法?这取决于你这个类是否不同对象调用这个方法时期望得到的值是否一样?如果不一样,就只能用动态方法!** + # IntList(构建一种简易链表) ```java @@ -138,6 +140,39 @@ L = new IntList(10, L); L = new IntList(5, L); ``` +## **朴素链表的遍历方法(核心是多定义一个指针记录最初链表的位置,最后返回它即可!)** + +```java +public class lecture_02_practice { + public static void main(String[] args) { + int [] a = new int[]{-3,5,-99}; + ListNode p = new ListNode(a[0]); + ListNode m=p; + for(int i=1;i implements List61B{ } ``` -AList与List61B是is-a的关系,接口List61B本质是一种声明或者规定,即只要是List61B就应该具备所定义的诸多方法。接口中除具有default签名的方法之外,只对其余这些方法的签名进行声明,至于这些方法如何实现,将由继承接口的子类自行发挥(default方法可以不需要被重写)而AList则是对List61B的一种承诺,即AList承诺具有List61B所定义的所有方法,并对每个方法进行重写(override)。 +AList与List61B是is-a的关系,接口List61B本质是一种声明或者规定,即只要是List61B就应该具备所定义的诸多方法。接口中除具有default签名的方法之外,只对其余这些方法的签名进行声明,至于这些方法如何实现,将由继承接口的子类自行发挥(default方法可以不需要被重写)而AList则是对List61B的一种承诺,即AList承诺具有List61B所定义的所有方法,并对每个方法进行**重写(override,也可翻译为覆盖)。** ### static type & dynamic type & dynamic method selection(静态类型 & 动态类型 &动态方法选择)以及向上转型 -在如下语句中,someList是对对象SLList的一个引用变量,该引用变量的类型为List61B,称之为静态类型,而只有java程序在编译时才能够知道新建的对象为SLList类型,称之为动态类型。利用静态父类类型的引用变量去引用子类类型的对象,这样的操作称之为java的向上转型。 +在如下语句中,someList是对对象SLList的一个引用变量,该引用变量的类型为List61B,称之为静态类型,而只有java程序在编译时才能够知道新建的对象为SLList类型,称之为动态类型。(如何理解上面这句话呢,就是说如果利用List61B引用一个AList类型的变量,则静态类型仍旧为List61B,而此时动态类型则变成了AList,因此从最初命名的角度看,静态类型是绝对的,而动态类型则是相对的。)利用静态父类类型的引用变量去引用子类类型的对象,这样的操作称之为java的向上转型。 ```java public static void main(String[] args) { @@ -96,7 +96,42 @@ public static void main(String[] args) { someList.print(); ``` -向上转型的执行结果是执行SLList中经过override的print()方法,而不是List61B中default的print()方法。也就是说,当 Java 运行一个overrided的方法时,它会在它的动态类型中搜索适当的方法签名并运行它。**注意:这一条规则不适用于overloaded的方法!** +向上转型的执行结果是执行SLList中经过override的print()方法,而不是List61B中default的print()方法。也就是说,当 Java 运行一个overrided的方法时,它会在它的动态类型中搜索适当的方法签名并运行它。**注意:这一条规则不适用于overloaded(重载)的方法!** + +考虑如下在同一个class中定义的两个overloaded(重载)方法: + +```java +public static void peek(List61B list) { + System.out.println(list.getLast()); +} +public static void peek(SLList list) { + System.out.println(list.getFirst()); +} +``` + +运行如下代码: + +```java +SLList SP = new SLList(); +List61B LP = SP; +SP.addLast("elk"); +SP.addLast("are"); +SP.addLast("cool"); +peek(SP); +peek(LP); +``` + +第一个`peek`调用的是`public static void peek(SLList list)`,而第二个`peek`调用的则是`public static void peek(List61B list) `,原因是java在判断overloaded方法时,只根据参数类型去判断,因此这里需要看它的静态类型,而不是动态类型! + +### 继承(关键字`extends`) + +与关键字`implenment`不一样,关键字`extends`继承了父类的所有members,这些members包括: + +1. 所有实例(instance)和静态(static)变量 +2. 所有方法(methods) +3. 所有嵌套类(nested class) + +**注意:构造函数(constructor function)不是继承的!通常情况下java在子类中默认使用`super()`来进行构造,如果你不这么写,java会隐式的把它写出来。当然,如果你希望在子类的构造函数中添加一些别的东西,则不管怎样,构造函数的第一行都必须先写super(** 例子2: diff --git "a/lecture_5_java\346\225\260\346\215\256\347\273\223\346\236\204\345\270\270\347\224\250API.md" "b/lecture_5_java\346\225\260\346\215\256\347\273\223\346\236\204\345\270\270\347\224\250API.md" new file mode 100644 index 0000000..3fbd8a8 --- /dev/null +++ "b/lecture_5_java\346\225\260\346\215\256\347\273\223\346\236\204\345\270\270\347\224\250API.md" @@ -0,0 +1,312 @@ +# Collection(廖雪峰) + +![image-20220511205820023](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\image-20220511205820023.png) + +## java大数生成 + +```java +int BigNumber = Integer.MAX_VALUE; +``` + +## java中的Arrays(数组api) + +```java +//有关两个数组进行比较(即所有元素与大小是否一致) +int [] a = new int [5]; +int [] b = new int [5]; +System.out.println(Arrays.equals(a,b)); +``` + +## `List` + +`List interface`的主要方法包括: + +- 在末尾添加一个元素:`boolean add(E e)` +- 在指定索引添加一个元素:`boolean add(int index, E e)` +- 删除指定索引的元素:`E remove(int index)` +- 删除某个元素:`boolean remove(Object e)` +- 获取指定索引的元素:`E get(int index)` +- 改变指定索引的元素:`E set(int index, E e)` 若成功改变,则返回原来那个被改变的值 +- 获取`List`大小(包含元素的个数):`int size()` +- 快速创建一个不可改变(immutable)的List:`E of(E e1,E e2,......)`,注意:该方法不支持传入`null` +- 判断`List`是否包含某个指定元素:`boolean contains(Object o)` +- 返回某个元素的索引:`int indexOf(Object o)` 如果元素不存在,就返回`-1` + +```java +//将List转换为同类型的Array,即 E [] ,通常向如下这么写: +List nums = List.of(12, 34, 56); +Integer [] array = nums.toArray(new Integer [0]); +//注意:toArray()方法中只需要传入一个类型相同的数组参数即可,大小直接设置为0就可以,toArray()方法将会默认把大小调整为和nums.size()相同的大小! + +Integer [] arr1 = new Integer [10]; +int[] arr2 = Arrays.stream(arr1).mapToInt(Integer::valueOf).toArray(); +// Integer [] 转 int [] 非常重要!!!!!!!!! +``` + +遍历`List interface`当然可以采用for循环+get()方法进行,但该方法不是最有效率的,至少对于LinkedList来说是这样的,因此最有效率的方法是采用Java封装好的Iterator去进行遍历: + +```java +for(Iterator it = a.iterator();it.hasNext();){ + String s = it.next(); + System.out.println(s); + } +``` + +或者写的可以更简单一些: + +```java +for (String s : list) { + System.out.println(s); + } +``` + +## `Map` + +`Map interface`的主要方法包括: + +- 将` key`和` value`建立映射关系:`V put(K key, V value)` **注意:该方法的返回值是之前旧的value值,如果之前该`key`和`value`没有建立映射关系,则返回`null`** +- 通过` key`查找对应的` value`:`V get(Object key)` +- 删除`Map`中的映射关系` key value` `V remove(Object key)` **该方法输入键,返回被删除的对应的值,如果本来就不存在这个映射,则返回`null`** +- 查询`Map`中是否存在健` key`:`boolean containsKey(Object key)` +- 查询`Map`中是否存在值` value`:`boolean containsValue(Object value)` +- 查询`Map`中一共包含多少对映射关系` key value`:`int size()` +- 返回`Map`中所有`key`组成的`Set`:`Set keySet()` +- 返回`Map`中所有`value`的值:`Collection values()` **注意:如果`Map`中本身包含多个`key`都指向同一个`value`,则返回的结果仍然是将这个`value`重复多遍,结果并不是以`Set`呈现的!** +- 返回`Map`中所有` key value`的映射:`Set> entrySet()` + +### Entry + +`Interface Entry`表示`Map`中的一对映射关系: + +- 得到该映射关系的`key`值: `K getKey()` +- 得到该映射关系的`value`值: `V getValue()` +- 改变该映射关系的`value`值:`V setValue(V value)` +- 得到该映射关系的`hashcode`:`int hashcode()` + +```java +//判断HashMap中是否存在key1,若不存在,增添映射,若存在,增添映射 的简单写法(很重要!): +HashMap map = new HashMap<>(); +int key1 = 1; +map.put(key1,map.getOrDefault(key,0)+1); +``` + +遍历`Map`的两种方法: + +通过`keyset()`遍历`Map`对应的不重复的键集合: + +```java +for (String key : map.keySet()) { + Integer value = map.get(key); + System.out.println(key + " = " + value); + } +``` + +通过`entryset()`遍历`Map`对应的不重复的映射集合: + +```java +for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + Integer value = entry.getValue(); + System.out.println(key + " = " + value); + } +``` + +## Set + +```java +boolean add(E e) //将元素添加进Set: +boolean remove(Object e) //将元素从Set删除: +boolean contains(Object e) //判断是否包含元素: +int size() //查看set的大小 +for(E key:set){ + .... +} //遍历set的元素 +``` + +### JAVA中的字符串 + +### char的引用类型 + +**在构建各种数据结构,譬如`HahsMap`等,可以直接构造`Character`类!** + +### char转String + +### String class + +java字符串类"String"为引用类型,一旦被建立就无法更改。 + +以下为"String"一些常用的api: + +```java + String s = "abcdef"; + System.out.println("length"+s.length()); //获取长度 + System.out.println("index"+s.charAt(1)); //获取索引为1位置的字符 + System.out.println("length"+s.indexOf('c')); //获取字符'c'位置的索引 + System.out.println("length"+s.indexOf("bc")); //获取字符串“bc”位置的索引 + System.out.println("length"+s.substring(1,4)); //获取索引位置为(1,4)的子串 + + //转换————字符,大小写,替换,连接 + char [] arr = s.toCharArray(); //字符串转字符数组 + System.out.println(s.toUpperCase()); //字符串转大写 + System.out.println(s.replace('a','A')); //原字符串'a'替代为'A' + System.out.println(s.replace("ab","AB")); //原字符串子串“ab”替代为"AB" + System.out.println(" 123 ".trim()); //将字符串前后两端空格删除 + System.out.println(s.concat("ABCD")); //原字符串后面连接“ABCD” + System.out.println(String.valueOf(4)); //将数字转换为字符 + + //比较—————相等,包含,开头和结尾、大小 + System.out.println(s.equals("asd")); //字符串判断相等 + System.out.println(s.equalsIgnoreCase("ABCD")); //忽略大小写判断大小 + System.out.println(s.contains("abc")); //是否包含 + System.out.println(s.startsWith("ab")); //是否以某个字符串开头 + System.out.println(s.endsWith("ab")); //是否以某个字符串结尾 + System.out.println(s.compareTo("aqw")); //比较两个字符串大小 +``` + +### StringBuffer class & StringBuilder class + +StringBuffer和StringBuilder是可以修改的字符串,其中StringBuffer是线程安全的,而StringBuilder不是线程安全的;然而StringBuilder相较于StringBuffer有速度优势,因此多数情况下仍然使用StringBuilder。以下例举其常用api: + +```java +public class RunoobTest{ + public static void main(String args[]){ + StringBuilder sb = new StringBuilder(10); + sb.append("Runoob.."); + System.out.println(sb); + sb.append("!"); + System.out.println(sb); + sb.insert(8, "Java"); + System.out.println(sb); + sb.delete(5,8); + System.out.println(sb); + } +} +``` + +![2021-03-01-java-stringbuffer](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2021-03-01-java-stringbuffer.svg) + +在线程安全的要求下,还是应该使用StringBuffer: + +```java +public class Test{ + public static void main(String args[]){ + StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:"); + sBuffer.append("www"); + sBuffer.append(".runoob"); + sBuffer.append(".com"); + System.out.println(sBuffer); + } +} +``` + +运行结果如下: + +``` +菜鸟教程官网:www.runoob.com +``` + +StringBuffer常用api: + +```java +public StringBuffer append(String s) //将指定的字符串追加到此字符序列。 +public StringBuffer reverse() //将此字符序列用其反转形式取代。 +public delete(int start, int end) //移除此序列的子字符串中的字符。 +public deleteCharAt(int index) //移除指定位置的char +public insert(int offset, int i) //将 int 参数的字符串表示形式插入此序列中。 +insert(int offset, String str) //将 str 参数的字符串插入此序列中。 +replace(int start, int end, String str) //使用给定 String 中的字符替换此序列的子字符串中的字符。 +char charAt(int index) //返回此序列中指定索引处的 char 值。 +int indexOf(String str) //返回第一次出现的指定子字符串在该字符串中的索引。 +int indexOf(String str, int fromIndex) //从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 +int length() //返回长度(字符数)。 +void setCharAt(int index, char ch) //将给定索引处的字符设置为 ch。 +String substring(int start, int end) //返回一个新的 String,它包含此序列当前所包含的字符子序列。 +String toString() //将StringBuffer转换为字符串形式 +``` + +## Queue + +队列实际上实现了一个先进先出的有序表。其常用api如下: + +```java +Queue q = new Queue<>(); +int size() //获取队列长度; +boolean add(E)/boolean offer(E) //添加元素到队尾; +E remove()/E poll() //获取队首元素并从队列中删除; +E element()/E peek() //获取队首元素但并不从队列中删除; +``` + +以上api除了获取队列长度,其余每种方法都有两个方法名,这两个方法名在执行方法失败时分别对应两种不同的行为,如下表所示: + +| | **throw Exception** | **返回false或null** | +| :----------------: | :-----------------: | :------------------: | +| 添加元素到队尾 | `add(E e)` | `boolean offer(E e)` | +| 取队首元素并删除 | `E remove()` | `E poll()` | +| 取队首元素但不删除 | `E element()` | `E peek()` | + +注意:不要把`null`添加到队列中,否则`poll()`方法返回`null`时,很难确定是取到了`null`元素还是队列为空。 + +## PriorityQueue + +使用PriorityQueue,调用`remove()`/`poll()`或者`element()`/`peek()`方法,其返回的都是优先级最高的元素。也就是说,Priority能够根据内部的`compatator`机制,将目前在队列中的所有元素进行优先级的排序,按照优先级顺序将优先级最高的放在队首,优先级最低的放在队尾。对于默认的类型(即已经实现了`comparable`的类型)可以自动实现优先队列的功能,然而对于没有实现`comparable`的类型,我们在构建PQ时需要手动传入一个`comparator`类。 + +```java +// PriorityQueue 的constructor方法 +PriorityQueue pq = new PriorityQueue(); //创建一个PQ +PriorityQueue pq = new PriorityQueue(Collection c); //创建一个集合为c的PQ +PriorityQueue pq = new PriorityQueue(int initialCapacity); //创建一个具有初始容量的PQ +//创建一个有comparator的PQ(最重要的constructor方法!) + +PriorityQueue pq = new PriorityQueue(new Comparator(){ + public int compare(E e1,E e2){ + return e1-e2; //这是默认以最小堆实现的优先队列! + } +}); + +PriorityQueue pq = new PriorityQueue(new Comparator(){ + public int compare(E e1,E e2){ + return e2-e1; //这是以最大堆实现的优先队列!只需要修改compare()函数的return值即可! + } +}); + + +//PQ的常用API +boolean add(E element); //向PQ添加一个元素 +E peek(); //查看PQ顶端元素 +E poll(); //删除PQ最顶端元素 + +/*切记,想打印PQ元素不要使用System.out.println(),将不会获得按默认顺序排序的元素结果,而是打印插入PQ时的顺序的元素*/ + +``` + +## Deque + +相较于queue,Deque既能够添加元素到队尾,也能够添加元素到队首,既可以从队首获取,也可以从队尾获取。 + +以下表格比较了Queue和Deque的常用api及其不同: + +| | Queue | Deque | +| :----------------: | :------------------------: | :---------------------------------: | +| 添加元素到队尾 | `add(E e) `/ `offer(E e)` | `addLast(E e) `/ `offerLast(E e)` | +| 取队首元素并删除 | `E remove() `/ `E poll()` | `E removeFirst() `/ `E pollFirst()` | +| 取队首元素但不删除 | `E element()` /` E peek()` | `E getFirst() `/ `E peekFirst()` | +| 添加元素到队首 | 无 | `addFirst(E e) `/ `offerFirst(E e)` | +| 取队尾元素并删除 | 无 | `E removeLast() `/ `E pollLast()` | +| 取队尾元素但不删除 | 无 | `E getLast() `/ `E peekLast()` | + +## Stack + +栈是一种后进先出的数据结构,常用API如下: + +```java +//java中构建栈一般要这么写: +Deque stack = new LinkedList<>(); + +//常用API: +E push(E) //把元素压栈; +E pop() //把栈顶的元素“弹出”; +E peek() //取栈顶元素但不弹出; +int size() //获取栈的大小 +boolean isEmpty() //栈是否为空 是返回true,否返回false +``` + diff --git a/lecture_6_ArraySet.md b/lecture_6_ArraySet.md new file mode 100644 index 0000000..a9b8fbf --- /dev/null +++ b/lecture_6_ArraySet.md @@ -0,0 +1,144 @@ +# 创建自己的`Set61B_ArraySet` + +这里先说句题外话,提一下Java中的Iterator接口: + +```java +public interface Iterator { + boolean hasNext(); /* 如果还有剩余没处理过的元素,则返回true,如果所有元素都已经处理过,则返回false*/ + E next(); /* 获得下一个元素,并将Iterator向前再推进一项 */ + default void remove() { + throw new UnsupportedOperationException("remove"); + } +} +``` + +再说一下java中的Iterablej接口: + +```java +public interface Iterable { /*实现此接口可使你自己定义的class被for each增强循环的写法所支持*/ + Iterator iterator(); +} +``` + +接口文件:`Set61B`: + +```java +public interface Set61B { + boolean add(E value); + boolean contains(E value); + int size(); +} +``` + +实现文件:`Set61B_ArraySet` + +```java +/* 想要将新定义的类赋予for(:)的增强遍历写法,首先需要implements Iterable接口*/ +public class Set61B_ArraySet implements Set61B,Iterable{ + private E [] items; + private int size; + +/* Iterable接口中定义的规范:其返回值需要是Iterator接口的一个实现*/ + public Iterator iterator() { + return new ArraySetIterator(); + } + +/* 实现Iterator接口*/ + private class ArraySetIterator implements Iterator{ + private int position; + + public ArraySetIterator(){ + position = 0; + } + + @Override + public boolean hasNext() { + return position < size; + } + + @Override + public E next() { + E result = items[position]; + position +=1; + return result; + } + } + + public Set61B_ArraySet(){ + items = (E[]) new Object[100]; + this.size = 0; + } + + @Override + public boolean add(E value) { + /* 当添加元素为null时,null本身无法调用方法,因此会出现错误,这里需要增加抛出异常的一段代码 */ + if (value == null) { + throw new IllegalArgumentException("can't add null"); + } + if(contains(value)){ + return false; + } + items[size]=value; + this.size +=1; + return true; + } + + @Override + public boolean contains(E value) { + for (int i = 0; i < size; i += 1) { + if(items[i].equals(value)){ + return true; + } + } + return false; + } + + @Override + public int size() { + return this.size; + } + + public String toString(){ + StringBuilder result = new StringBuilder("{"); + int i=0; + for(;i o = (Set61B_ArraySet) other; + if (o.size() != this.size()) { + return false; + } + for (E item : this) { + if (!o.contains(item)) { + return false; + } + } + return true; + } +``` + diff --git "a/lecture_8_\346\270\220\350\277\233\345\244\215\346\235\202\345\272\246_\344\272\214\345\210\206\346\237\245\346\211\276_\345\275\222\345\271\266\346\216\222\345\272\217.md" "b/lecture_8_\346\270\220\350\277\233\345\244\215\346\235\202\345\272\246_\344\272\214\345\210\206\346\237\245\346\211\276_\345\275\222\345\271\266\346\216\222\345\272\217.md" new file mode 100644 index 0000000..e40537c --- /dev/null +++ "b/lecture_8_\346\270\220\350\277\233\345\244\215\346\235\202\345\272\246_\344\272\214\345\210\206\346\237\245\346\211\276_\345\275\222\345\271\266\346\216\222\345\272\217.md" @@ -0,0 +1,104 @@ +# 渐进分析导论以及大O复杂度 + +### 常用`for loop`的复杂度分析: + +`for loop 1`: + +```java +int N = A.length; +for (int i = 0; i < N; i += 1) + for (int j = i + 1; j < N; j += 1) + if (A[i] == A[j]) + return true; +return false; +``` + + +$$ +1+2+...+N=\frac{N\left( N-1 \right)}{2}=O\left( N^2 \right) +$$ +`for loop 2`: + +```java +public static void printParty(int N) { + for (int i = 1; i <= N; i = i * 2) { + for (int j = 0; j < i; j += 1) { + System.out.println("hello"); + int ZUG = 1 + 1; + } + } +} +``` + +$$ +a_1+a_1q+a_1q^2+...+a_1q^{N-1}=\frac{a_1\left( q^N-1 \right)}{q-1} +$$ + +$$ +\Rightarrow 1+2+4+...+N=2N-1=O\left( N \right) +$$ + +### `recursion`的复杂度分析 + +```java +public static int f3(int n) { + if (n <= 1) + return 1; + return f3(n-1) + f3(n-1); +} +``` + +分析递归程序的复杂度需要看其函数的总调用次数,如下图所示: + +![asymptotics2_tree2](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\asymptotics2_tree2.png) + +如果输入数字为N,则f3的调用次数为: +$$ +1+2+4+...+2^{N-1}=2^N-1=O\left( 2^N \right) +$$ + +### Binary Search(二分查找)时间复杂度分析 + +```java +public class Binary_Search { + public static void main(String[] args) { + int [] array = new int []{6,13,14,25,33,43,51,53,64,72,84,93,95,96,97}; + System.out.println(binary_search(array,13)); + } + + public static int binary_search(int [] array,int target){ + Arrays.sort(array); + int first =0; + int last =array.length-1; + int mid = array.length/2; + while(first<=last){ + if(array[mid]>target){ + last = mid-1; + } else if(array[mid] Θ(N) */ + public void connect(int p, int q){ + int pid = id[p]; + int qid = id[q]; + for (int i = 0; i < id.length; i++){ + if (id[i] == pid){ + id[i] = qid; + } + } + } + + /* Θ(1) */ + public boolean isConnected(int p, int q){ + return (id[p] == id[q]); + } +} +``` + +### Quick Union + +lecture在Quick Union部分更改了对Disjointed Sets的定义方式,即把每一个Sets想象成一个树的形式(只是还是采用array来表达),根节点索引对应的元素值为-1,获取索引对应的元素值,再以元素值作为索引继续获取元素值,最终可遍历至每个Sets对应的根结点处。这个向上遍历至根节点的功能被lecture定义为辅助函数`find()`。 + +![2022-05-21_222221](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\2022-05-21_222221.PNG) + +```java +public class QuickUnionDS implements DisjointSets { + private int[] parent; + + public QuickUnionDS(int num) { + parent = new int[num]; + for (int i = 0; i < num; i++) { + parent[i] = i; + } + } + + private int find(int p) { + while (parent[p] >= 0) { + p = parent[p]; + } + return p; + } + + /* 最好情况 Θ(1),最坏情况Θ(N) */ + @Override + public void connect(int p, int q) { + int i = find(p); + int j= find(q); + parent[i] = j; + } + + /* 最好情况 Θ(1),最坏情况Θ(N) */ + @Override + public boolean isConnected(int p, int q) { + return find(p) == find(q); + } +} +``` + +对于`QuickUnion(int p,int q)`而言,最好的结果是`p`和`q`分别为两个树的根节点,这样将其进行connect仅仅需要O(1)的时间复杂度,而最坏的情况为O(N),如下图所示: + +![9.3.3](C:\Users\lizi2\Desktop\python_reptile_practice\UC_Berkeley_CS61B\picture\9.3.3.png) + +### Weighted Quick Union (WQU) + diff --git a/out/production/Discussion/Disc_01.class b/out/production/Discussion/Disc_01.class new file mode 100644 index 0000000..e19f541 Binary files /dev/null and b/out/production/Discussion/Disc_01.class differ diff --git a/out/production/Discussion/Disc_02.class b/out/production/Discussion/Disc_02.class new file mode 100644 index 0000000..3e6743f Binary files /dev/null and b/out/production/Discussion/Disc_02.class differ diff --git a/out/production/Discussion/fib.class b/out/production/Discussion/fib.class new file mode 100644 index 0000000..d36a7d4 Binary files /dev/null and b/out/production/Discussion/fib.class differ diff --git a/Homework/out/production/Hw_0/Hw_0.class b/out/production/HomeWork_/Hw_0.class similarity index 54% rename from Homework/out/production/Hw_0/Hw_0.class rename to out/production/HomeWork_/Hw_0.class index f9fc6c3..1c4eebc 100644 Binary files a/Homework/out/production/Hw_0/Hw_0.class and b/out/production/HomeWork_/Hw_0.class differ diff --git a/out/production/Lab/Foo.class b/out/production/Lab/Foo.class new file mode 100644 index 0000000..1642b0e Binary files /dev/null and b/out/production/Lab/Foo.class differ diff --git a/out/production/Lab/IntList.class b/out/production/Lab/IntList.class new file mode 100644 index 0000000..d350b84 Binary files /dev/null and b/out/production/Lab/IntList.class differ diff --git a/out/production/Lab/Lab2.class b/out/production/Lab/Lab2.class new file mode 100644 index 0000000..29ca289 Binary files /dev/null and b/out/production/Lab/Lab2.class differ diff --git a/out/production/Leetcode_practice/KMP_practive.class b/out/production/Leetcode_practice/KMP_practive.class new file mode 100644 index 0000000..4c376e3 Binary files /dev/null and b/out/production/Leetcode_practice/KMP_practive.class differ diff --git a/out/production/Leetcode_practice/ListNode.class b/out/production/Leetcode_practice/ListNode.class new file mode 100644 index 0000000..98605fa Binary files /dev/null and b/out/production/Leetcode_practice/ListNode.class differ diff --git a/out/production/Leetcode_practice/MyLinkedList$ListNODE.class b/out/production/Leetcode_practice/MyLinkedList$ListNODE.class new file mode 100644 index 0000000..637970b Binary files /dev/null and b/out/production/Leetcode_practice/MyLinkedList$ListNODE.class differ diff --git a/out/production/Leetcode_practice/MyLinkedList.class b/out/production/Leetcode_practice/MyLinkedList.class new file mode 100644 index 0000000..3781677 Binary files /dev/null and b/out/production/Leetcode_practice/MyLinkedList.class differ diff --git a/out/production/Leetcode_practice/leetcode_practice$1.class b/out/production/Leetcode_practice/leetcode_practice$1.class new file mode 100644 index 0000000..1182433 Binary files /dev/null and b/out/production/Leetcode_practice/leetcode_practice$1.class differ diff --git a/out/production/Leetcode_practice/leetcode_practice.class b/out/production/Leetcode_practice/leetcode_practice.class new file mode 100644 index 0000000..9ee4e5d Binary files /dev/null and b/out/production/Leetcode_practice/leetcode_practice.class differ diff --git a/out/production/lecture/Benz.class b/out/production/lecture/Benz.class deleted file mode 100644 index daf6c77..0000000 Binary files a/out/production/lecture/Benz.class and /dev/null differ diff --git a/out/production/lecture/Car.class b/out/production/lecture/Car.class deleted file mode 100644 index 7c89167..0000000 Binary files a/out/production/lecture/Car.class and /dev/null differ diff --git a/out/production/lecture/SLList.class b/out/production/lecture/SLList.class index e6650fd..f8bf295 100644 Binary files a/out/production/lecture/SLList.class and b/out/production/lecture/SLList.class differ diff --git a/out/production/lecture_guide/IntNode.class b/out/production/lecture_guide/IntNode.class new file mode 100644 index 0000000..b496b45 Binary files /dev/null and b/out/production/lecture_guide/IntNode.class differ diff --git a/out/production/lecture_guide/Lists1Exercises.class b/out/production/lecture_guide/Lists1Exercises.class new file mode 100644 index 0000000..2719256 Binary files /dev/null and b/out/production/lecture_guide/Lists1Exercises.class differ diff --git a/out/production/lecture_guide/SLList$IntNode.class b/out/production/lecture_guide/SLList$IntNode.class new file mode 100644 index 0000000..123c6f2 Binary files /dev/null and b/out/production/lecture_guide/SLList$IntNode.class differ diff --git a/out/production/lecture_guide/SLList.class b/out/production/lecture_guide/SLList.class new file mode 100644 index 0000000..1e8d489 Binary files /dev/null and b/out/production/lecture_guide/SLList.class differ diff --git a/out/production/practice_self/AList.class b/out/production/practice_self/AList.class new file mode 100644 index 0000000..c45fc01 Binary files /dev/null and b/out/production/practice_self/AList.class differ diff --git a/out/production/practice_self/BST.class b/out/production/practice_self/BST.class new file mode 100644 index 0000000..63868c2 Binary files /dev/null and b/out/production/practice_self/BST.class differ diff --git a/out/production/practice_self/Binary_Search.class b/out/production/practice_self/Binary_Search.class new file mode 100644 index 0000000..4c7e842 Binary files /dev/null and b/out/production/practice_self/Binary_Search.class differ diff --git a/out/production/practice_self/DisjointSets61B.class b/out/production/practice_self/DisjointSets61B.class new file mode 100644 index 0000000..50eb8c6 Binary files /dev/null and b/out/production/practice_self/DisjointSets61B.class differ diff --git a/out/production/practice_self/DisjointSets61b_QuickFind.class b/out/production/practice_self/DisjointSets61b_QuickFind.class new file mode 100644 index 0000000..c9c269f Binary files /dev/null and b/out/production/practice_self/DisjointSets61b_QuickFind.class differ diff --git a/out/production/practice_self/DisjointSets61b_QuickUnion.class b/out/production/practice_self/DisjointSets61b_QuickUnion.class new file mode 100644 index 0000000..dd37745 Binary files /dev/null and b/out/production/practice_self/DisjointSets61b_QuickUnion.class differ diff --git a/out/production/practice_self/List61B.class b/out/production/practice_self/List61B.class new file mode 100644 index 0000000..61a2077 Binary files /dev/null and b/out/production/practice_self/List61B.class differ diff --git a/out/production/practice_self/List61B_AList.class b/out/production/practice_self/List61B_AList.class new file mode 100644 index 0000000..01d9557 Binary files /dev/null and b/out/production/practice_self/List61B_AList.class differ diff --git a/out/production/practice_self/List61B_SLList.class b/out/production/practice_self/List61B_SLList.class new file mode 100644 index 0000000..c3918b4 Binary files /dev/null and b/out/production/practice_self/List61B_SLList.class differ diff --git a/out/production/practice_self/Map61B.class b/out/production/practice_self/Map61B.class new file mode 100644 index 0000000..260d444 Binary files /dev/null and b/out/production/practice_self/Map61B.class differ diff --git a/out/production/practice_self/Map61B_ArrayMap.class b/out/production/practice_self/Map61B_ArrayMap.class new file mode 100644 index 0000000..48b9eea Binary files /dev/null and b/out/production/practice_self/Map61B_ArrayMap.class differ diff --git a/out/production/practice_self/SLList$IntNode.class b/out/production/practice_self/SLList$IntNode.class new file mode 100644 index 0000000..6150a47 Binary files /dev/null and b/out/production/practice_self/SLList$IntNode.class differ diff --git a/out/production/practice_self/SLList.class b/out/production/practice_self/SLList.class new file mode 100644 index 0000000..e42e1b9 Binary files /dev/null and b/out/production/practice_self/SLList.class differ diff --git a/out/production/practice_self/Set61B.class b/out/production/practice_self/Set61B.class new file mode 100644 index 0000000..37bbed5 Binary files /dev/null and b/out/production/practice_self/Set61B.class differ diff --git a/out/production/practice_self/Set61B_ArraySet$ArraySetIterator.class b/out/production/practice_self/Set61B_ArraySet$ArraySetIterator.class new file mode 100644 index 0000000..72561c2 Binary files /dev/null and b/out/production/practice_self/Set61B_ArraySet$ArraySetIterator.class differ diff --git a/out/production/practice_self/Set61B_ArraySet.class b/out/production/practice_self/Set61B_ArraySet.class new file mode 100644 index 0000000..4272603 Binary files /dev/null and b/out/production/practice_self/Set61B_ArraySet.class differ diff --git a/out/production/practice_self/lecture_02_practice.class b/out/production/practice_self/lecture_02_practice.class new file mode 100644 index 0000000..82bd578 Binary files /dev/null and b/out/production/practice_self/lecture_02_practice.class differ diff --git a/out/production/practice_self/merge_sort.class b/out/production/practice_self/merge_sort.class new file mode 100644 index 0000000..7dad1f2 Binary files /dev/null and b/out/production/practice_self/merge_sort.class differ diff --git a/out/production/practice_self/selection_sort.class b/out/production/practice_self/selection_sort.class new file mode 100644 index 0000000..8937ccd Binary files /dev/null and b/out/production/practice_self/selection_sort.class differ diff --git a/picture/2021-03-01-java-stringbuffer.svg b/picture/2021-03-01-java-stringbuffer.svg new file mode 100644 index 0000000..614af99 --- /dev/null +++ b/picture/2021-03-01-java-stringbuffer.svg @@ -0,0 +1,3 @@ + + +
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%226%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23000000%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22340%22%20y%3D%22280%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
8%3C...
9
9
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%226%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23000000%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22340%22%20y%3D%22280%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
8%3C...
9
9
StringBuilder sb = new StringBuilder(10);
StringBuilder sb = new StringBuilder(10);
sb.append("Runoob..");
sb.append("Runoob..");
R
R
u
u
n
n
o
o
o
o
b
b
.
.
.
.
sb.append('!');
sb.append('!');
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%226%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23000000%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22340%22%20y%3D%22280%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
8%3C...
9
9
R
R
u
u
n
n
o
o
o
o
b
b
.
.
.
.
!
!
sb.insert(8,"Java");
sb.insert(8,"Java");
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%226%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23000000%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22340%22%20y%3D%22280%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
8%3C...
9
9
R
R
u
u
n
n
o
o
o
o
b
b
.
.
.
.
10%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%229%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23FF3333%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22502%22%20y%3D%22659%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
10%3...
11%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%2210%26lt%3Bspan%20style%3D%26quot%3Bcolor%3A%20rgba(0%20%2C%200%20%2C%200%20%2C%200)%20%3B%20font-family%3A%20monospace%20%3B%20font-size%3A%200px%26quot%3B%26gt%3B%253CmxGraphModel%253E%253Croot%253E%253CmxCell%2520id%253D%25220%2522%252F%253E%253CmxCell%2520id%253D%25221%2522%2520parent%253D%25220%2522%252F%253E%253CmxCell%2520id%253D%25222%2522%2520value%253D%25229%2522%2520style%253D%2522text%253Bhtml%253D1%253BstrokeColor%253Dnone%253BfillColor%253Dnone%253Balign%253Dcenter%253BverticalAlign%253Dmiddle%253BwhiteSpace%253Dwrap%253Brounded%253D0%253Bshadow%253D1%253BfontFamily%253DComic%2520Sans%2520MS%253BfontSize%253D18%253BfontColor%253D%2523FF3333%253B%2522%2520vertex%253D%25221%2522%2520parent%253D%25221%2522%253E%253CmxGeometry%2520x%253D%2522502%2522%2520y%253D%2522659%2522%2520width%253D%252240%2522%2520height%253D%252220%2522%2520as%253D%2522geometry%2522%252F%253E%253C%252FmxCell%253E%253C%252Froot%253E%253C%252FmxGraphModel%253E%26lt%3B%2Fspan%26gt%3B%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23FF3333%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22542%22%20y%3D%22659%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
11%3...
13
13
12
12
J
J
a
a
v
v
a
a
!
!
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%226%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23000000%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22340%22%20y%3D%22280%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
8%3C...
9
9
R
R
u
u
n
n
o
o
o
o
10%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%229%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23FF3333%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22502%22%20y%3D%22659%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
10%3...
11%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%2210%26lt%3Bspan%20style%3D%26quot%3Bcolor%3A%20rgba(0%20%2C%200%20%2C%200%20%2C%200)%20%3B%20font-family%3A%20monospace%20%3B%20font-size%3A%200px%26quot%3B%26gt%3B%253CmxGraphModel%253E%253Croot%253E%253CmxCell%2520id%253D%25220%2522%252F%253E%253CmxCell%2520id%253D%25221%2522%2520parent%253D%25220%2522%252F%253E%253CmxCell%2520id%253D%25222%2522%2520value%253D%25229%2522%2520style%253D%2522text%253Bhtml%253D1%253BstrokeColor%253Dnone%253BfillColor%253Dnone%253Balign%253Dcenter%253BverticalAlign%253Dmiddle%253BwhiteSpace%253Dwrap%253Brounded%253D0%253Bshadow%253D1%253BfontFamily%253DComic%2520Sans%2520MS%253BfontSize%253D18%253BfontColor%253D%2523FF3333%253B%2522%2520vertex%253D%25221%2522%2520parent%253D%25221%2522%253E%253CmxGeometry%2520x%253D%2522502%2522%2520y%253D%2522659%2522%2520width%253D%252240%2522%2520height%253D%252220%2522%2520as%253D%2522geometry%2522%252F%253E%253C%252FmxCell%253E%253C%252Froot%253E%253C%252FmxGraphModel%253E%26lt%3B%2Fspan%26gt%3B%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3Bshadow%3D1%3BfontFamily%3DComic%20Sans%20MS%3BfontSize%3D18%3BfontColor%3D%23FF3333%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22542%22%20y%3D%22659%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
11%3...
13
13
12
12
J
J
a
a
v
v
a
a
!
!
sb.delete(5,8);
sb.delete(5,8);
Viewer does not support full SVG 1.1
\ No newline at end of file diff --git a/picture/2022-05-21_222221.PNG b/picture/2022-05-21_222221.PNG new file mode 100644 index 0000000..268ecd7 Binary files /dev/null and b/picture/2022-05-21_222221.PNG differ diff --git a/picture/2022-05-23_150825.PNG b/picture/2022-05-23_150825.PNG new file mode 100644 index 0000000..c14e3ac Binary files /dev/null and b/picture/2022-05-23_150825.PNG differ diff --git a/picture/2022-05-23_201104.PNG b/picture/2022-05-23_201104.PNG new file mode 100644 index 0000000..b2fa320 Binary files /dev/null and b/picture/2022-05-23_201104.PNG differ diff --git a/picture/2022-05-23_201459.PNG b/picture/2022-05-23_201459.PNG new file mode 100644 index 0000000..08d7ffe Binary files /dev/null and b/picture/2022-05-23_201459.PNG differ diff --git a/picture/2022-05-23_201640.PNG b/picture/2022-05-23_201640.PNG new file mode 100644 index 0000000..3e4d4fd Binary files /dev/null and b/picture/2022-05-23_201640.PNG differ diff --git a/picture/2022-05-23_201953.PNG b/picture/2022-05-23_201953.PNG new file mode 100644 index 0000000..6093d70 Binary files /dev/null and b/picture/2022-05-23_201953.PNG differ diff --git a/picture/2022-05-23_203359.PNG b/picture/2022-05-23_203359.PNG new file mode 100644 index 0000000..ed69e03 Binary files /dev/null and b/picture/2022-05-23_203359.PNG differ diff --git a/picture/2022-05-23_203828.PNG b/picture/2022-05-23_203828.PNG new file mode 100644 index 0000000..ad9f965 Binary files /dev/null and b/picture/2022-05-23_203828.PNG differ diff --git a/picture/2022-05-23_204057.PNG b/picture/2022-05-23_204057.PNG new file mode 100644 index 0000000..87f332a Binary files /dev/null and b/picture/2022-05-23_204057.PNG differ diff --git a/picture/2022-05-23_204207.PNG b/picture/2022-05-23_204207.PNG new file mode 100644 index 0000000..4e70402 Binary files /dev/null and b/picture/2022-05-23_204207.PNG differ diff --git a/picture/2022-05-23_204623.PNG b/picture/2022-05-23_204623.PNG new file mode 100644 index 0000000..af6a645 Binary files /dev/null and b/picture/2022-05-23_204623.PNG differ diff --git a/picture/2022-05-23_204657.PNG b/picture/2022-05-23_204657.PNG new file mode 100644 index 0000000..5ece2e5 Binary files /dev/null and b/picture/2022-05-23_204657.PNG differ diff --git a/picture/2022-05-23_205109.PNG b/picture/2022-05-23_205109.PNG new file mode 100644 index 0000000..c16a9d9 Binary files /dev/null and b/picture/2022-05-23_205109.PNG differ diff --git a/picture/2022-05-23_205212.PNG b/picture/2022-05-23_205212.PNG new file mode 100644 index 0000000..e3ad30c Binary files /dev/null and b/picture/2022-05-23_205212.PNG differ diff --git a/picture/2022-05-23_214756.PNG b/picture/2022-05-23_214756.PNG new file mode 100644 index 0000000..e46237b Binary files /dev/null and b/picture/2022-05-23_214756.PNG differ diff --git a/picture/2022-05-23_214930.PNG b/picture/2022-05-23_214930.PNG new file mode 100644 index 0000000..3f8a48f Binary files /dev/null and b/picture/2022-05-23_214930.PNG differ diff --git a/picture/2022-05-23_215101.PNG b/picture/2022-05-23_215101.PNG new file mode 100644 index 0000000..ede0c00 Binary files /dev/null and b/picture/2022-05-23_215101.PNG differ diff --git a/picture/2022-05-23_220807.PNG b/picture/2022-05-23_220807.PNG new file mode 100644 index 0000000..8d104c8 Binary files /dev/null and b/picture/2022-05-23_220807.PNG differ diff --git a/picture/2022-06-03_201110.PNG b/picture/2022-06-03_201110.PNG new file mode 100644 index 0000000..72cc598 Binary files /dev/null and b/picture/2022-06-03_201110.PNG differ diff --git a/picture/61B depth first paths demo.pptx b/picture/61B depth first paths demo.pptx new file mode 100644 index 0000000..c654fe8 Binary files /dev/null and b/picture/61B depth first paths demo.pptx differ diff --git a/picture/9.3.3.png b/picture/9.3.3.png new file mode 100644 index 0000000..9b36d43 Binary files /dev/null and b/picture/9.3.3.png differ diff --git a/picture/A_ Algorithm Demo.pptx b/picture/A_ Algorithm Demo.pptx new file mode 100644 index 0000000..85936ec Binary files /dev/null and b/picture/A_ Algorithm Demo.pptx differ diff --git a/picture/B.PNG b/picture/B.PNG new file mode 100644 index 0000000..94f0734 Binary files /dev/null and b/picture/B.PNG differ diff --git a/picture/B1.PNG b/picture/B1.PNG new file mode 100644 index 0000000..7597bb3 Binary files /dev/null and b/picture/B1.PNG differ diff --git a/picture/B2.PNG b/picture/B2.PNG new file mode 100644 index 0000000..31570ff Binary files /dev/null and b/picture/B2.PNG differ diff --git a/picture/B3.PNG b/picture/B3.PNG new file mode 100644 index 0000000..2b80e04 Binary files /dev/null and b/picture/B3.PNG differ diff --git a/picture/HashTable1.PNG b/picture/HashTable1.PNG new file mode 100644 index 0000000..104ba60 Binary files /dev/null and b/picture/HashTable1.PNG differ diff --git a/picture/HashTable_performance1.PNG b/picture/HashTable_performance1.PNG new file mode 100644 index 0000000..d9bc0a8 Binary files /dev/null and b/picture/HashTable_performance1.PNG differ diff --git a/picture/HashTable_performance2.PNG b/picture/HashTable_performance2.PNG new file mode 100644 index 0000000..750647f Binary files /dev/null and b/picture/HashTable_performance2.PNG differ diff --git a/picture/HashTable_performance3.PNG b/picture/HashTable_performance3.PNG new file mode 100644 index 0000000..15338bb Binary files /dev/null and b/picture/HashTable_performance3.PNG differ diff --git a/picture/Hash_motivate1.PNG b/picture/Hash_motivate1.PNG new file mode 100644 index 0000000..b7ad187 Binary files /dev/null and b/picture/Hash_motivate1.PNG differ diff --git a/picture/Hash_motivate2.PNG b/picture/Hash_motivate2.PNG new file mode 100644 index 0000000..4ec4846 Binary files /dev/null and b/picture/Hash_motivate2.PNG differ diff --git a/picture/Hash_motivate3.PNG b/picture/Hash_motivate3.PNG new file mode 100644 index 0000000..c17ee69 Binary files /dev/null and b/picture/Hash_motivate3.PNG differ diff --git a/picture/Heap1.PNG b/picture/Heap1.PNG new file mode 100644 index 0000000..c3cb068 Binary files /dev/null and b/picture/Heap1.PNG differ diff --git a/picture/LLRB_build_1.PNG b/picture/LLRB_build_1.PNG new file mode 100644 index 0000000..36136a2 Binary files /dev/null and b/picture/LLRB_build_1.PNG differ diff --git a/picture/LLRB_build_2.PNG b/picture/LLRB_build_2.PNG new file mode 100644 index 0000000..db4af1d Binary files /dev/null and b/picture/LLRB_build_2.PNG differ diff --git a/picture/LLRB_build_3.PNG b/picture/LLRB_build_3.PNG new file mode 100644 index 0000000..11629e6 Binary files /dev/null and b/picture/LLRB_build_3.PNG differ diff --git a/picture/LLRB_build_4.PNG b/picture/LLRB_build_4.PNG new file mode 100644 index 0000000..a5f7fea Binary files /dev/null and b/picture/LLRB_build_4.PNG differ diff --git a/picture/LLRB_build_5.PNG b/picture/LLRB_build_5.PNG new file mode 100644 index 0000000..04f0a19 Binary files /dev/null and b/picture/LLRB_build_5.PNG differ diff --git a/picture/LLRB_build_6.PNG b/picture/LLRB_build_6.PNG new file mode 100644 index 0000000..6390e6d Binary files /dev/null and b/picture/LLRB_build_6.PNG differ diff --git a/picture/LLRB_runtime.PNG b/picture/LLRB_runtime.PNG new file mode 100644 index 0000000..ce73846 Binary files /dev/null and b/picture/LLRB_runtime.PNG differ diff --git a/picture/Lecture 27 Software Engineering I.pptx b/picture/Lecture 27 Software Engineering I.pptx new file mode 100644 index 0000000..243c5e4 Binary files /dev/null and b/picture/Lecture 27 Software Engineering I.pptx differ diff --git a/picture/Lecture 28 Decomposition and Reductions.pptx b/picture/Lecture 28 Decomposition and Reductions.pptx new file mode 100644 index 0000000..4af9d84 Binary files /dev/null and b/picture/Lecture 28 Decomposition and Reductions.pptx differ diff --git a/picture/Prefix operatio& Tries.pptx b/picture/Prefix operatio& Tries.pptx new file mode 100644 index 0000000..35575b9 Binary files /dev/null and b/picture/Prefix operatio& Tries.pptx differ diff --git a/picture/asymptotics2_tree2.png b/picture/asymptotics2_tree2.png new file mode 100644 index 0000000..3e0f811 Binary files /dev/null and b/picture/asymptotics2_tree2.png differ diff --git a/picture/avoid_collisions1.PNG b/picture/avoid_collisions1.PNG new file mode 100644 index 0000000..01f560a Binary files /dev/null and b/picture/avoid_collisions1.PNG differ diff --git a/picture/avoid_collisions2.PNG b/picture/avoid_collisions2.PNG new file mode 100644 index 0000000..ca2eb69 Binary files /dev/null and b/picture/avoid_collisions2.PNG differ diff --git a/picture/cs61b 2020 ds5 lec19 kd trees.pptx b/picture/cs61b 2020 ds5 lec19 kd trees.pptx new file mode 100644 index 0000000..12a86bb Binary files /dev/null and b/picture/cs61b 2020 ds5 lec19 kd trees.pptx differ diff --git a/picture/cs61b 2020 lec 25 shortest paths.pptx b/picture/cs61b 2020 lec 25 shortest paths.pptx new file mode 100644 index 0000000..6e52987 Binary files /dev/null and b/picture/cs61b 2020 lec 25 shortest paths.pptx differ diff --git a/picture/cs61b 2020 lec 26 mst.pptx b/picture/cs61b 2020 lec 26 mst.pptx new file mode 100644 index 0000000..3b13bd9 Binary files /dev/null and b/picture/cs61b 2020 lec 26 mst.pptx differ diff --git a/picture/cs61b 2021 ds6 lec20 heaps and pq.pptx b/picture/cs61b 2021 ds6 lec20 heaps and pq.pptx new file mode 100644 index 0000000..87d23dd Binary files /dev/null and b/picture/cs61b 2021 ds6 lec20 heaps and pq.pptx differ diff --git a/picture/cs61b 2021 lec 22 graphs2 - dfs, bfs.pptx b/picture/cs61b 2021 lec 22 graphs2 - dfs, bfs.pptx new file mode 100644 index 0000000..961b1a4 Binary files /dev/null and b/picture/cs61b 2021 lec 22 graphs2 - dfs, bfs.pptx differ diff --git a/picture/cs61b 2021 lec21 graphs1 - intro to graphs.pptx b/picture/cs61b 2021 lec21 graphs1 - intro to graphs.pptx new file mode 100644 index 0000000..16f5021 Binary files /dev/null and b/picture/cs61b 2021 lec21 graphs1 - intro to graphs.pptx differ diff --git a/picture/cs61b Breadth First Paths Demo (2019).pptx b/picture/cs61b Breadth First Paths Demo (2019).pptx new file mode 100644 index 0000000..74197a8 Binary files /dev/null and b/picture/cs61b Breadth First Paths Demo (2019).pptx differ diff --git a/picture/cs61b kdtree insertion demo.pptx b/picture/cs61b kdtree insertion demo.pptx new file mode 100644 index 0000000..b5f137a Binary files /dev/null and b/picture/cs61b kdtree insertion demo.pptx differ diff --git a/picture/cs61b kdtree nearest demo.pptx b/picture/cs61b kdtree nearest demo.pptx new file mode 100644 index 0000000..efed49e Binary files /dev/null and b/picture/cs61b kdtree nearest demo.pptx differ diff --git a/picture/hashTable.PNG b/picture/hashTable.PNG new file mode 100644 index 0000000..a8ec2bd Binary files /dev/null and b/picture/hashTable.PNG differ diff --git a/picture/hashTable2.PNG b/picture/hashTable2.PNG new file mode 100644 index 0000000..8419d8a Binary files /dev/null and b/picture/hashTable2.PNG differ diff --git a/picture/heap10.PNG b/picture/heap10.PNG new file mode 100644 index 0000000..63aca31 Binary files /dev/null and b/picture/heap10.PNG differ diff --git a/picture/heap11.PNG b/picture/heap11.PNG new file mode 100644 index 0000000..d08aebb Binary files /dev/null and b/picture/heap11.PNG differ diff --git a/picture/heap12.PNG b/picture/heap12.PNG new file mode 100644 index 0000000..f92c9ea Binary files /dev/null and b/picture/heap12.PNG differ diff --git a/picture/heap13.PNG b/picture/heap13.PNG new file mode 100644 index 0000000..d69d71e Binary files /dev/null and b/picture/heap13.PNG differ diff --git a/picture/heap14.PNG b/picture/heap14.PNG new file mode 100644 index 0000000..2f33285 Binary files /dev/null and b/picture/heap14.PNG differ diff --git a/picture/heap15.PNG b/picture/heap15.PNG new file mode 100644 index 0000000..500da73 Binary files /dev/null and b/picture/heap15.PNG differ diff --git a/picture/heap16.PNG b/picture/heap16.PNG new file mode 100644 index 0000000..14c49d8 Binary files /dev/null and b/picture/heap16.PNG differ diff --git a/picture/heap17.PNG b/picture/heap17.PNG new file mode 100644 index 0000000..e7e35ee Binary files /dev/null and b/picture/heap17.PNG differ diff --git a/picture/heap18.PNG b/picture/heap18.PNG new file mode 100644 index 0000000..17b7e31 Binary files /dev/null and b/picture/heap18.PNG differ diff --git a/picture/heap19.PNG b/picture/heap19.PNG new file mode 100644 index 0000000..0464200 Binary files /dev/null and b/picture/heap19.PNG differ diff --git a/picture/heap2.PNG b/picture/heap2.PNG new file mode 100644 index 0000000..bbe9c2c Binary files /dev/null and b/picture/heap2.PNG differ diff --git a/picture/heap20.PNG b/picture/heap20.PNG new file mode 100644 index 0000000..84ae876 Binary files /dev/null and b/picture/heap20.PNG differ diff --git a/picture/heap21.PNG b/picture/heap21.PNG new file mode 100644 index 0000000..e4a74eb Binary files /dev/null and b/picture/heap21.PNG differ diff --git a/picture/heap22.PNG b/picture/heap22.PNG new file mode 100644 index 0000000..2353852 Binary files /dev/null and b/picture/heap22.PNG differ diff --git a/picture/heap3.PNG b/picture/heap3.PNG new file mode 100644 index 0000000..3faa8d3 Binary files /dev/null and b/picture/heap3.PNG differ diff --git a/picture/heap4.PNG b/picture/heap4.PNG new file mode 100644 index 0000000..7bb3f44 Binary files /dev/null and b/picture/heap4.PNG differ diff --git a/picture/heap5.PNG b/picture/heap5.PNG new file mode 100644 index 0000000..b55bc78 Binary files /dev/null and b/picture/heap5.PNG differ diff --git a/picture/heap6.PNG b/picture/heap6.PNG new file mode 100644 index 0000000..39f3ffb Binary files /dev/null and b/picture/heap6.PNG differ diff --git a/picture/heap7.PNG b/picture/heap7.PNG new file mode 100644 index 0000000..90c9f59 Binary files /dev/null and b/picture/heap7.PNG differ diff --git a/picture/heap8.PNG b/picture/heap8.PNG new file mode 100644 index 0000000..15a28aa Binary files /dev/null and b/picture/heap8.PNG differ diff --git a/picture/heap9.PNG b/picture/heap9.PNG new file mode 100644 index 0000000..d28c37e Binary files /dev/null and b/picture/heap9.PNG differ diff --git a/picture/image-20220511205820023.png b/picture/image-20220511205820023.png new file mode 100644 index 0000000..8f796e9 Binary files /dev/null and b/picture/image-20220511205820023.png differ diff --git a/picture/read_black_1.PNG b/picture/read_black_1.PNG new file mode 100644 index 0000000..b19cbf2 Binary files /dev/null and b/picture/read_black_1.PNG differ diff --git a/picture/read_black_2.PNG b/picture/read_black_2.PNG new file mode 100644 index 0000000..fb95ad0 Binary files /dev/null and b/picture/read_black_2.PNG differ diff --git a/picture/red_black_3.PNG b/picture/red_black_3.PNG new file mode 100644 index 0000000..6dc311a Binary files /dev/null and b/picture/red_black_3.PNG differ diff --git a/picture/rotate1.PNG b/picture/rotate1.PNG new file mode 100644 index 0000000..90347a2 Binary files /dev/null and b/picture/rotate1.PNG differ diff --git a/picture/rotate2.PNG b/picture/rotate2.PNG new file mode 100644 index 0000000..3b36d27 Binary files /dev/null and b/picture/rotate2.PNG differ diff --git a/picture/rotate3.PNG b/picture/rotate3.PNG new file mode 100644 index 0000000..2337e17 Binary files /dev/null and b/picture/rotate3.PNG differ diff --git a/picture/rotate4.PNG b/picture/rotate4.PNG new file mode 100644 index 0000000..0cd21ec Binary files /dev/null and b/picture/rotate4.PNG differ diff --git a/Discussion/.idea/Discussion.iml b/practice_self/practice_self.iml similarity index 76% rename from Discussion/.idea/Discussion.iml rename to practice_self/practice_self.iml index 6b648a4..28ac7fd 100644 --- a/Discussion/.idea/Discussion.iml +++ b/practice_self/practice_self.iml @@ -7,7 +7,9 @@ - + + + diff --git a/practice_self/src/AList.java b/practice_self/src/AList.java new file mode 100644 index 0000000..026d58a --- /dev/null +++ b/practice_self/src/AList.java @@ -0,0 +1,43 @@ +import java.util.Arrays; + +/** + * @author Li Zezhong + * @create 2022-05-08 19:13 + */ +public class AList { + private int size; + private int [] item; + + public AList() { + this.size = 0; + this.item = new int [size]; + } + + /** Inserts X into the back of the list. */ + public void addLast(int x) { + item[size+1] =x; + size +=1; + } + + /** Returns the item from the back of the list. */ + public int getLast() { + return this.item[size-1]; + } + /** Gets the ith item in the list (0 is the front). */ + public int get(int i) { + return this.item[i]; + } + + /** Returns the number of items in the list. */ + public int size() { + return this.size; + } + + /** Deletes item from back of the list and + * returns deleted item. */ + public int removeLast() { + int x = this.getLast(); + size -=1; + return x; + } +} diff --git a/practice_self/src/BST61B.java b/practice_self/src/BST61B.java new file mode 100644 index 0000000..3ee2665 --- /dev/null +++ b/practice_self/src/BST61B.java @@ -0,0 +1,61 @@ +/** + * @author Li Zezhong + * @create 2022-05-20 16:02 + */ + +public class BST61B{ + public static void main(String[] args) { + + } +} + + class BST { + public Key key; + public BST left; + public BST right; + + public BST(Key key){ + this.key = key; + } + + public BST(BST left,BST right,Key key){ + this.left = left; + this.right = right; + this.key = key; + } + + /* 如果树非常浓密,则该操作时间复杂度为Θ(log(N)) (log(N)为树高) + * 注意:这里应该为课上写的pseudo code,正式的代码需要你在对应的lab中完成! */ + public static BST search(BST T,Key key){ + if(T==null){ + return null; + } + if(T.key.equals(key)){ + return T; + } else if(key>T.key){ + return search(T.right,key); + } else{ + return search(T.left,key); + } + } + + + /* 利用递归实现BST的insert操作! + * 注意:这里应该为课上写的pseudo code,正式的代码需要你在对应的lab中完成! */ + public static BST insert(BST T, Key ik){ + if(T==null){ + return new BST(ik); + } + if(ikT.key){ + T.right = insert(T.right,ik); + } + return T; + } +// +// public static BST delete(){ +// +// } +} diff --git a/practice_self/src/Binary_Search.java b/practice_self/src/Binary_Search.java new file mode 100644 index 0000000..9e749ec --- /dev/null +++ b/practice_self/src/Binary_Search.java @@ -0,0 +1,31 @@ +import java.util.Arrays; + +/** + * @author Li Zezhong + * @create 2022-05-18 20:18 + */ +/* Binary Search Time complexity: Θ(log(N))*/ +public class Binary_Search { + public static void main(String[] args) { + int [] array = new int []{6,13,14,25,33,43,51,53,64,72,84,93,95,96,97}; + System.out.println(binary_search(array,97)); + } + + public static int binary_search(int [] array,int target){ + Arrays.sort(array); + int first =0; + int last =array.length-1; + int mid = array.length/2; + while(first<=last){ + if(array[mid]>target){ + last = mid-1; + } else if(array[mid]{ + public void addLast(item x); + public void addFrist(item x); + public item removeLast(); + public item get(int x); + public item getFirst(); + public item getLast(); + public void insert(item x,int position); + public int size(); +} diff --git a/practice_self/src/List61B_AList.java b/practice_self/src/List61B_AList.java new file mode 100644 index 0000000..107e549 --- /dev/null +++ b/practice_self/src/List61B_AList.java @@ -0,0 +1,46 @@ +/** + * @author Li Zezhong + * @create 2022-05-08 20:37 + */ +public class List61B_AList implements List61B{ + + @Override + public void addLast(item x) { + + } + + @Override + public void addFrist(item x) { + + } + + @Override + public item removeLast() { + return null; + } + + @Override + public item get(int x) { + return null; + } + + @Override + public item getFirst() { + return null; + } + + @Override + public item getLast() { + return null; + } + + @Override + public void insert(item x, int position) { + + } + + @Override + public int size() { + return 0; + } +} diff --git a/practice_self/src/List61B_SLList.java b/practice_self/src/List61B_SLList.java new file mode 100644 index 0000000..2e20031 --- /dev/null +++ b/practice_self/src/List61B_SLList.java @@ -0,0 +1,55 @@ +/** + * @author Li Zezhong + * @create 2022-05-08 20:17 + */ +public class List61B_SLList implements List61B { + item first; + List61B_SLList next; + + public List61B_SLList(item first){ + this.first = first; + next = null; + } + + + @Override + public void addLast(item x) { + this.next.first = x; + this.next.next = null; + } + + @Override + public void addFrist(item x) { + + } + + @Override + public item removeLast() { + return null; + } + + @Override + public item get(int x) { + return null; + } + + @Override + public item getFirst() { + return null; + } + + @Override + public item getLast() { + return null; + } + + @Override + public void insert(item x, int position) { + + } + + @Override + public int size() { + return 0; + } +} diff --git a/practice_self/src/Map61B.java b/practice_self/src/Map61B.java new file mode 100644 index 0000000..1834757 --- /dev/null +++ b/practice_self/src/Map61B.java @@ -0,0 +1,11 @@ +/** + * @author Li Zezhong + * @create 2022-05-10 22:04 + */ +public interface Map61B { + public void put(item key,int value); + public boolean containsKey(item key); + public int get(item key); + public SLList keys(); + public int size(); +} diff --git a/practice_self/src/Map61B_ArrayMap.java b/practice_self/src/Map61B_ArrayMap.java new file mode 100644 index 0000000..b83a95a --- /dev/null +++ b/practice_self/src/Map61B_ArrayMap.java @@ -0,0 +1,33 @@ +/** + * @author Li Zezhong + * @create 2022-05-10 22:10 + */ + +import java.util.List; +public class Map61B_ArrayMap implements Map61B{ + @Override + public void put(item key, int value) { + + } + + @Override + public boolean containsKey(item key) { + return false; + } + + @Override + public int get(item key) { + return 0; + } + + @Override + public SLList keys() { + return null; + } + + @Override + public int size() { + return 0; + } +} + diff --git a/practice_self/src/SLList.java b/practice_self/src/SLList.java new file mode 100644 index 0000000..6bf380d --- /dev/null +++ b/practice_self/src/SLList.java @@ -0,0 +1,66 @@ +/** + * @author Li Zezhong + * @create 2022-05-08 19:11 + */ +public class SLList { + static class IntNode { + public int item; + public IntNode next; + + public IntNode(int i, IntNode n) { + item = i; + next = n; + } + } + + private IntNode first; + private int size; + private IntNode sentinel; + public SLList(int x) { + first = new IntNode(x, null); + sentinel = new IntNode(0,first); + size = 1; + } + + public SLList(){ + sentinel = new IntNode(0,null); + first = null; + size = 0; + } + + public int getFirst(){ + return this.first.item; + } + + public void addFirst(int b){ + this.sentinel.next = new IntNode(b, this.first); + this.first = this.sentinel.next; + this.size +=1; + } + + public void addLast(IntNode p, int x){ + if(p.next ==null) { + p.next = new IntNode(x, null); + } else{ + addLast(p.next,x); + } + } + + public void addLast(int x){ + addLast(this.sentinel,x); + this.size +=1; + } + + public int size(){ + return this.size; + } + + public void print_SLList(){ + IntNode p =this.first; + while(p.next !=null){ + System.out.println(p.item); + p = p.next; + } + System.out.println(p.item); + } +} diff --git a/practice_self/src/Set61B.java b/practice_self/src/Set61B.java new file mode 100644 index 0000000..c16e7dd --- /dev/null +++ b/practice_self/src/Set61B.java @@ -0,0 +1,9 @@ +/** + * @author Li Zezhong + * @create 2022-05-14 18:44 + */ +public interface Set61B { + boolean add(E value); + boolean contains(E value); + int size(); +} diff --git a/practice_self/src/Set61B_ArraySet.java b/practice_self/src/Set61B_ArraySet.java new file mode 100644 index 0000000..a9ff4f0 --- /dev/null +++ b/practice_self/src/Set61B_ArraySet.java @@ -0,0 +1,103 @@ +import java.lang.reflect.Array; +import java.util.Iterator; + +/** + * @author Li Zezhong + * @create 2022-05-14 18:42 + */ +public class Set61B_ArraySet implements Set61B,Iterable{ + private E [] items; + private int size; + + + public Iterator iterator() { + return new ArraySetIterator(); + } + + private class ArraySetIterator implements Iterator{ + private int position; + + public ArraySetIterator(){ + position = 0; + } + + @Override + public boolean hasNext() { + return position < size; + } + + @Override + public E next() { + E result = items[position]; + position +=1; + return result; + } + } + + public Set61B_ArraySet(){ + items = (E[]) new Object[100]; + this.size = 0; + } + + @Override + public boolean add(E value) { + if (value == null) { + throw new IllegalArgumentException("can't add null"); + } + if(contains(value)){ + return false; + } + items[size]=value; + this.size +=1; + return true; + } + + @Override + public boolean contains(E value) { + for (int i = 0; i < size; i += 1) { + if(items[i].equals(value)){ + return true; + } + } + return false; + } + + @Override + public int size() { + return this.size; + } + + public String toString(){ + StringBuilder result = new StringBuilder("{"); + int i=0; + for(;i o = (Set61B_ArraySet) other; + if (o.size() != this.size()) { + return false; + } + for (E item : this) { + if (!o.contains(item)) { + return false; + } + } + return true; + } +} diff --git a/practice_self/src/lecture_02_practice.java b/practice_self/src/lecture_02_practice.java new file mode 100644 index 0000000..69ebb0a --- /dev/null +++ b/practice_self/src/lecture_02_practice.java @@ -0,0 +1,44 @@ +import java.sql.SQLOutput; +import java.util.*; + +/** + * @author Li Zezhong + * @create 2022-05-05 18:22 + */ +public class lecture_02_practice { + public static void main(String[] args) { + int [][] grid = {{1,2,5},{3,2,1}}; + System.out.println(maxValue(grid)); + } + + public static int maxValue(int[][] grid) { + int i=0; + int j=0; + int sum= grid[0][0]; + while((igrid[i][j+1])){ + i=i+1; + } else if((grid[i+1][j]<=grid[i][j+1])){ + j=j+1; + } + } + return sum; + } +} + + + + + + + + diff --git a/practice_self/src/merge_sort.java b/practice_self/src/merge_sort.java new file mode 100644 index 0000000..0574b58 --- /dev/null +++ b/practice_self/src/merge_sort.java @@ -0,0 +1,14 @@ +/** + * @author Li Zezhong + * @create 2022-05-18 21:18 + */ +public class merge_sort { + public static void main(String[] args) { + + } + + public static int [] Merge_sort(int [] array){ + /*待完成!*/ + return array; + } +} diff --git a/practice_self/src/selection_sort.java b/practice_self/src/selection_sort.java new file mode 100644 index 0000000..6149fb6 --- /dev/null +++ b/practice_self/src/selection_sort.java @@ -0,0 +1,35 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Stack; + +/** + * @author Li Zezhong + * @create 2022-05-08 19:13 + */ +public class selection_sort { + public static void main(String[] args) { + int [] a = {2, 3, 1, 0, 7, 5, 3}; + int buffer; + int buffer_id; + for(int i=0;i