Skip to content

Commit daf47e7

Browse files
author
sy-md
committed
my dsa work recorded
1 parent 6b8a495 commit daf47e7

23 files changed

+1145
-0
lines changed

data_struct/myexamples/algo/bfs.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def how_it_workd(data, start, end, visited=[]):
2+
queue = [start]
3+
4+
while queue:
5+
current_node = queue.pop(0)
6+
7+
visited.append(current_node)
8+
print(visited)
9+
10+
for i in data[current_node] - set(visited):
11+
print("for looped --> {}".format(i))
12+
print()
13+
queue.append(i)
14+
15+
print("wants the data being bf-searched")
16+
print(data)
17+
18+
19+
def bfs(data, start, end, visited=[]):
20+
queue = [start]
21+
22+
while queue:
23+
current_node = queue.pop(0)
24+
if current_node==end:
25+
print("Path: " + "->".join(visited) + "->" + end)
26+
return
27+
visited.append(current_node)
28+
29+
for i in data[current_node] - set(visited):
30+
print("for looping -->",i)
31+
queue.append(i)
32+
print("Path does not exist!")
33+
34+
35+
if __name__ == '__main__':
36+
data = {
37+
'A': {'B'},
38+
'B': {'C', 'D'},
39+
'C': {'E'},
40+
'D': {'E'},
41+
'E': {'F'},
42+
'F': set()
43+
}
44+
print("how it works")
45+
how_it_workd(data, "A", "D")
46+
print("out come")
47+
bfs(data, 'A', 'D')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class node:
2+
def __init__(self,val=None):
3+
self.val = val
4+
self.l = None
5+
self.r = None
6+
7+
class bst:
8+
def __init__(self):
9+
self.head = None
10+
11+
def lvl_order(self):
12+
""""
13+
level order queue:
14+
1.)give q the starting pos
15+
2.)as long as the q is full
16+
17+
3.) take first thing in q mark as visited
18+
4.) check if that popped items has children
19+
if they do put then in the queue
20+
21+
""""
22+
vis = []
23+
q = []
24+
q.append(self.head)
25+
26+
while len(q) > 0:
27+
cur = q.pop(0)
28+
vis.append(cur)
29+
30+
if cur.l:
31+
q.append(cur.l)
32+
if cur.r:
33+
q.append(cur.r)
34+
35+
for x in vis:
36+
print(x.val)
37+
38+
t = bst()
39+
t.head = node(4)
40+
t.head.l = node(2)
41+
t.head.r = node(8)
42+
t.head.l.l = node(1)
43+
t.head.l.r = node(3)
44+
t.head.r.l = node(5)
45+
t.head.r.r = node(10)
46+
t.lvl_order()
47+
48+

