Skip to content

Commit bcf9efa

Browse files
committed
第一次添加
内容包括PDF版教材,北大陈斌python数据结构课的课件以及pythonds数据结构实现的源代码
1 parent 814398e commit bcf9efa

File tree

138 files changed

+25412
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+25412
-0
lines changed

2022数据结构与算法B/pythonds/__init__.py

Whitespace-only changes.
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#__all__ = ["stack"]
3+
4+
5+
from .stack import Stack
6+
from .queue import Queue
7+
8+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Bradley N. Miller, David L. Ranum
2+
# Introduction to Data Structures and Algorithms in Python
3+
# Copyright 2005
4+
#
5+
#deque.py
6+
7+
8+
class Deque:
9+
def __init__(self):
10+
self.items = []
11+
12+
def isEmpty(self):
13+
return self.items == []
14+
15+
def addFront(self, item):
16+
self.items.append(item)
17+
18+
def addRear(self, item):
19+
self.items.insert(0,item)
20+
21+
def removeFront(self):
22+
return self.items.pop()
23+
24+
def removeRear(self):
25+
return self.items.pop(0)
26+
27+
def size(self):
28+
return len(self.items)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Bradley N. Miller, David L. Ranum
2+
# Introduction to Data Structures and Algorithms in Python
3+
# Copyright 2005
4+
#
5+
#queue.py
6+
7+
class Queue:
8+
def __init__(self):
9+
self.items = []
10+
11+
def isEmpty(self):
12+
return self.items == []
13+
14+
def enqueue(self, item):
15+
self.items.insert(0,item)
16+
17+
def dequeue(self):
18+
return self.items.pop()
19+
20+
def size(self):
21+
return len(self.items)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Bradley N. Miller, David L. Ranum
2+
# Introduction to Data Structures and Algorithms in Python
3+
# Copyright 2005
4+
#
5+
#stack.py
6+
7+
class Stack:
8+
def __init__(self):
9+
self.items = []
10+
11+
def isEmpty(self):
12+
return self.items == []
13+
14+
def push(self, item):
15+
self.items.append(item)
16+
17+
def pop(self):
18+
return self.items.pop()
19+
20+
def peek(self):
21+
return self.items[len(self.items)-1]
22+
23+
def size(self):
24+
return len(self.items)
25+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
from .adjGraph import Graph
4+
from .adjGraph import Vertex
5+
from .priorityQueue import PriorityQueue
Binary file not shown.
Binary file not shown.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#
2+
# adjGraph
3+
#
4+
# Created by Brad Miller on 2005-02-24.
5+
# Copyright (c) 2005 Brad Miller, David Ranum, Luther College. All rights reserved.
6+
#
7+
8+
import sys
9+
import os
10+
import unittest
11+
12+
class Graph:
13+
def __init__(self):
14+
self.vertices = {}
15+
self.numVertices = 0
16+
17+
def addVertex(self,key):
18+
self.numVertices = self.numVertices + 1
19+
newVertex = Vertex(key)
20+
self.vertices[key] = newVertex
21+
return newVertex
22+
23+
def getVertex(self,n):
24+
if n in self.vertices:
25+
return self.vertices[n]
26+
else:
27+
return None
28+
29+
def __contains__(self,n):
30+
return n in self.vertices
31+
32+
def addEdge(self,f,t,cost=0):
33+
if f not in self.vertices:
34+
nv = self.addVertex(f)
35+
if t not in self.vertices:
36+
nv = self.addVertex(t)
37+
self.vertices[f].addNeighbor(self.vertices[t],cost)
38+
39+
def getVertices(self):
40+
return list(self.vertices.keys())
41+
42+
def __iter__(self):
43+
return iter(self.vertices.values())
44+
45+
class Vertex:
46+
def __init__(self,num):
47+
self.id = num
48+
self.connectedTo = {}
49+
self.color = 'white'
50+
self.dist = sys.maxsize
51+
self.pred = None
52+
self.disc = 0
53+
self.fin = 0
54+
55+
# def __lt__(self,o):
56+
# return self.id < o.id
57+
58+
def addNeighbor(self,nbr,weight=0):
59+
self.connectedTo[nbr] = weight
60+
61+
def setColor(self,color):
62+
self.color = color
63+
64+
def setDistance(self,d):
65+
self.dist = d
66+
67+
def setPred(self,p):
68+
self.pred = p
69+
70+
def setDiscovery(self,dtime):
71+
self.disc = dtime
72+
73+
def setFinish(self,ftime):
74+
self.fin = ftime
75+
76+
def getFinish(self):
77+
return self.fin
78+
79+
def getDiscovery(self):
80+
return self.disc
81+
82+
def getPred(self):
83+
return self.pred
84+
85+
def getDistance(self):
86+
return self.dist
87+
88+
def getColor(self):
89+
return self.color
90+
91+
def getConnections(self):
92+
return self.connectedTo.keys()
93+
94+
def getWeight(self,nbr):
95+
return self.connectedTo[nbr]
96+
97+
def __str__(self):
98+
return str(self.id) + ":color " + self.color + ":disc " + str(self.disc) + ":fin " + str(self.fin) + ":dist " + str(self.dist) + ":pred \n\t[" + str(self.pred)+ "]\n"
99+
100+
def getId(self):
101+
return self.id
102+
103+
class adjGraphTests(unittest.TestCase):
104+
def setUp(self):
105+
self.tGraph = Graph()
106+
107+
def testMakeGraph(self):
108+
gFile = open("test.dat")
109+
for line in gFile:
110+
fVertex, tVertex = line.split('|')
111+
fVertex = int(fVertex)
112+
tVertex = int(tVertex)
113+
self.tGraph.addEdge(fVertex,tVertex)
114+
for i in self.tGraph:
115+
adj = i.getAdj()
116+
for k in adj:
117+
print(i, k)
118+
119+
120+
if __name__ == '__main__':
121+
unittest.main()
122+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Bradley N. Miller, David L. Ranum
2+
# Introduction to Data Structures and Algorithms in Python
3+
# Copyright 2005
4+
#
5+
import unittest
6+
7+
# this implementation of binary heap takes key value pairs,
8+
# we will assume that the keys are all comparable
9+
10+
class PriorityQueue:
11+
def __init__(self):
12+
self.heapArray = [(0,0)]
13+
self.currentSize = 0
14+
15+
def buildHeap(self,alist):
16+
self.currentSize = len(alist)
17+
self.heapArray = [(0,0)]
18+
for i in alist:
19+
self.heapArray.append(i)
20+
i = len(alist) // 2
21+
while (i > 0):
22+
self.percDown(i)
23+
i = i - 1
24+
25+
def percDown(self,i):
26+
while (i * 2) <= self.currentSize:
27+
mc = self.minChild(i)
28+
if self.heapArray[i][0] > self.heapArray[mc][0]:
29+
tmp = self.heapArray[i]
30+
self.heapArray[i] = self.heapArray[mc]
31+
self.heapArray[mc] = tmp
32+
i = mc
33+
34+
def minChild(self,i):
35+
if i*2 > self.currentSize:
36+
return -1
37+
else:
38+
if i*2 + 1 > self.currentSize:
39+
return i*2
40+
else:
41+
if self.heapArray[i*2][0] < self.heapArray[i*2+1][0]:
42+
return i*2
43+
else:
44+
return i*2+1
45+
46+
def percUp(self,i):
47+
while i // 2 > 0:
48+
if self.heapArray[i][0] < self.heapArray[i//2][0]:
49+
tmp = self.heapArray[i//2]
50+
self.heapArray[i//2] = self.heapArray[i]
51+
self.heapArray[i] = tmp
52+
i = i//2
53+
54+
def add(self,k):
55+
self.heapArray.append(k)
56+
self.currentSize = self.currentSize + 1
57+
self.percUp(self.currentSize)
58+
59+
def delMin(self):
60+
retval = self.heapArray[1][1]
61+
self.heapArray[1] = self.heapArray[self.currentSize]
62+
self.currentSize = self.currentSize - 1
63+
self.heapArray.pop()
64+
self.percDown(1)
65+
return retval
66+
67+
def isEmpty(self):
68+
if self.currentSize == 0:
69+
return True
70+
else:
71+
return False
72+
73+
def decreaseKey(self,val,amt):
74+
# this is a little wierd, but we need to find the heap thing to decrease by
75+
# looking at its value
76+
done = False
77+
i = 1
78+
myKey = 0
79+
while not done and i <= self.currentSize:
80+
if self.heapArray[i][1] == val:
81+
done = True
82+
myKey = i
83+
else:
84+
i = i + 1
85+
if myKey > 0:
86+
self.heapArray[myKey] = (amt,self.heapArray[myKey][1])
87+
self.percUp(myKey)
88+
89+
def __contains__(self,vtx):
90+
for pair in self.heapArray:
91+
if pair[1] == vtx:
92+
return True
93+
return False
94+
95+
class TestBinHeap(unittest.TestCase):
96+
def setUp(self):
97+
self.theHeap = PriorityQueue()
98+
self.theHeap.add((2,'x'))
99+
self.theHeap.add((3,'y'))
100+
self.theHeap.add((5,'z'))
101+
self.theHeap.add((6,'a'))
102+
self.theHeap.add((4,'d'))
103+
104+
105+
def testInsert(self):
106+
assert self.theHeap.currentSize == 5
107+
108+
def testDelmin(self):
109+
assert self.theHeap.delMin() == 'x'
110+
assert self.theHeap.delMin() == 'y'
111+
112+
def testDecKey(self):
113+
self.theHeap.decreaseKey('d',1)
114+
assert self.theHeap.delMin() == 'd'
115+
116+
if __name__ == '__main__':
117+
unittest.main()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
4+
from .balance import AVLTree
5+
from .bst import BinarySearchTree
6+
from .binheap import BinHeap
7+
8+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)