Skip to content

Commit 4dc9ecc

Browse files
committed
add cs6006 recitations
1 parent 8a45d30 commit 4dc9ecc

File tree

6 files changed

+585
-0
lines changed

6 files changed

+585
-0
lines changed
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
from typing import List
2+
import math
3+
4+
5+
class Dynamic_Array:
6+
def __init__(self):
7+
self.size = 0
8+
self.array = []
9+
self.array_value_bound = 0
10+
pass
11+
12+
def __len__(self):
13+
return self.size
14+
15+
def __iter__(self):
16+
yield self.array
17+
18+
def build(self, x: List[int]):
19+
for index, item in enumerate(x):
20+
while index >= self.size:
21+
self._resize()
22+
self.array[index] = item
23+
self.array_value_bound = len(x)
24+
return self
25+
26+
def _resize(self):
27+
current_size = self.size if self.size > 0 else 1
28+
new_size = current_size * 2
29+
new_array = [None]*new_size
30+
new_array = self._old_to_new(True, new_array)
31+
self.array = new_array
32+
self.size = len(new_array)
33+
34+
def _old_to_new(self, isBuildProcess, new_array: List[int])->List[int]:
35+
if self.size == 0:
36+
return new_array
37+
if isBuildProcess:
38+
for index, _ in enumerate(self.array):
39+
new_array[index] = self.array[index]
40+
else:
41+
for index in range(self.array_value_bound):
42+
new_array[index] = self.array[index]
43+
return new_array
44+
45+
46+
def insert_last(self, item: int)-> None:
47+
if self.size == self.array_value_bound:
48+
self._resize()
49+
return self.insert_last(item)
50+
self.array[self.array_value_bound] = item
51+
52+
def _resize_decrese(self):
53+
current_size = self.size if self.size > 0 else 1
54+
new_size = math.floor(current_size / 2)
55+
new_array = [None]*new_size
56+
new_array = self._old_to_new(False, new_array)
57+
self.array = new_array
58+
self.size = len(new_array)
59+
60+
61+
def delete_last(self):
62+
self.array[self.array_value_bound - 1] = None
63+
print("comparing to",math.floor((self.size)/4))
64+
65+
if self.array_value_bound <= math.floor((self.size)/4):
66+
print("decresing size")
67+
self._resize_decrese()
68+
self.size = len(self.array) - 1
69+
self.array_value_bound -= 1
70+
print("final array", self.array)
71+
72+
def insert_at(self, index):
73+
pass
74+
75+
def delete_at(self):
76+
pass
77+
78+
def insert_first(self):
79+
pass
80+
81+
def delete_first(self):
82+
pass
83+
84+
test = [1, 2, 3, 4, 5]
85+
d_array = Dynamic_Array().build(test)
86+
print("STARTING WITH", d_array.array)
87+
# [1, 2, 3, 4, 5, None, None, None]
88+
print("DELETE OPERATION")
89+
d_array.delete_last()
90+
# [1, 2, 3, 4, None, None, None, None]
91+
92+
print("DELETE OPERATION")
93+
d_array.delete_last()
94+
# [1, 2, 3, None, None, None, None, None]
95+
96+
print("DELETE OPERATION")
97+
d_array.delete_last()
98+
# [1, 2, None, None, None, None, None, None]
99+
# [1, 2, None, None]
100+
101+
print("DELETE OPERATION")
102+
d_array.delete_last()
103+
# [1, None, None, None]
104+
# [1, None]
105+
106+
print("FINALLY")
107+
for item in d_array:
108+
print(item)