data_struct/myexamples/bst.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class bst:
2+
def __init__(self,val=None):
3+
self.val = val
4+
self.l = None
5+
self.r = None
6+
7+
8+
def inorder(self):
9+
10+
if self.l:
11+
self.l.postorder()
12+
print(self.val)
13+
if self.r:
14+
self.r.postorder()
15+
16+
def postorder(self):
17+
18+
print(self.val)
19+
if self.l:
20+
self.l.postorder()
21+
if self.r:
22+
self.r.postorder()
23+
24+
def insert(self,val):
25+
26+
if val < self.val:
27+
if self.l == None:
28+
self.l = bst(val)
29+
else:
30+
self.l.insert(val)
31+
else:
32+
if self.r == None:
33+
self.r = bst(val)
34+
else:
35+
self.r.insert(val)
36+
37+
def prt2d(self,sp,h):
38+
#dis between lv
39+
sp += h
40+
41+
cur = self
42+
43+
if cur is None:
44+
return
45+
46+
bst.prt2d(cur.r,sp,h)
47+
print()
48+
for i in range(h,sp):
49+
print(" ",end="")
50+
print(cur.val,end="")
51+
print()
52+
bst.prt2d(cur.l,sp,h)
53+
54+
tree = [bst(50),bst(50)]
55+
56+
tree[0].insert(20)
57+
tree[0].insert(16)
58+
tree[0].insert(10)
59+
tree[0].insert(18)
60+
tree[0].insert(60)
61+
tree[0].insert(70)
62+
tree[0].insert(65)
63+
tree[0].insert(100)
64+
65+
tree[0].postorder()
66+
print()
67+
print("displaying 2d tree")
68+
tree[0].prt2d(0,5)
69+
70+
print("##################")
71+
72+
tree[1].insert(40)
73+
tree[1].insert(60)
74+
tree[1].inorder()
75+
print()
76+
print("displaying 2d tree")
77+
tree[1].prt2d(0,5)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class node:
2+
def __init__(self,val=None):
3+
self.val = val
4+
self.next = None
5+
self.prev = None
6+
7+
class linked:
8+
def __init__(self):
9+
self.head = None
10+
11+
def insert(self,val):
12+
cur = self.head
13+
prev = None
14+
nd = node(val)
15+
16+
if cur is None:
17+
cur = nd
18+
else:
19+
while cur.next:
20+
cur = cur.next
21+
cur.next = nd
22+
nd.next = cur
23+
24+
25+
26+
l = linked()
27+
28+
l.head = node("start")
29+
l.insert("a")
30+
31+
32+
print("test")
33+
print(l.head.val)
34+
print(l.head.next.val)
35+
print(l.head.next.next.val)

data_struct/myexamples/double-ll.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class node:
2+
def __init__(self,val=None):
3+
self.val = val
4+
self.next = None
5+
self.prev = None
6+
7+
class linked:
8+
def __init__(self):
9+
self.head = None
10+
self.tail = None
11+
12+
def insert(self,val):
13+
cur = self.head
14+
tail = self.head
15+
nd = node(val)
16+
17+
if cur is None:
18+
cur = nd
19+
20+
while cur.next:
21+
cur = cur.next
22+
cur.next = nd
23+
nd.prev = cur
24+
25+
def display_next(self):
26+
cur = self.head
27+
while cur :
28+
print(cur.val,"-->",end="")
29+
cur = cur.next
30+
print("None")
31+
32+
def display_prev(self):
33+
cur = self.head
34+
while cur :
35+
print(cur.val,"<--",end="")
36+
cur = cur.next
37+
print("None")
38+
39+
l = linked()
40+
l.head = node("a")
41+
l.insert("b")
42+
l.insert("c")
43+
l.insert("d")
44+
l.insert("e")
45+
l.insert("f")
46+
l.insert("g")
47+
l.display_next()
48+
l.display_prev()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#hashtable
2+
3+
class hashmap:
4+
def __init__(self):
5+
self.size = 20
6+
self.ht = [[] for _ in range(0, self.size)]
7+
8+
def set(self,key,data):
9+
hashed_key = hash(key) % self.size
10+
buckets = self.ht[hashed_key]
11+
12+
exist = False
13+
for i , kv in enumerate(buckets):
14+
k,v = kv
15+
if key == k:
16+
exist = True
17+
break
18+
19+
if exist:
20+
buckets[i] = ((key,data))
21+
else:
22+
buckets.append((key,data))
23+
24+
def get(self,key):
25+
26+
hashed_key = hash(key) % self.size
27+
buckets = self.ht[hashed_key]
28+
29+
found = False
30+
for i , kv in enumerate(buckets):
31+
k,v = kv
32+
if key == k:
33+
found = True
34+
break
35+
if found:
36+
return key
37+
else:
38+
print("not found")
39+
40+
41+
h = hashmap()
42+
43+
44+
h.set("big_house", "martell")
45+
h.set("med_house", "tony")
46+
h.set("small_house", "emma")
47+
48+
print(h.ht)
49+
50+
51+
52+

0 commit comments

Comments
 (0)