├── AATree ├── AATree.cpp ├── AATree.h ├── README.md └── test.cpp ├── AVLTree ├── AVLTree.cpp ├── AVLTree.h └── test.cpp ├── AdjacencyList(Version2) ├── AdjacencyList.cpp ├── AdjacencyList.h ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── README.md ├── TopSort.cpp ├── queue.cpp ├── queue.h └── test.cpp ├── AdjacencyList ├── AdjacencyList.cpp ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── README.md ├── queue.cpp ├── queue.h └── test.cpp ├── BinaryHeap ├── binaryHeap.cpp ├── binaryHeap.h └── test.cpp ├── DisjSet ├── DisjSet.cpp ├── DisjSet.h └── test.cpp ├── HashTable(Open Addressing Hashing) ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h └── test.cpp ├── HashTable(separate chaining) ├── HashTable.cpp ├── HashTable.h ├── README.md ├── list.cpp ├── list.h └── test.cpp ├── KdTree ├── 2-dTree.cpp ├── 2-dTree.h ├── README.md └── test.cpp ├── LeftistHeap ├── LeftistHeap.cpp ├── LeftistHeap.h └── test.cpp ├── MaxNetStream ├── AdjacencyList.cpp ├── AdjacencyList.h ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── MaxNetStream.cpp ├── README.md ├── queue.cpp ├── queue.h └── test.cpp ├── MinTree ├── AdjacencyList.cpp ├── AdjacencyList.h ├── DisjSet.cpp ├── DisjSet.h ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── MinTree.cpp ├── README.md ├── binaryHeap.cpp ├── binaryHeap.h └── test.cpp ├── PairHeap ├── PairingHeap.cpp ├── PairingHeap.h ├── README.md └── test.cpp ├── README.md ├── RedBlackTree ├── README.md ├── RedBlackTree.cpp ├── RedBlackTree.h └── test.cpp ├── SearchTree ├── searchTree.cpp ├── searchTree.h └── test.cpp ├── ShortestPath ├── AdjacencyList.cpp ├── AdjacencyList.h ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── README.md ├── ShortestPath.cpp ├── binaryHeap.cpp ├── binaryHeap.h └── test.cpp ├── Sort ├── binaryHeap.cpp ├── binaryHeap.h ├── sort.cpp ├── sort.h └── test.cpp ├── SplayTree(Bottom Up) ├── SplayTree.cpp ├── SplayTree.h └── test.cpp ├── SplayTree(Up Down) ├── SplayTree.cpp ├── SplayTree.h └── test.cpp ├── Treap ├── README.md ├── Treap.cpp ├── Treap.h └── test.cpp ├── binomialQueue ├── binomialQueue.cpp ├── binomialQueue.h └── test.cpp ├── deepFirstSearch ├── AdjacencyList.cpp ├── AdjacencyList.h ├── HashTableOpenAdd.cpp ├── HashTableOpenAdd.h ├── README.md ├── deepFirst.cpp ├── list.cpp ├── list.h └── test.cpp ├── dynamicProgramming ├── README.md ├── dynamicPro.cpp ├── dynamicPro.h └── test.cpp ├── list ├── list.cpp └── list.h ├── nearPoint ├── README.md ├── nearPoint.cpp ├── nearPoint.h └── test.cpp ├── queue ├── queue.cpp └── queue.h ├── roadRebuild ├── README.md ├── roadRebuild.cpp ├── roadRebuild.h └── test.cpp ├── skipList ├── README.md ├── SkipList.cpp ├── SkipList.h └── test.cpp ├── stack ├── stack.cpp └── stack.h └── tic_tac_toe ├── README.md ├── main.cpp ├── tic_tac_toe.h └── tie_tac_toe.cpp /AATree/AATree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AATree/AATree.cpp -------------------------------------------------------------------------------- /AATree/AATree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AATree/AATree.h -------------------------------------------------------------------------------- /AATree/README.md: -------------------------------------------------------------------------------- 1 | #AA树 2 | AA树,又叫做BB-树,是B-树的一种简单的实现方法。它是带有附加条件的红黑树,相比红黑树降低了保持平衡的难度。 3 | 4 | ###附加条件: 5 | 1. 只有右孩子可以是红孩子 6 | 2. 不再存储颜色位,改成储存level层次,树叶的层次是1,红孩子和父亲层次相同,黑孩子层次比父亲低1 7 | 8 | ###实现: 9 | 保持层次结构,引入了两个新的操作: 10 | 1. Skew 11 | 2. Split 12 | 实际上是带有判断level是否符合要求的旋转。 -------------------------------------------------------------------------------- /AATree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AATree/test.cpp -------------------------------------------------------------------------------- /AVLTree/AVLTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AVLTree/AVLTree.cpp -------------------------------------------------------------------------------- /AVLTree/AVLTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AVLTree/AVLTree.h -------------------------------------------------------------------------------- /AVLTree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AVLTree/test.cpp -------------------------------------------------------------------------------- /AdjacencyList(Version2)/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/AdjacencyList.cpp -------------------------------------------------------------------------------- /AdjacencyList(Version2)/AdjacencyList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/AdjacencyList.h -------------------------------------------------------------------------------- /AdjacencyList(Version2)/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /AdjacencyList(Version2)/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/HashTableOpenAdd.h -------------------------------------------------------------------------------- /AdjacencyList(Version2)/README.md: -------------------------------------------------------------------------------- 1 | #version 2 2 | 本版本实现的邻接表是基于开放定址法的哈希表,此次实现应该基本与标准方法相同: 3 | 4 | 1. 哈希表开辟的空间为顶点V,使用线性探测法解决冲突(缺点是插入最后几个顶点时效率比较低,插入边效率同样低下,为O(V)) 5 | 2. 在实现入度表时,直接使用int数组,将复杂度降为了O(V) 6 | 3. 进行入度统计时,算法复杂度时O(V+E) 7 | 4. 拓扑排序复杂度同样为O(V+E) 8 | 9 | 10 | 本次修改之前,仔细考虑了如何降低插入顶点与边的开销,最后发现如果顶点名字是字符串的话,没法降低。只有把顶点定义为int,直接对顶点结构体数组进行寻址才可以做到。所以插入顶点的与边的开销不能算实现的缺陷。 -------------------------------------------------------------------------------- /AdjacencyList(Version2)/TopSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/TopSort.cpp -------------------------------------------------------------------------------- /AdjacencyList(Version2)/queue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/queue.cpp -------------------------------------------------------------------------------- /AdjacencyList(Version2)/queue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/queue.h -------------------------------------------------------------------------------- /AdjacencyList(Version2)/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList(Version2)/test.cpp -------------------------------------------------------------------------------- /AdjacencyList/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/AdjacencyList.cpp -------------------------------------------------------------------------------- /AdjacencyList/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /AdjacencyList/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/HashTableOpenAdd.h -------------------------------------------------------------------------------- /AdjacencyList/README.md: -------------------------------------------------------------------------------- 1 | #version 1 2 | 本版本实现的邻接表是基于开放定址法的哈希表,实现之处与标准方法略有不同: 3 | 4 | 1. 哈希表开辟的空间为顶点个数的4倍,并且使用平方探测法解决冲突 5 | 2. 在实现入度表时,并非直接使用int数组,而是使用了记录顶点序号和入度的结构体数组 6 | 3. 进行入度统计时,算法复杂度时O(V\*E),原因是哈希表空间多余顶点数,顶点序号不连续 7 | 4. 拓扑排序复杂度同样为O(V*E),原因同上。 8 | 9 | 10 | 本书《数据结构与算法分析》对于邻接表只进行了简单的描述,结果第一次尝试实现的时候犯了这个错误,已经在第二版中修正。 11 | 12 | 注:第二版本提取出了拓扑排序的实现文件,在TopSort.cpp之中 13 | 14 | 按:原谅我忘记添加邻接表的头文件了,已经被删除找不到了,因为第一遍实现速度有缺陷,已经被我删了。。 -------------------------------------------------------------------------------- /AdjacencyList/queue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/queue.cpp -------------------------------------------------------------------------------- /AdjacencyList/queue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/queue.h -------------------------------------------------------------------------------- /AdjacencyList/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/AdjacencyList/test.cpp -------------------------------------------------------------------------------- /BinaryHeap/binaryHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/BinaryHeap/binaryHeap.cpp -------------------------------------------------------------------------------- /BinaryHeap/binaryHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/BinaryHeap/binaryHeap.h -------------------------------------------------------------------------------- /BinaryHeap/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/BinaryHeap/test.cpp -------------------------------------------------------------------------------- /DisjSet/DisjSet.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/DisjSet/DisjSet.cpp -------------------------------------------------------------------------------- /DisjSet/DisjSet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/DisjSet/DisjSet.h -------------------------------------------------------------------------------- /DisjSet/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/DisjSet/test.cpp -------------------------------------------------------------------------------- /HashTable(Open Addressing Hashing)/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(Open Addressing Hashing)/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /HashTable(Open Addressing Hashing)/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(Open Addressing Hashing)/HashTableOpenAdd.h -------------------------------------------------------------------------------- /HashTable(Open Addressing Hashing)/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(Open Addressing Hashing)/test.cpp -------------------------------------------------------------------------------- /HashTable(separate chaining)/HashTable.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(separate chaining)/HashTable.cpp -------------------------------------------------------------------------------- /HashTable(separate chaining)/HashTable.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(separate chaining)/HashTable.h -------------------------------------------------------------------------------- /HashTable(separate chaining)/README.md: -------------------------------------------------------------------------------- 1 | #不同之处 2 | 在哈希表中,我所使用的链表不同于我之前上传的链表文件。这里使用的链表是不带头结点的实现(最开始忘了这点,查bug查的我头疼)。原先的链表实现带有头结点。 3 | -------------------------------------------------------------------------------- /HashTable(separate chaining)/list.cpp: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | 3 | int isEmpty(List L) 4 | { 5 | return L->Next ==NULL; 6 | } 7 | 8 | int isLast(Position P) 9 | { 10 | return P->Next ==NULL; 11 | } 12 | 13 | Position find(ElementType X, List L) 14 | { 15 | if(L == NULL) 16 | return NULL; 17 | Position P = L; 18 | while(P !=NULL && P->Element !=X) 19 | P = P->Next; 20 | 21 | return P; 22 | } 23 | 24 | void Delete(ElementType X, List L) 25 | { 26 | Position P, temp; 27 | P = findPrevious(X, L); 28 | 29 | if(!isLast(P)) 30 | { 31 | temp = P->Next; 32 | 33 | P->Next = temp->Next; 34 | delete P; 35 | } 36 | } 37 | 38 | Position findPrevious(ElementType X, List L) 39 | { 40 | Position P = L; 41 | 42 | while(P->Next != NULL && P->Next->Element != X) 43 | P = P->Next; 44 | return P; 45 | } 46 | 47 | void insert(ElementType X, Position P) 48 | { 49 | Position temp; 50 | temp = (Position)malloc(sizeof(struct Node)); 51 | if(temp !=NULL) 52 | { 53 | temp->Element = X; 54 | temp->Next = P->Next; 55 | P->Next = temp; 56 | } 57 | perror("Not enough memory"); 58 | } 59 | 60 | void deleteList(List L) 61 | { 62 | Position P = L->Next; 63 | Position temp; 64 | L->Next = NULL; 65 | 66 | while(P !=NULL) 67 | { 68 | temp = P->Next; 69 | free (P); 70 | P = temp; 71 | } 72 | } 73 | 74 | List makeEmpty(List L) 75 | { 76 | if(L == NULL) 77 | return L; 78 | 79 | List temp; 80 | while(L != NULL) 81 | { 82 | temp = L; 83 | L = L->Next; 84 | free(temp); 85 | } 86 | return NULL; 87 | } 88 | -------------------------------------------------------------------------------- /HashTable(separate chaining)/list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef _list_h 3 | #define _list_h 4 | 5 | 6 | struct Node; 7 | typedef int ElementType; 8 | typedef struct Node * PtrToNode; 9 | typedef PtrToNode List; 10 | typedef PtrToNode Position; 11 | 12 | List makeEmpty(List L); 13 | int isEmpty(List L); 14 | int isLast(Position P, List L); 15 | Position find(ElementType X, List L); 16 | void Delete(ElementType X, List L); 17 | Position findPrevious(ElementType X, List L); 18 | void insert(ElementType X, Position P, List L); 19 | void deleteList(List L); 20 | Position Header(List L); 21 | Position First(List L); 22 | Position Advance(List L); 23 | ElementType retrieve(Position P); 24 | 25 | 26 | 27 | 28 | 29 | struct Node 30 | { 31 | ElementType Element; 32 | struct Node * Next; 33 | }; 34 | 35 | 36 | #endif -------------------------------------------------------------------------------- /HashTable(separate chaining)/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/HashTable(separate chaining)/test.cpp -------------------------------------------------------------------------------- /KdTree/2-dTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/KdTree/2-dTree.cpp -------------------------------------------------------------------------------- /KdTree/2-dTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/KdTree/2-dTree.h -------------------------------------------------------------------------------- /KdTree/README.md: -------------------------------------------------------------------------------- 1 | #Kd树 2 | Kd树是K个数据组合在一个节点之中,可以实现对K种数据的范围进行搜索。 3 | 4 | ###2d树: 5 | 1. 这里实现的是2d树,即两个数据组成一个节点的数据成员 6 | 2. 查找二叉树的实现方式是,奇数层分支按照第一个关键字查找 7 | 3. 偶数层分支按照第二个关键字查找 8 | 9 | ###实现: 10 | 结构体中保存数据数组,奇偶层的判断通过一个标志位反复取反。 -------------------------------------------------------------------------------- /KdTree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/KdTree/test.cpp -------------------------------------------------------------------------------- /LeftistHeap/LeftistHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/LeftistHeap/LeftistHeap.cpp -------------------------------------------------------------------------------- /LeftistHeap/LeftistHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/LeftistHeap/LeftistHeap.h -------------------------------------------------------------------------------- /LeftistHeap/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/LeftistHeap/test.cpp -------------------------------------------------------------------------------- /MaxNetStream/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/AdjacencyList.cpp -------------------------------------------------------------------------------- /MaxNetStream/AdjacencyList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/AdjacencyList.h -------------------------------------------------------------------------------- /MaxNetStream/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /MaxNetStream/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/HashTableOpenAdd.h -------------------------------------------------------------------------------- /MaxNetStream/MaxNetStream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/MaxNetStream.cpp -------------------------------------------------------------------------------- /MaxNetStream/README.md: -------------------------------------------------------------------------------- 1 | #最大网络流问题 2 | 在给定边容量的有向图中,找到从发点(source)到收点(sink)之间可以存在的最大网络流。 3 | 4 | ###算法简介: 5 | 使用Edmond-Karp算法: 6 | 7 | 1. 建立一个空的Gf图,作为流图 8 | 2. 建立一个残余图Gr 9 | 3. 在残余图上使用BFS算法寻找增广路径,如果找到,则使用该路径上的最小流值修改Gr与Gf图。 10 | 4. 重复过程3,直到无法找出能从发点到收点的增广路径。此次流图Gf为所求。 11 | 12 | ###实现: 13 | 使用邻接表表示图,边的权值表示路径的流量。略微修改了最短无权路径算法,作为求增广路径的算法。最后的输出是直接输出了流图Gf的各个边,需要自己把边节点连起来才能看明白。 14 | -------------------------------------------------------------------------------- /MaxNetStream/queue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/queue.cpp -------------------------------------------------------------------------------- /MaxNetStream/queue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/queue.h -------------------------------------------------------------------------------- /MaxNetStream/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MaxNetStream/test.cpp -------------------------------------------------------------------------------- /MinTree/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/AdjacencyList.cpp -------------------------------------------------------------------------------- /MinTree/AdjacencyList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/AdjacencyList.h -------------------------------------------------------------------------------- /MinTree/DisjSet.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/DisjSet.cpp -------------------------------------------------------------------------------- /MinTree/DisjSet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/DisjSet.h -------------------------------------------------------------------------------- /MinTree/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /MinTree/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/HashTableOpenAdd.h -------------------------------------------------------------------------------- /MinTree/MinTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/MinTree.cpp -------------------------------------------------------------------------------- /MinTree/README.md: -------------------------------------------------------------------------------- 1 | #最小生成树 2 | 给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边连通,并且树上权的总和为最小,这个问题就是最小生成树问题. 3 | 4 | 1. Prim算法 5 | 2. Kruskal算法 6 | 7 | Prim算法: 8 | 9 | 1. 从指定的顶点开始,将该顶点设为已知 10 | 2. 更新所有与它相连的顶点到它的距离(比原来到已知集合的另一个点的距离短则更新) 11 | 3. 在所有未知点中,找到一个到已知顶点的距离最短的未知顶点,然后重复过程2,3,直到未知顶点为0 12 | 13 | 该算法的实现方式与Dijkstra算法非常相似,在原有代码上稍微修改即可。 14 | 15 | Kruskal算法: 16 | 17 | 1. 所有边的权值生成一个二叉堆 18 | 2. 每次找到一条最小权值的边 19 | 3. 如果边的两个顶点不属于同一个集合,那么把它们加入同一个集合 20 | 4. 重复过程2,3,直到所有顶点都属于同一个集合 21 | 22 | 实现这个算法的时候,生成的二叉堆需要保存边两个顶点的信息以及边权值信息,在之前实现Dijkstra算法时使用的二叉堆结构体只需要添加一个成员即可,之前的赋值是结构体赋值,不需要修改,直接兼容。 23 | 实现判断两个顶点是否在同一个集合,加入两个顶点到同一个集合,可以使用不相交集来完成。这个数据结构非常简单,实现也高效。 -------------------------------------------------------------------------------- /MinTree/binaryHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/binaryHeap.cpp -------------------------------------------------------------------------------- /MinTree/binaryHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/binaryHeap.h -------------------------------------------------------------------------------- /MinTree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/MinTree/test.cpp -------------------------------------------------------------------------------- /PairHeap/PairingHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/PairHeap/PairingHeap.cpp -------------------------------------------------------------------------------- /PairHeap/PairingHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/PairHeap/PairingHeap.h -------------------------------------------------------------------------------- /PairHeap/README.md: -------------------------------------------------------------------------------- 1 | #配对堆 2 | 配对堆是最实用的斐波那契堆的变种,实现合并最方便 3 | 4 | ###配对堆实现: 5 | 1. 使用了树结构 6 | 2. 使用左孩子和兄弟指针,兄弟指针位于同一层 7 | 3. 使用了前向指针,意义不代表父亲 8 | 9 | ###实现: 10 | 实现与二项队列比较相似,具体实现不难,请见代码 -------------------------------------------------------------------------------- /PairHeap/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/PairHeap/test.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | d# Data-Structure 2 | ##代码内容 3 | 《数据结构与算法分析》上的代码实现, 4 | 按照该书的章节顺序,主要实现书上给出的例子。 5 | 6 | ##已经实现: 7 | ###第三章 8 | - 链表 9 | - 栈 10 | - 队列 11 | ###第四章 12 | - 排序二叉树 13 | - 平衡二叉树 14 | - 伸展树(自底向上) 15 | - 伸展树(自顶往下) 16 | ###第五章 17 | - 哈希表(分离链接法) 18 | - 哈希表(开放定址法) 19 | ###第六章 20 | - 二叉堆 21 | - 左式堆 22 | - 二项队列 23 | ###第七章 24 | - 插入排序 25 | - 希尔排序 26 | - 堆排序 27 | - 归并排序 28 | - 快速排序 29 | ###第八章 30 | -不相交集 31 | ###第九章 32 | -邻接表(Versioni 1,2) 33 | -拓扑排序(Versioni 1,2) 34 | -单源最短路径算法 35 | -最大网络流 36 | -最小生成树 37 | -深度优先搜索 38 | -双连通性 39 | -欧拉回路 40 | ###第十章 41 | -分治算法:最近点问题 42 | -动态规划:斐波那契数列,递归关系,矩阵乘法顺序,最优搜索二叉树 43 | -随机化算法:跳表 44 | -回溯法:收费公路重建,三连棋游戏(带AI) 45 | ###第十二章 46 | -红黑树:自顶向下插入,自顶向下删除 47 | -AA树 48 | -Treap树 49 | -Kd树 50 | -配对堆 51 | ##文件内容 52 | 其中链表,栈,队列只给出了.h与.cpp文件 53 | 其他所有实现均给出了.h .cpp文件以及测试的test.cpp代码 54 | 编程平台是Vitual Studio 2010,使用c语言编写(其中有少部分使用std::cout输出)。 55 | 56 | 57 | ##我的博客 58 | 在我的博客中写下了这些数据结构的介绍,以及实现过程中遇到的问题,欢迎大家来我的博客: 59 | http://blog.csdn.net/yw8355507 60 | 61 | ##完成! 62 | 耗时两个月,每天晚上5个小时。终于把《数据结构与算法分析》一书中,几乎所有的数据结构,以及它所提及的算法全部实现!(除了少数特别简单的未实现) 63 | 64 | 感慨万千,亲手实现数据结构与算法的过程,让我学习到了太多太多的东西: 65 | **1.** **递归:** 66 | 以前编码的过程中,对于递归与非递归的转化是非常的不熟练的。现在无论怎么切换,头脑中都会有一个分层的模型可以模拟出整个的处理过程,并且现在可以非常熟练的使用递归来化简编码。 67 | 68 | **2.** **时间空间分析:** 69 | 现在,我已经可以非常轻松的来评估一个算法的时间复杂度,空间复杂度。对于递归过程也能确定是否需要使用动态规划来处理。对于整个程序的运行时间已经可以轻松估计了。 70 | 71 | **3.** **数据结构与算法选择:** 72 | 不夸张的说,现在看见网上的算法练习题,或者是公司的笔试题,第一遍就能立刻找到解法,然后可以进一步的通过选择合理的数据结构与算法,来尝试降低算法的复杂度。特别是对于hash表,优先队列,排序等等问题,似乎已经没有多少难度。 73 | 74 | **4.** **攻克难题的信心:** 75 | 在实现这本书上的算法时,有许多算法书上基本就是顺带一提,或者一个很难的算法随意讲一讲。我总是尝试通过各种方法来把这个算法实现。 76 | 1. 首先是博客,书上讲的不清楚,别人的博客里总有讲的清楚的吧,最典型的例子就是伸展树的学习。书上大致一提,然后通过别人的博客,彻底学明白了,然后实现了出来。 77 | 2. 其次是自行尝试优化:图论中,寻找欧拉回路,要达到算法复杂度O(E+V),最初在网上博客中寻找,别人的博客中,要不是达不到这个复杂度,要不就是说的和书上一样含糊,而且没有实现的代码。一不做二不休,先写出可以用的再来优化,结果写完第一遍之后,就明白了该如何优化,书上即使不给我指导,我自己也能找出来,最后一共修改了3次代码,最后达到了书上所说的复杂度。 78 | 3. 最后是能去寻找英文的文献来解决自己遇到的问题。在实现自顶向下的红黑树时,翻遍了csdn的博客,找来找去都没有一个博客是使用自顶向下的方式实现红黑树的删除的,即使有,也不过是把《数据结构与算法分析》书中简单的几句话再抄一遍。最终还是通过了一篇英文的文献,找到了实现的方法。但是既没有流程图,也没有伪代码,自己一步一步把流程图与伪代码写出来,然后总算是完成了自顶向下的删除。并且还通过Jully的博客,学习了自底向上的红黑树实现方法,对比了他们之间的优势与劣势。 79 | 80 | **4.** **调试的能力与耐心:** 81 | 说实话,二叉树这种带有指针的东西,特别是有父亲节点的时候,是最不好调试的。而且,在编码过程中,我犯过的错误不仅仅是逻辑错误,还有许多编码的错误,比如==写成=,这些细节都是编译器发现不了的,逐行阅读代码,也是自己没法注意到的。调试的过程,自己一步步加断点,打印,一步步缩小范围。找到出错的函数后,跟着代码一步步实现,最终都找出了bug。没有放弃一个代码,全部都完成了debug。 82 | 83 | ##下一步计划: 84 | 现在书单中还有《现代操作系统》,《深入理解计算机系统》,《APUE》,《Effective C++》《STL源码解析》等等书没有读完,最近事情也比较多,打算先看看操作系统,其他的书挑出重点来仔细读读,不再像数据结构这本书这样特别细的读了。再就是算法实习生这边,打算有时间再读读Python与机器学习的一些算法,CSS与HTML入个门,做一做小demo,让自己对前端的东西也能上手写一写。 -------------------------------------------------------------------------------- /RedBlackTree/README.md: -------------------------------------------------------------------------------- 1 | #红黑树 2 | AVL树的一个变种,不追求严格的平衡,插入的效率更加高。 3 | 4 | ###实现方案: 5 | 1. 自顶向下插入 6 | 2. 自顶向下删除 7 | 8 | ###实现: 9 | 《数据结构与算法分析》一书中只给出了自顶向下插入的详细解析。自顶向下删除只略微提及,自行研究完成了自顶向下删除。 10 | 1. 网络上给出的删除算法基本都是自顶向上的 11 | 2. 根据一篇英文文献所描述的方案 12 | 3. 将方案先变成了流程图 13 | 4. 再将流程图变成伪代码 14 | 5. 最后完成了编码测试 -------------------------------------------------------------------------------- /RedBlackTree/RedBlackTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/RedBlackTree/RedBlackTree.cpp -------------------------------------------------------------------------------- /RedBlackTree/RedBlackTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/RedBlackTree/RedBlackTree.h -------------------------------------------------------------------------------- /RedBlackTree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/RedBlackTree/test.cpp -------------------------------------------------------------------------------- /SearchTree/searchTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SearchTree/searchTree.cpp -------------------------------------------------------------------------------- /SearchTree/searchTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SearchTree/searchTree.h -------------------------------------------------------------------------------- /SearchTree/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SearchTree/test.cpp -------------------------------------------------------------------------------- /ShortestPath/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/AdjacencyList.cpp -------------------------------------------------------------------------------- /ShortestPath/AdjacencyList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/AdjacencyList.h -------------------------------------------------------------------------------- /ShortestPath/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /ShortestPath/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/HashTableOpenAdd.h -------------------------------------------------------------------------------- /ShortestPath/README.md: -------------------------------------------------------------------------------- 1 | #单源最短路径问题 2 | 1. 无权最短路径 3 | 2. Dijktra算法 4 | 5 | 问题1和问题2使用同一张图,图在输入的时候是有权的,不过在进行无权最短路径计算时默认路径无权。 6 | 7 | 问题1使用了队列,队列在第三章中实现,拿来直接使用。 8 | 9 | 问题2使用了优先队列,对队列的元素进行了修改,改成了带有图顶点序号和路径开销的结构体。 10 | 11 | 注:测试代码中含有拓扑排序测试代码,如果想要编译运行,需要删除拓扑排序内容,或者从我另一个文件夹中提取出拓扑排序实现文件添加进去。 -------------------------------------------------------------------------------- /ShortestPath/ShortestPath.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/ShortestPath.cpp -------------------------------------------------------------------------------- /ShortestPath/binaryHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/binaryHeap.cpp -------------------------------------------------------------------------------- /ShortestPath/binaryHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/binaryHeap.h -------------------------------------------------------------------------------- /ShortestPath/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/ShortestPath/test.cpp -------------------------------------------------------------------------------- /Sort/binaryHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Sort/binaryHeap.cpp -------------------------------------------------------------------------------- /Sort/binaryHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Sort/binaryHeap.h -------------------------------------------------------------------------------- /Sort/sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Sort/sort.cpp -------------------------------------------------------------------------------- /Sort/sort.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Sort/sort.h -------------------------------------------------------------------------------- /Sort/test.cpp: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | #include 3 | #include 4 | 5 | #define random(x) (rand()%x) 6 | 7 | 8 | int main() 9 | { 10 | int randData[6][5000]; 11 | int i,j; 12 | for(i=0; i<6; i++) 13 | for(j=0; j<5000; j++) 14 | randData[i][j] = random(5000); 15 | 16 | 17 | InsertionSort(randData[0], 5000); 18 | 19 | ShellSort(randData[1], 5000); 20 | 21 | HeapSort(randData[2], 5000); 22 | 23 | MergeSort(randData[3], 5000); 24 | 25 | QuickSort(randData[4], 5000); 26 | 27 | QuickSelect(randData[5], 10, 5000); 28 | } -------------------------------------------------------------------------------- /SplayTree(Bottom Up)/SplayTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Bottom Up)/SplayTree.cpp -------------------------------------------------------------------------------- /SplayTree(Bottom Up)/SplayTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Bottom Up)/SplayTree.h -------------------------------------------------------------------------------- /SplayTree(Bottom Up)/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Bottom Up)/test.cpp -------------------------------------------------------------------------------- /SplayTree(Up Down)/SplayTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Up Down)/SplayTree.cpp -------------------------------------------------------------------------------- /SplayTree(Up Down)/SplayTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Up Down)/SplayTree.h -------------------------------------------------------------------------------- /SplayTree(Up Down)/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/SplayTree(Up Down)/test.cpp -------------------------------------------------------------------------------- /Treap/README.md: -------------------------------------------------------------------------------- 1 | #Treap树 2 | Treap树,使用随机数对任意的输入给出O(LogN)的期望时间性能 3 | 4 | ###新的方法: 5 | 1. 添加了优先级成员 6 | 2. 优先级越低,节点深度越低 7 | 8 | ###实现: 9 | 只需要使用递归的方式加上旋转,可以轻松实现优先级顺序,优先级的赋予通过随机数。 -------------------------------------------------------------------------------- /Treap/Treap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Treap/Treap.cpp -------------------------------------------------------------------------------- /Treap/Treap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Treap/Treap.h -------------------------------------------------------------------------------- /Treap/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/Treap/test.cpp -------------------------------------------------------------------------------- /binomialQueue/binomialQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/binomialQueue/binomialQueue.cpp -------------------------------------------------------------------------------- /binomialQueue/binomialQueue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/binomialQueue/binomialQueue.h -------------------------------------------------------------------------------- /binomialQueue/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/binomialQueue/test.cpp -------------------------------------------------------------------------------- /deepFirstSearch/AdjacencyList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/AdjacencyList.cpp -------------------------------------------------------------------------------- /deepFirstSearch/AdjacencyList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/AdjacencyList.h -------------------------------------------------------------------------------- /deepFirstSearch/HashTableOpenAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/HashTableOpenAdd.cpp -------------------------------------------------------------------------------- /deepFirstSearch/HashTableOpenAdd.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/HashTableOpenAdd.h -------------------------------------------------------------------------------- /deepFirstSearch/README.md: -------------------------------------------------------------------------------- 1 | #深度优先搜索 2 | 深度优先搜索是先序遍历的一种推广,从某个顶点开始,递归的遍历所有与该顶点相邻的顶点。 3 | 4 | 5 | ###算法扩展: 6 | 7 | 1. 无向图中,求取深度优先生成树 8 | 2. 测试无向图的双连通性,寻找割点 9 | 3. 求解欧拉回路的环游路径 10 | 4. 查找强分支 11 | 12 | ###实现: 13 | 在这里实现了前三个算法,第四个算法感觉难度不大,暂且略过 14 | 15 | 1. 使用之前Dijkstra算法中使用的遍历路径表来保存深度优先搜索的搜索顺序信息。 16 | 17 | 2. 这一部分还是利用深度优先生成树来求解,一个顶点是否是割点,判断的标准是它的儿子的Low(w)是否大于它的排序Num(v)。具体实现是构造了一个新的结构体,其中保存known, parent, Num, Low四个数据。使用递归的方式求解。 18 | 3. 求解环游路径需要先判断一张图是否是欧拉图,无向图需要每个顶点的度为偶数。有向图需要每个顶点的入度等于出度。这里不进行判断,直接求解。使用链表保存经过的每个顶点,每经过一条边就移除该边,直到所有的边都被移除。 19 | -------------------------------------------------------------------------------- /deepFirstSearch/deepFirst.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/deepFirst.cpp -------------------------------------------------------------------------------- /deepFirstSearch/list.cpp: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | 3 | int isEmpty(List L) 4 | { 5 | return L ==NULL; 6 | } 7 | 8 | int isLast(List P) 9 | { 10 | return P->Next ==NULL; 11 | } 12 | 13 | List find(ElementType X, List L) 14 | { 15 | if(L == NULL) 16 | return NULL; 17 | List P = L; 18 | while(P !=NULL && P->Element !=X) 19 | P = P->Next; 20 | 21 | return P; 22 | } 23 | 24 | void Delete(ElementType X, List L) 25 | { 26 | List P, temp; 27 | P = findPrevious(X, L); 28 | 29 | if(!isLast(P)) 30 | { 31 | temp = P->Next; 32 | 33 | P->Next = temp->Next; 34 | delete temp; 35 | } 36 | } 37 | 38 | List findPrevious(ElementType X, List L) 39 | { 40 | List P = L; 41 | 42 | while(P->Next != NULL && P->Next->Element != X) 43 | P = P->Next; 44 | return P; 45 | } 46 | 47 | 48 | 49 | void insert(ElementType X, List P) 50 | { 51 | List temp; 52 | temp = (List)malloc(sizeof(struct ListNode)); 53 | if(temp !=NULL) 54 | { 55 | temp->Element = X; 56 | temp->Next = P->Next; 57 | P->Next = temp; 58 | } 59 | else 60 | perror("Not enough memory"); 61 | } 62 | 63 | void deleteList(List L) 64 | { 65 | List P = L; 66 | List temp; 67 | 68 | 69 | while(P !=NULL) 70 | { 71 | temp = P->Next; 72 | free (P); 73 | P = temp; 74 | } 75 | } 76 | 77 | List makeEmpty(List L) 78 | { 79 | if(L == NULL) 80 | return L; 81 | 82 | List temp; 83 | while(L != NULL) 84 | { 85 | temp = L; 86 | L = L->Next; 87 | free(temp); 88 | } 89 | return NULL; 90 | } 91 | -------------------------------------------------------------------------------- /deepFirstSearch/list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef _list_h 3 | #define _list_h 4 | 5 | 6 | struct ListNode; 7 | typedef int ElementType; 8 | typedef struct ListNode * PtrToNode; 9 | typedef PtrToNode List; 10 | struct ListHead; 11 | 12 | List makeEmpty(List L); 13 | int isEmpty(List L); 14 | int isLast(List P, List L); 15 | List find(ElementType X, List L); 16 | void Delete(ElementType X, List L); 17 | List findPrevious(ElementType X, List L); 18 | 19 | void insert(ElementType X, List P); 20 | 21 | 22 | void deleteList(List L); 23 | List Header(List L); 24 | List First(List L); 25 | List Advance(List L); 26 | ElementType retrieve(List P); 27 | 28 | 29 | struct ListNode 30 | { 31 | ElementType Element; 32 | struct ListNode * Next; 33 | }; 34 | 35 | 36 | 37 | #endif -------------------------------------------------------------------------------- /deepFirstSearch/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/deepFirstSearch/test.cpp -------------------------------------------------------------------------------- /dynamicProgramming/README.md: -------------------------------------------------------------------------------- 1 | #动态规划 2 | 当递归算法得到的程序时低效的时候,把递归算法改写为非递归算法能得到显著的性能提升,递归算法过程中使用的子问题的答案,会被记录在一个表内。这种方法就叫做动态规划。 3 | 4 | ###算法问题: 5 | 使用分治法求解: 6 | 1. 斐波那契数列计算 7 | 2. 递归关系求解 8 | 3. 矩阵乘法顺序安排 9 | 4. 最优查找二叉树 10 | 11 | 12 | ###实现: 13 | 问题1与2的思路是相同的,用几个变量记录下求解下一个变量所需要的必要的成员,每计算出一个新的变量,记录变量做出相应的改变 14 | 问题3,4之间也是类似的,一个大小为N的问题,可以拆分成子问题的和,要从子问题的和中选出一个最优的组合,那么,把所有的子问题的最优结果都记录下来,就可以解决这个问题了。这里记录的方式是使用一个N*N的矩阵。 15 | -------------------------------------------------------------------------------- /dynamicProgramming/dynamicPro.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/dynamicProgramming/dynamicPro.cpp -------------------------------------------------------------------------------- /dynamicProgramming/dynamicPro.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/dynamicProgramming/dynamicPro.h -------------------------------------------------------------------------------- /dynamicProgramming/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/dynamicProgramming/test.cpp -------------------------------------------------------------------------------- /list/list.cpp: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | 3 | int isEmpty(List L) 4 | { 5 | return L->Next ==NULL; 6 | } 7 | 8 | int isLast(Position P) 9 | { 10 | return P->Next ==NULL; 11 | } 12 | 13 | Position find(ElementType X, List L) 14 | { 15 | if(L == NULL) 16 | return NULL; 17 | Position P = L->Next; 18 | while(P !=NULL && P->Element !=X) 19 | P = P->Next; 20 | 21 | return P; 22 | } 23 | 24 | void Delete(ElementType X, List L) 25 | { 26 | Position P, temp; 27 | P = findPrevious(X, L); 28 | 29 | if(!isLast(P)) 30 | { 31 | temp = P->Next; 32 | 33 | P->Next = temp->Next; 34 | delete P; 35 | } 36 | } 37 | 38 | Position findPrevious(ElementType X, List L) 39 | { 40 | Position P = L->Next; 41 | 42 | while(P->Next != NULL && P->Next->Element != X) 43 | P = P->Next; 44 | return P; 45 | } 46 | 47 | void insert(ElementType X, Position P) 48 | { 49 | Position temp; 50 | temp = new struct Node; 51 | if(temp !=NULL) 52 | { 53 | temp->Element = X; 54 | temp->Next = P->Next; 55 | P->Next = temp; 56 | } 57 | perror("Not enough memory"); 58 | } 59 | 60 | void deleteList(List L) 61 | { 62 | Position P = L->Next; 63 | Position temp; 64 | L->Next = NULL; 65 | 66 | while(P !=NULL) 67 | { 68 | temp = P->Next; 69 | delete P; 70 | P = temp; 71 | } 72 | } 73 | 74 | List makeEmpty(List L) 75 | { 76 | if(L == NULL) 77 | return L; 78 | 79 | List temp; 80 | while(L != NULL) 81 | { 82 | temp = L; 83 | L = L->Next; 84 | free(temp); 85 | } 86 | return NULL; 87 | } 88 | -------------------------------------------------------------------------------- /list/list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef _list_h 3 | #define _list_h 4 | 5 | 6 | struct Node; 7 | typedef int ElementType; 8 | typedef struct Node * PtrToNode; 9 | typedef PtrToNode List; 10 | typedef PtrToNode Position; 11 | 12 | List makeEmpty(List L); 13 | int isEmpty(List L); 14 | int isLast(Position P, List L); 15 | Position find(ElementType X, List L); 16 | void Delete(ElementType X, List L); 17 | Position findPrevious(ElementType X, List L); 18 | void insert(ElementType X, Position P, List L); 19 | void deleteList(List L); 20 | Position Header(List L); 21 | Position First(List L); 22 | Position Advance(List L); 23 | ElementType retrieve(Position P); 24 | 25 | 26 | 27 | 28 | 29 | struct Node 30 | { 31 | ElementType Element; 32 | struct Node * Next; 33 | }; 34 | 35 | 36 | #endif -------------------------------------------------------------------------------- /nearPoint/README.md: -------------------------------------------------------------------------------- 1 | #最近点问题 2 | 在平面上有点列P,p1=(x1,y1) p2=(x2,y2),求这些点中最近的两个点的距离 3 | 4 | ###算法简介: 5 | 使用分治法求解: 6 | 1. 将所有的点按照X坐标排序,放入数组X 7 | 2. 将按X排序的点再按Y坐标排序,放入数组Y 8 | 3. 将X数组从中间分开,递归查找左边最小距离,右边最小距离以及跨左右两边的最小距离 9 | 10 | 11 | ###实现: 12 | 排序使用并归排序,递归之外的工作为O(N),因此总的复杂度保持在O(NlogN)。具体实现参考代码。 13 | -------------------------------------------------------------------------------- /nearPoint/nearPoint.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/nearPoint/nearPoint.cpp -------------------------------------------------------------------------------- /nearPoint/nearPoint.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/nearPoint/nearPoint.h -------------------------------------------------------------------------------- /nearPoint/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/nearPoint/test.cpp -------------------------------------------------------------------------------- /queue/queue.cpp: -------------------------------------------------------------------------------- 1 | #include "queue.h" 2 | #include 3 | 4 | int isEmpty(Queue Q) 5 | { 6 | return Q->front ==NULL; 7 | } 8 | 9 | Queue createQueue(void) 10 | { 11 | Queue Q; 12 | Q = new QueueRecord; 13 | if(Q ==NULL) 14 | { 15 | perror("Not Enough Memory"); 16 | return NULL; 17 | } 18 | Q->rear = NULL; 19 | Q->front = NULL; 20 | return Q; 21 | } 22 | 23 | void enqueue(ElementType X, Queue Q) 24 | { 25 | struct Node * temp; 26 | temp = (struct Node *)malloc(sizeof(struct Node)); 27 | if(temp ==NULL) 28 | { 29 | perror("Not Enough Memory"); 30 | return; 31 | } 32 | temp->Element = X; 33 | temp->Next = NULL; 34 | if(Q->rear ==NULL) 35 | { 36 | Q->rear = Q->front = temp; 37 | } 38 | else 39 | { 40 | Q->rear->Next = temp; 41 | Q->rear = temp; 42 | } 43 | } 44 | 45 | ElementType dequeue(Queue Q) 46 | { 47 | if(isEmpty(Q)) 48 | { 49 | perror("Queue is empty"); 50 | return 0; 51 | } 52 | ElementType temp; 53 | struct Node *P; 54 | temp = Q->front->Element; 55 | P = Q->front; 56 | Q->front = P->Next; 57 | free(P); 58 | if(Q->front ==NULL) 59 | { 60 | Q->rear = NULL; 61 | } 62 | return temp; 63 | } -------------------------------------------------------------------------------- /queue/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_H 2 | #define _QUEUE_H 3 | 4 | struct QueueRecord; 5 | struct Node; 6 | typedef struct QueueRecord * PtrToNode; 7 | typedef QueueRecord * Queue; 8 | 9 | typedef int ElementType; 10 | 11 | int isEmpty(Queue Q); 12 | Queue createQueue(void); 13 | void disposeQueue(Queue Q); 14 | void makeEmpty(Queue Q); 15 | void enqueue(ElementType X, Queue Q); 16 | ElementType dequeue(Queue Q); 17 | 18 | 19 | struct QueueRecord 20 | { 21 | struct Node * front; 22 | struct Node * rear; 23 | }; 24 | 25 | struct Node 26 | { 27 | ElementType Element; 28 | struct Node * Next; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /roadRebuild/README.md: -------------------------------------------------------------------------------- 1 | #收费公路重建 2 | 问题描述: 3 | 在X轴上给定N个点,再给出N个点之间的每两个点之间的距离,一共有N(N-1)/2对距离。那么现在给出所有的距离,求出在X点上的各个点的位置。 4 | 5 | 解法思路: 6 | 先确定一个基准,x1位于原点,最大距离为M,最大点xN也在M处。第二距离为m,x(N-1)位于m。 7 | 现有: 8 | 0__________________________________________m\_\_\_\_\_\_\_\_\_\_\_\_\_M 9 | X1\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_x(N-1)\_\_\_\_\_\_\_\_\_\_\_xN 10 | 11 | 然后在x1到x(N-1)之间,填入还剩的最大的距离,先放在x(N-2)位置,如果满足其他距离条件,则删除已经满足的距离,继续往前测试。 12 | 如果测试不通过,退回至上一步,放在x2位置,再重复整个测试过程,直到得到解,或者得出无解。 -------------------------------------------------------------------------------- /roadRebuild/roadRebuild.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/roadRebuild/roadRebuild.cpp -------------------------------------------------------------------------------- /roadRebuild/roadRebuild.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/roadRebuild/roadRebuild.h -------------------------------------------------------------------------------- /roadRebuild/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/roadRebuild/test.cpp -------------------------------------------------------------------------------- /skipList/README.md: -------------------------------------------------------------------------------- 1 | #跳跃表 2 | 1. 随机化算法,如何确定节点的级别 3 | 2.每个节点如何存储k个指针 4 | 问题1: 5 | 解决问题1的做法不是通过计数,而是直接通过随机的方式来决定这个节点的级别,从1-K级别,概率依次缩小1/2,这样最后生成的跳跃表级别的出现概率符合要求,也避免了对跳跃表的遍历 6 | 7 | 问题2 : 8 | 这个问题本来一直头疼,看了别人的代码才知道,原来可以用一个结构体指针,指向大于该结构体容量的空间,多出来的空间就是需要的指针的空间。在学习UNIX网络编程时,有看到过这样的使用方法,不过没有想到真的可以这样用。 -------------------------------------------------------------------------------- /skipList/SkipList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/skipList/SkipList.cpp -------------------------------------------------------------------------------- /skipList/SkipList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/skipList/SkipList.h -------------------------------------------------------------------------------- /skipList/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/skipList/test.cpp -------------------------------------------------------------------------------- /stack/stack.cpp: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | 4 | int isEmpty(Stack S) 5 | { 6 | return S->Next == NULL; 7 | } 8 | 9 | Stack createStack(void) 10 | { 11 | Stack S; 12 | S = new Node; 13 | if( S!=NULL) 14 | { 15 | S->Next = NULL; 16 | } 17 | return S; 18 | } 19 | 20 | void disposeStack(Stack S) 21 | { 22 | while(!isEmpty(S)) 23 | pop(S); 24 | } 25 | 26 | void makeEmpty(Stack S) 27 | { 28 | if(S !=NULL) 29 | { 30 | while(!isEmpty(S)) 31 | pop(S); 32 | } 33 | } 34 | 35 | void push(ElementType X, Stack S) 36 | { 37 | PtrToNode temp; 38 | temp = new Node; 39 | if(temp ==NULL) 40 | { 41 | perror("Not Enough memory"); 42 | return; 43 | } 44 | temp ->Element = X; 45 | temp->Next= S->Next; 46 | S->Next = temp; 47 | } 48 | 49 | ElementType pop(Stack S) 50 | { 51 | if(isEmpty(S)) 52 | { 53 | perror("Stack is empty"); 54 | return 0; 55 | } 56 | ElementType temp; 57 | PtrToNode P; 58 | P = S->Next; 59 | temp = P->Element; 60 | S->Next = P->Next; 61 | delete P; 62 | return temp; 63 | } -------------------------------------------------------------------------------- /stack/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _STACK_H 2 | #define _STACK_H 3 | 4 | struct Node; 5 | typedef Node * PtrToNode; 6 | typedef PtrToNode Stack; 7 | typedef int ElementType; 8 | 9 | 10 | 11 | int isEmpty(Stack S); 12 | Stack createStack(void); 13 | void disposeStack(Stack S); 14 | void makeEmpty(Stack S); 15 | void push(ElementType X, Stack S); 16 | ElementType pop(Stack S); 17 | 18 | struct Node 19 | { 20 | ElementType Element; 21 | PtrToNode Next; 22 | }; 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /tic_tac_toe/README.md: -------------------------------------------------------------------------------- 1 | #三连棋(tic tac toe) 2 | 1. 人机对战 3 | 2. 任意顺序 4 | 3. alpha-beta枝减 5 | 6 | 本游戏的AI算法来自《数据结构与算法分析》书上10.5节回溯算法,书上提供了寻找下一步的伪代码,实现了伪代码之后将程序包装成了一个小游戏。 7 | tic_tac_toe.cpp 中有注释掉的未使用alpha-beta枝减的AI代码,之后根据alpha-beta枝减优化了程序。 8 | 电脑先走时,产生了一个随机数,从4个角和中点之中随机选择一点作为第一步。 -------------------------------------------------------------------------------- /tic_tac_toe/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/tic_tac_toe/main.cpp -------------------------------------------------------------------------------- /tic_tac_toe/tic_tac_toe.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/tic_tac_toe/tic_tac_toe.h -------------------------------------------------------------------------------- /tic_tac_toe/tie_tac_toe.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinWenAtBIT/Data-Structure/d44e100d29b03ae1ee501886799ec35dae16413c/tic_tac_toe/tie_tac_toe.cpp --------------------------------------------------------------------------------