cs-6006/recitations/2/linked-list.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Node class is a single node in the linked list.
2+
class Node:
3+
def __init__(self, x):
4+
self.node = x
5+
self.next = None
6+
7+
8+
class Linked_List:
9+
def __init__(self):
10+
self.head = None
11+
self.size = 0
12+
13+
def __len__(self):
14+
return self.size
15+
16+
def __iter__(self):
17+
node = self.head
18+
19+
while(node):
20+
yield node.node
21+
node = node.next
22+
23+
def build(self, items):
24+
if(len(items) <= 0):
25+
self.head = None
26+
self.size = 0
27+
return self
28+
29+
self.head = Node(items[0])
30+
temp_pointer = self.head
31+
self.size += 1
32+
for item in items[1:]:
33+
new_node = Node(item)
34+
temp_pointer.next = new_node
35+
temp_pointer = new_node
36+
self.size += 1
37+
return self
38+
39+
40+
def get_at(self, index):
41+
if(self.head) == None: return None
42+
start = self.head
43+
while index > 1:
44+
if (start == None):
45+
return None
46+
start = start.next
47+
index -= 1
48+
49+
def set_at(self, item, index):
50+
node = self.get_at(index-1)
51+
if (node == None):
52+
return "index out of range"
53+
new_node = Node(item)
54+
if (node.next == None):
55+
node.next = new_node
56+
next_node = node.next
57+
new_node.next = next_node.next
58+
node.next = new_node
59+
60+
def insert_first(self, x):
61+
new_node = Node(x)
62+
new_node.next = self.head
63+
self.head = new_node
64+
65+
66+
def delete_first(self):
67+
if(self.head == None): return None
68+
self.head = self.head.next
69+
70+
71+
def insert_at(self, index, item):
72+
node = self.get_at(index-1)
73+
if node == None:
74+
return "index out of range"
75+
temp_node = node
76+
new_node = Node(item)
77+
new_node.next = temp_node.next
78+
node.next = new_node
79+
self.size += 1
80+
81+
def delete_at(self, index):
82+
prev_node = self.get_at(index-1)
83+
if prev_node== None: return "index out of range"
84+
if (prev_node.next == None): return self
85+
node_to_delete = prev_node.next
86+
prev_node.next = node_to_delete.next
87+
88+
89+
def insert_last(self, item):
90+
new_node = Node(item)
91+
if not self.head: # If the list is empty
92+
self.head = new_node
93+
else:
94+
last_node = self.get_at(self.size)
95+
last_node.next = new_node
96+
97+
def delete_last(self):
98+
return self.delete_at(self.size)
99+

cs-6006/recitations/tree/BST.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from binary_tree import BinaryNode, BinaryTree
2+
3+
class BST_Node(BinaryNode):
4+
def subtree_find(self, A):
5+
"""
6+
If A is self.item return it.
7+
If A is greater than self.item then go to the right
8+
subtree, else go to the left subtree.
9+
"""
10+
if A < self.item.key:
11+
if self.left:
12+
return self.left.subtree_find(A)
13+
elif A > self.item.key:
14+
if self.right:
15+
return self.right.subtree_find(A)
16+
else: return A
17+
return None
18+
19+
def subtree_find_next(self, A):
20+
"""
21+
If A is found, return first item in the right subtree.
22+
If A is not found, return item itself.
23+
"""
24+
if self.item.key <= A:
25+
if self.right:
26+
return self.right.subtree_find_next(A)
27+
else: return None
28+
elif self.left:
29+
B = self.left.subtree_find_next(A)
30+
if B: return B
31+
return self
32+
33+
def subtree_find_prev(self, A):
34+
if self.item.key >= A:
35+
if self.left: return self.left.subtree_find_prev(A)
36+
# end of the tree.
37+
else: return None
38+
elif self.right:
39+
return self.right.subtree_find_prev(A)
40+
return self
41+
42+
def subtree_insert(self, A):
43+
pass
44+
45+
46+
class Set_Binary_Tree(BinaryTree):
47+
"""
48+
Top level Data structure for holding a Set Binary Tree
49+
"""
50+
def __init__(self): super().__init__(BST_Node)
51+
52+
def build_tree(self, X):
53+
pass
54+
55+
def find_min(self):
56+
pass
57+
58+
def find_max(self):
59+
pass
60+
61+
def find(self, k):
62+
pass
63+
64+
def find_next(self, k):
65+
pass
66+
67+
def find_prev(self, k):
68+
pass
69+
70+
def insert(self, x):
71+
pass
72+
73+
def delete(self, k):
74+
pass

0 commit comments

Comments
 (0)