├── LAB 5 ├── Task 1 d.py ├── Task 1 a.py ├── Task 2 a.py ├── Task 1 b.py ├── Task 1 c.py ├── Task 3.py ├── Task 4 a.py ├── Task 4 b.py ├── Task 5.py ├── Task 2 b.py └── Task 2 c.py ├── LAB 2 ├── task 1.py ├── Task 2.py └── task 3.py ├── LAB 6 ├── Task 7.py ├── Task 2.py ├── Task 1.py ├── Task 6.py ├── Task 5.py ├── Task 3.py └── Task 4.py ├── LAB 1 ├── task 2.py ├── task 6.py ├── task 1.py ├── task 7.py ├── task 3.py ├── task 8.py ├── task 4.py ├── task 9.py ├── task 5.py ├── task 11.py └── task 10.py ├── LAB 8 ├── Task 4.py ├── Task 5.py ├── Task 3.py ├── task 7.py ├── Task 2.py ├── Task 1.py └── Task 6.py ├── LAB 7 ├── Task 1.py └── Task 2.py ├── LAB 4 ├── Task 1.py └── Task 2.py ├── LAB 3 └── task1+2.py └── README.md /LAB 5/Task 1 d.py: -------------------------------------------------------------------------------- 1 | # d 2 | def powerN(base, n): 3 | if n == 0: 4 | return 1 5 | else: 6 | return base*powerN(base, n-1) 7 | 8 | 9 | power = powerN(3, 3) 10 | print(power) 11 | -------------------------------------------------------------------------------- /LAB 5/Task 1 a.py: -------------------------------------------------------------------------------- 1 | # Task 1 [Very Easy] 2 | # a 3 | 4 | def factorial(n): 5 | if n == 0 | n == 1: 6 | return 1 7 | else: 8 | return n * factorial(n-1) 9 | 10 | 11 | fact = factorial(6) 12 | print(fact) 13 | -------------------------------------------------------------------------------- /LAB 5/Task 2 a.py: -------------------------------------------------------------------------------- 1 | # task 2 [Easy] 2 | # a 3 | def deci_to_binary(n): 4 | if n == 0: 5 | return 0 6 | else: 7 | return deci_to_binary(n//2)*10 + (n % 2) 8 | 9 | 10 | decimal = deci_to_binary(11) 11 | print(decimal) 12 | -------------------------------------------------------------------------------- /LAB 5/Task 1 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | 3 | def fibonacci(n): 4 | if n == 0: 5 | return 0 6 | elif n == 1: 7 | return 1 8 | else: 9 | return fibonacci(n-1) + fibonacci(n-2) 10 | 11 | 12 | fib = fibonacci(4) 13 | print(fib) 14 | -------------------------------------------------------------------------------- /LAB 5/Task 1 c.py: -------------------------------------------------------------------------------- 1 | # c 2 | def print_array(list, index): 3 | if index == len(list): 4 | return 5 | else: 6 | print(list[index]) 7 | print_array(list, index+1) 8 | 9 | 10 | list = [1, 2, 3, 4, 5, 7] 11 | print_array(list, 0) 12 | -------------------------------------------------------------------------------- /LAB 2/task 1.py: -------------------------------------------------------------------------------- 1 | # Task 1: 2 | 3 | class Node: 4 | def __init__(self, integer, next): 5 | self.integer = integer 6 | self.next = next 7 | 8 | 9 | class MyList: 10 | def __init__(self): 11 | self.head = None 12 | 13 | 14 | mylinkedlist = MyList() 15 | -------------------------------------------------------------------------------- /LAB 6/Task 7.py: -------------------------------------------------------------------------------- 1 | # task 7 2 | arr = [-1]*99 3 | 4 | 5 | def fibonacci(n): 6 | if n == 0 or n == 1: 7 | return n 8 | else: 9 | if arr[n] == -1: 10 | arr[n] = fibonacci(n-1) + fibonacci(n-2) 11 | return arr[n] 12 | 13 | 14 | fi = fibonacci(12) 15 | print(fi) 16 | -------------------------------------------------------------------------------- /LAB 5/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 [Medium] 2 | def hocBuilder(height): 3 | if height == 0: 4 | return str(0) + "\nnot build a house at all." 5 | elif height == 1: 6 | return 8 7 | else: 8 | return 5+hocBuilder(height-1) 9 | 10 | 11 | height = int(input("Enter height: ")) 12 | print(hocBuilder(height)) 13 | -------------------------------------------------------------------------------- /LAB 1/task 2.py: -------------------------------------------------------------------------------- 1 | def rotateLeft(source, k): 2 | list_lenght = len(source) 3 | for values in range(k): 4 | new_value = source[0] 5 | for value in range(1, list_lenght): 6 | source[value - 1] = source[value] 7 | source[list_lenght - 1] = new_value 8 | 9 | 10 | source = [10, 20, 30, 40, 50, 60] 11 | rotateLeft(source, 3) 12 | print(source) 13 | -------------------------------------------------------------------------------- /LAB 1/task 6.py: -------------------------------------------------------------------------------- 1 | def array_series(n): 2 | n_squared = n*n 3 | new_array = [0]*n_squared 4 | 5 | x = 1 6 | while x <= n: 7 | y = 1 8 | while y <= x: 9 | new_array[(x*n)-y] = y 10 | 11 | y += 1 12 | x += 1 13 | return new_array 14 | 15 | 16 | n = int(input("Please input a number: n= ")) 17 | print(array_series(n)) 18 | -------------------------------------------------------------------------------- /LAB 5/Task 4 a.py: -------------------------------------------------------------------------------- 1 | # task 4 [Hard] 2 | # a 3 | 4 | def pattern(n): 5 | if n == 0: 6 | return 0 7 | else: 8 | pattern(n-1) 9 | print(n, end="") 10 | 11 | 12 | def print_pattern(n): 13 | if n == 0: 14 | return 0 15 | else: 16 | print_pattern(n-1) 17 | pattern(n) 18 | print() 19 | 20 | 21 | n = int(input("Enter n: ")) 22 | 23 | print_pattern(n) 24 | -------------------------------------------------------------------------------- /LAB 1/task 1.py: -------------------------------------------------------------------------------- 1 | def shiftLeft(source, k): 2 | index = len(source) # total index of array 3 | for v in range(index): 4 | x = v - k 5 | if x >= 0: 6 | source[x] = source[v] 7 | 8 | remain_index = len(source) - k 9 | for v in range(remain_index, index): 10 | source[v] = 0 11 | 12 | print(source) 13 | 14 | 15 | source = [10, 20, 30, 40, 50, 60] 16 | 17 | shiftLeft(source, 3) 18 | -------------------------------------------------------------------------------- /LAB 1/task 7.py: -------------------------------------------------------------------------------- 1 | # 7 Max Bunch Count 2 | 3 | def Max_Bunch_Count(new_array): 4 | sum_list = [] 5 | first = new_array[0] 6 | c = 0 7 | for values in new_array: 8 | if values == first: 9 | c += 1 10 | 11 | else: 12 | first = values 13 | c = 1 14 | sum_list.append(c) 15 | print(max(sum_list)) 16 | 17 | 18 | new_array = [1, 2, 2, 3, 4, 4, 4] 19 | Max_Bunch_Count(new_array) 20 | -------------------------------------------------------------------------------- /LAB 1/task 3.py: -------------------------------------------------------------------------------- 1 | def remove(source, size, idx): 2 | source_index = len(source) 3 | new_list = source_index * [0] 4 | x = 0 5 | for values in range(size): 6 | if values == idx: 7 | continue 8 | else: 9 | new_list[x] = source[values] 10 | x += 1 11 | 12 | source = new_list 13 | 14 | print(source) 15 | 16 | 17 | source = [10, 20, 30, 40, 50, 0, 0] 18 | 19 | remove(source, 5, 2) 20 | -------------------------------------------------------------------------------- /LAB 6/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | def rec_insertion_sort(array, index): 4 | if index <= 1: 5 | return -1 6 | rec_insertion_sort(array, index-1) 7 | x = index - 2 8 | y = array[index - 1] 9 | while (array[x] > y and x >= 0): 10 | array[x+1] = array[x] 11 | x -= 1 12 | array[x+1] = y 13 | 14 | 15 | array = [6, 7, 8, 9, 4, 3, 2] 16 | index = len(array) 17 | rec_insertion_sort(array, index) 18 | print(array) 19 | -------------------------------------------------------------------------------- /LAB 1/task 8.py: -------------------------------------------------------------------------------- 1 | # 8 Repetition 2 | 3 | def repetition(new_array): 4 | new_list = [] 5 | dictionary = {x: new_array.count(x) for x in new_array} 6 | for keys, values in dictionary.items(): 7 | if values > 1: 8 | new_list.append(values) 9 | if len(new_list) == len(set(new_list)): 10 | print("False") 11 | else: 12 | print("True") 13 | 14 | 15 | new_array = [4, 5, 6, 6, 4, 3, 6, 4] 16 | repetition(new_array) 17 | -------------------------------------------------------------------------------- /LAB 1/task 4.py: -------------------------------------------------------------------------------- 1 | def removeAll(source, size, elements): 2 | source_index = len(source) 3 | new_list = [0] * source_index 4 | x = 0 5 | for values in source: 6 | if values == elements: 7 | continue 8 | else: 9 | new_list[x] = values 10 | x += 1 11 | if x >= size: 12 | break 13 | source = new_list 14 | print(source) 15 | 16 | 17 | source = [10, 2, 30, 2, 50, 2, 2, 0, 0] 18 | removeAll(source, 7, 2) 19 | -------------------------------------------------------------------------------- /LAB 6/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | def rec_selection_sort(array, x, y): 4 | index = len(array) 5 | if x < index-1: 6 | min_index = x 7 | rec_selection_sort(array, x + 1, x + 2) 8 | if y < index: 9 | if array[y] < array[min_index]: 10 | min_index = y 11 | rec_selection_sort(array, x, y+1) 12 | array[x], array[min_index] = array[min_index], array[x] 13 | 14 | 15 | array = [5, 7, 9, 8, 2, 1] 16 | rec_selection_sort(array, 0, 1) 17 | print(array) 18 | -------------------------------------------------------------------------------- /LAB 5/Task 4 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | 3 | 4 | def pattern(n): 5 | if n == 0: 6 | return 0 7 | else: 8 | pattern(n-1) 9 | print(n, end="") 10 | 11 | 12 | def print_pattern(n, i): 13 | if n == 0: 14 | return 0 15 | else: 16 | space(n-1) 17 | pattern((i)-n+1) 18 | print() 19 | print_pattern(n-1, i) 20 | 21 | 22 | def space(n): 23 | if n == 0: 24 | return 0 25 | else: 26 | space(n - 1) 27 | print("", end=" ") 28 | 29 | 30 | print_pattern(6, 6) 31 | -------------------------------------------------------------------------------- /LAB 6/Task 6.py: -------------------------------------------------------------------------------- 1 | # task 6 2 | 3 | def rec_binary_search(array, left, right, value): 4 | if left > right: 5 | return -1 6 | else: 7 | mid_value = int((left+right) // 2) 8 | if array[mid_value] < value: 9 | return rec_binary_search(array, mid_value+1, right, value) 10 | elif array[mid_value] > value: 11 | return rec_binary_search(array, left, mid_value-1, value) 12 | else: 13 | return mid_value 14 | 15 | 16 | array = [1, 2, 3, 4, 5, 6, 7] 17 | left = 0 18 | right = len(array) 19 | value = 6 20 | print(rec_binary_search(array, left, right, value)) 21 | -------------------------------------------------------------------------------- /LAB 1/task 9.py: -------------------------------------------------------------------------------- 1 | # Circular Array 2 | # 1 Palindrome 3 | 4 | 5 | def palindrome(array, start, size): 6 | empty_list = [] 7 | start_num = start 8 | array_lenght = len(array) 9 | for values in range(size): 10 | if array[start_num] != 0: 11 | empty_list.append(array[start_num]) 12 | start_num += 1 13 | if start_num == array_lenght: 14 | start_num = 0 15 | logic = (len(empty_list)+1)//2 16 | if empty_list[0:logic] == empty_list[-1:-(logic+1):-1]: 17 | print("True") 18 | else: 19 | print("False") 20 | 21 | 22 | array = [20, 10, 0, 0, 0, 10, 20, 30] 23 | palindrome(array, 5, 5) 24 | -------------------------------------------------------------------------------- /LAB 5/Task 5.py: -------------------------------------------------------------------------------- 1 | class FinalQ: 2 | def print(self, array, idx): 3 | if(idx < len(array)): 4 | profit = self.calcProfit(array[idx]) 5 | print(f"{str(idx+1)}. Investment: {array[idx]}; Profit: {profit} ") 6 | self.print(array, idx+1) 7 | 8 | def calcProfit(self, investment): 9 | if investment <= 25000: 10 | return 0.0 11 | elif investment > 25000 and investment <= 100000: 12 | return 45 + self.calcProfit(investment-1000) 13 | elif investment > 100000: 14 | return 80 + self.calcProfit(investment-1000) 15 | else: 16 | return 0 17 | 18 | 19 | array = [25000, 100000, 250000, 350000] 20 | f = FinalQ() 21 | f.print(array, 0) 22 | -------------------------------------------------------------------------------- /LAB 1/task 5.py: -------------------------------------------------------------------------------- 1 | def Splitting_array(A): 2 | zero_list = [0] 3 | empty_list = [] 4 | A_lentgh = len(A) 5 | logic = None 6 | for i in range(A_lentgh): 7 | if i == 0: 8 | zero_list[i] = A[i] 9 | empty_list = A[i+1:(A_lentgh)] 10 | if sum(zero_list) == sum(empty_list): 11 | logic = True 12 | break 13 | else: 14 | zero_list = [] 15 | logic = False 16 | else: 17 | zero_list = A[0:i+1] 18 | empty_list = A[i + 1:(A_lentgh)] 19 | if sum(zero_list) == sum(empty_list): 20 | logic = True 21 | break 22 | else: 23 | logic = False 24 | print(logic) 25 | 26 | 27 | A = [10, 3, 1, 2, 10] 28 | Splitting_array(A) 29 | -------------------------------------------------------------------------------- /LAB 8/Task 4.py: -------------------------------------------------------------------------------- 1 | # task 4 2 | 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.parent = None 8 | self.left = None 9 | self.right = None 10 | 11 | 12 | def inorder(node): 13 | if node is None: 14 | return 15 | else: 16 | inorder(node.left) 17 | print(node.data, end=" ") 18 | inorder(node.right) 19 | 20 | 21 | root = Node(1) 22 | second_node = Node(2) 23 | thrd_node = Node(3) 24 | fourth_node = Node(4) 25 | fifth_node = Node(5) 26 | sixt_node = Node(6) 27 | seven_node = Node(7) 28 | eight_node = Node(8) 29 | root.left = second_node 30 | root.right = thrd_node 31 | thrd_node.left = fourth_node 32 | thrd_node.right = fifth_node 33 | fifth_node.left = sixt_node 34 | fifth_node.right = seven_node 35 | seven_node.left = eight_node 36 | 37 | inorder(root) 38 | -------------------------------------------------------------------------------- /LAB 8/Task 5.py: -------------------------------------------------------------------------------- 1 | # task 5 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def postorder(node): 12 | if node is None: 13 | return 14 | else: 15 | postorder(node.left) 16 | postorder(node.right) 17 | print(node.data, end=" ") 18 | 19 | 20 | root = Node(1) 21 | second_node = Node(2) 22 | thrd_node = Node(3) 23 | fourth_node = Node(4) 24 | fifth_node = Node(5) 25 | sixt_node = Node(6) 26 | seven_node = Node(7) 27 | eight_node = Node(8) 28 | root.left = second_node 29 | root.right = thrd_node 30 | thrd_node.left = fourth_node 31 | thrd_node.right = fifth_node 32 | fifth_node.left = sixt_node 33 | fifth_node.right = seven_node 34 | seven_node.left = eight_node 35 | 36 | postorder(root) 37 | -------------------------------------------------------------------------------- /LAB 8/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def preorder(node): 12 | if node is None: 13 | return 14 | else: 15 | print(node.data, end=" ") 16 | preorder(node.left) 17 | preorder(node.right) 18 | 19 | 20 | root = Node(1) 21 | second_node = Node(2) 22 | thrd_node = Node(3) 23 | fourth_node = Node(4) 24 | fifth_node = Node(5) 25 | sixt_node = Node(6) 26 | seven_node = Node(7) 27 | eight_node = Node(8) 28 | root.left = second_node 29 | root.right = thrd_node 30 | thrd_node.left = fourth_node 31 | thrd_node.right = fifth_node 32 | fifth_node.left = sixt_node 33 | fifth_node.right = seven_node 34 | seven_node.left = eight_node 35 | 36 | 37 | preorder(root) 38 | -------------------------------------------------------------------------------- /LAB 5/Task 2 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | class Node: 3 | def __init__(self, value): 4 | self.value = value 5 | self.next = None 6 | 7 | 8 | class linklist: 9 | def __init__(self, array): 10 | self.head = None 11 | tail = None 12 | for x in array: 13 | new_node = Node(x) 14 | if self.head == None: 15 | self.head = new_node 16 | tail = new_node 17 | else: 18 | tail.next = new_node 19 | tail = new_node 20 | 21 | def sum_add(self, head): 22 | if head is None: 23 | return 0 24 | else: 25 | return head.value + self.sum_add(head.next) 26 | 27 | def sum(self): 28 | return self.sum_add(self.head) 29 | 30 | 31 | array = [1, 2, 3, 4, 5, 6, 7] 32 | listlinked = linklist(array) 33 | print(listlinked.sum()) 34 | -------------------------------------------------------------------------------- /LAB 8/task 7.py: -------------------------------------------------------------------------------- 1 | # task 7 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def copy_tree(node): 12 | if node == None: 13 | return 14 | else: 15 | print(node.data) 16 | copy_tree(node.left) 17 | copy_tree(node.right) 18 | node.left, node.right = node.right, node.left 19 | 20 | 21 | root = Node(1) 22 | second_node = Node(2) 23 | thrd_node = Node(3) 24 | fourth_node = Node(4) 25 | fifth_node = Node(5) 26 | sixt_node = Node(6) 27 | seven_node = Node(7) 28 | eight_node = Node(8) 29 | root.left = second_node 30 | root.right = thrd_node 31 | thrd_node.left = fourth_node 32 | thrd_node.right = fifth_node 33 | fifth_node.left = sixt_node 34 | fifth_node.right = seven_node 35 | seven_node.left = eight_node 36 | 37 | copy_tree(root) 38 | -------------------------------------------------------------------------------- /LAB 5/Task 2 c.py: -------------------------------------------------------------------------------- 1 | # c 2 | class Node: 3 | def __init__(self, value): 4 | self.value = value 5 | self.next = None 6 | 7 | 8 | class linklist: 9 | def __init__(self, array): 10 | self.head = None 11 | tail = None 12 | for x in array: 13 | new_node = Node(x) 14 | if self.head == None: 15 | self.head = new_node 16 | tail = new_node 17 | else: 18 | tail.next = new_node 19 | tail = new_node 20 | 21 | def reverse(self, head): 22 | if head.next is None: 23 | print(head.value) 24 | else: 25 | self.reverse(head.next) 26 | print(head.value) 27 | 28 | def reverse_print(self): 29 | return self.reverse(self.head) 30 | 31 | 32 | array = [1, 2, 3, 4, 5, 6, 7] 33 | listlinked = linklist(array) 34 | listlinked.reverse_print() 35 | -------------------------------------------------------------------------------- /LAB 8/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | class Node: 4 | 5 | def __init__(self, data): 6 | self.data = data 7 | self.parent = None 8 | self.left = None 9 | self.right = None 10 | 11 | 12 | def get_level(node, data, l): 13 | if node is None: 14 | return 0 15 | if data == node.data: 16 | return l 17 | 18 | leveld = get_level(node.left, data, l + 1) 19 | if leveld != 0: 20 | return leveld 21 | 22 | leveld = get_level(node.right, data, l + 1) 23 | return leveld 24 | 25 | 26 | def getlevel(node, data): 27 | return get_level(node, data, 1) 28 | 29 | 30 | root = Node(1) 31 | second_node = Node(2) 32 | thrd_node = Node(3) 33 | fourth_node = Node(4) 34 | fifth_node = Node(5) 35 | sixt_node = Node(6) 36 | seven_node = Node(7) 37 | eight_node = Node(8) 38 | root.left = second_node 39 | root.right = thrd_node 40 | thrd_node.left = fourth_node 41 | thrd_node.right = fifth_node 42 | fifth_node.left = sixt_node 43 | fifth_node.right = seven_node 44 | seven_node.left = eight_node 45 | 46 | print(f"LEVEL: {getlevel(root,8)}") 47 | -------------------------------------------------------------------------------- /LAB 7/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | def max_val(list): 4 | val = list[0] 5 | for i in list: 6 | if val < abs(i): 7 | val = abs(i) 8 | return val 9 | 10 | 11 | class keyIndex: 12 | def __init__(self, k): 13 | self.k = k 14 | 15 | def key_indx(self): 16 | global maxv 17 | maxv = max_val(self.k) 18 | self.temp = [0] * ((max_val(self.k)*2)+1) 19 | 20 | for i in self.k: 21 | if 0 == self.temp[maxv + i]: 22 | self.temp[maxv + i] = 1 23 | else: 24 | self.temp[maxv+i] = self.temp[maxv+i]+1 25 | 26 | def printlist(self): 27 | print(self.temp) 28 | 29 | def search(self, key): 30 | k1 = int(key) 31 | if 0 > k1+maxv or k1+maxv >= len(self.temp): 32 | print(False) 33 | else: 34 | print(True) 35 | 36 | def sorted(self): 37 | indx = 0 38 | for x in range(0, len(self.temp)): 39 | if len(self.k) <= indx: 40 | print(self.k) 41 | break 42 | if 1 <= self.temp[x]: 43 | for y in range(0, self.temp[x]): 44 | self.k[indx] = x - maxv 45 | indx = indx + 1 46 | 47 | 48 | list = [-4, 7, 3, 4, 2, -9, -7] 49 | key_indx = keyIndex(list) 50 | key_indx.key_indx() 51 | key_indx.printlist() 52 | key_indx.search(3) 53 | key_indx.sorted() 54 | -------------------------------------------------------------------------------- /LAB 8/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | class Node: 4 | def __init__(self, data, parent_node, left, right): 5 | self.data = data 6 | self.parent_node = parent_node 7 | self.left = left 8 | self.right = right 9 | 10 | 11 | class binarytree: 12 | def __init__(self): 13 | self.root = Node(1, None, None, None) 14 | self.second_node = Node(2, self.root, None, None) 15 | self.thrd_node = Node(3, self.root, None, None) 16 | self.fourth_node = Node(4, self.thrd_node, None, None) 17 | self.fifth_node = Node(5, self.thrd_node, None, None) 18 | self.sixt_node = Node(6, self.fifth_node, None, None) 19 | self.seven_node = Node(7, self.fifth_node, None, None) 20 | self.root.left = self.second_node 21 | self.root.right = self.thrd_node 22 | self.thrd_node.left = self.fourth_node 23 | self.thrd_node.right = self.fifth_node 24 | self.fifth_node.left = self.sixt_node 25 | self.fifth_node.right = self.seven_node 26 | 27 | def maximum(self, first, second): 28 | if second <= first: 29 | return first 30 | else: 31 | return second 32 | 33 | def height(self, root): 34 | 35 | if root is None: 36 | return 0 37 | 38 | return 1 + self.maximum(self.height(root.left), self.height(root.right)) 39 | 40 | 41 | tree = binarytree() 42 | 43 | print(" Height : ", tree.height(tree.root)) 44 | -------------------------------------------------------------------------------- /LAB 1/task 11.py: -------------------------------------------------------------------------------- 1 | # 3 2 | 3 | import random 4 | 5 | participant = [1, 2, 3, 4, 5, 6, 7] 6 | parti_lenght = len(participant) 7 | 8 | def musical_chair(list): 9 | while parti_lenght > 1: 10 | print("Player Order:", participant) 11 | randint = random.randint(0, 3) 12 | 13 | if randint == 1: 14 | if parti_lenght % 2 == 0: 15 | delete = int(parti_lenght/2)-1 16 | else: 17 | delete = int(parti_lenght/2) 18 | print(f'player{delete} is out') 19 | participant.pop(delete) 20 | participant.insert(0, participant[len(participant)-1]) 21 | participant.pop(len(participant)-1) 22 | print("Winner is", participant[0]) 23 | 24 | participant = [1, 2, 3, 4, 5, 6, 7] 25 | musical_chair(list) 26 | 27 | Player Order: [1, 2, 3, 4, 5, 6, 7] 28 | Player Order: [7, 1, 2, 3, 4, 5, 6] 29 | player3 is out 30 | Player Order: [6, 7, 1, 2, 4, 5] 31 | player3 is out 32 | Player Order: [5, 6, 7, 1, 4] 33 | Player Order: [4, 5, 6, 7, 1] 34 | Player Order: [1, 4, 5, 6, 7] 35 | Player Order: [7, 1, 4, 5, 6] 36 | Player Order: [6, 7, 1, 4, 5] 37 | player3 is out 38 | Player Order: [5, 6, 7, 1] 39 | Player Order: [1, 5, 6, 7] 40 | Player Order: [7, 1, 5, 6] 41 | Player Order: [6, 7, 1, 5] 42 | player3 is out 43 | Player Order: [1, 6, 7] 44 | Player Order: [7, 1, 6] 45 | player3 is out 46 | -------------------------------------------------------------------------------- /LAB 6/Task 5.py: -------------------------------------------------------------------------------- 1 | # task 5 2 | 3 | class Node: 4 | def __init__(self, value, next, prev): 5 | self.value = value 6 | self.next = next 7 | self.prev = prev 8 | 9 | 10 | class Doublylinkedlist: 11 | def __init__(self, array): 12 | self.head = Node(array[0], None, None) 13 | tail = self.head 14 | for x in range(1, len(array)): 15 | new_node = Node(array[x], None, tail) 16 | tail.next = new_node 17 | tail = new_node 18 | 19 | def showList(self): 20 | if self.head.next is None: 21 | print("Empty list") 22 | else: 23 | head = self.head 24 | while self.head is not None: 25 | if head.next is None: 26 | print(head.value) 27 | break 28 | else: 29 | print(head.value, end=" ") 30 | head = head.next 31 | 32 | def insertion_sort(self): 33 | head = self.head 34 | if head == None: 35 | return 36 | else: 37 | while head is not None: 38 | tail = head.next 39 | while tail and tail.prev != None and tail.value < tail.prev.value: 40 | tail.value, tail.prev.value = tail.prev.value, tail.value 41 | tail = tail.prev 42 | head = head.next 43 | 44 | 45 | array = [7, 6, 9, 3, 5, 99, 1] 46 | doublylinkedlist = Doublylinkedlist(array) 47 | doublylinkedlist.insertion_sort() 48 | doublylinkedlist.showList() 49 | -------------------------------------------------------------------------------- /LAB 6/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.next = None 7 | 8 | 9 | class MyList: 10 | def __init__(self, a): 11 | self.head = None 12 | tail = None 13 | 14 | for x in a: 15 | new_node = Node(x) 16 | if self.head == None: 17 | self.head = new_node 18 | tail = new_node 19 | 20 | else: 21 | tail.next = new_node 22 | tail = new_node 23 | 24 | def showList(self): 25 | temp = self.head 26 | if temp is None: 27 | print("Empty List") 28 | else: 29 | while temp is not None: 30 | if temp.next is None: 31 | print(temp.value) 32 | break 33 | else: 34 | print(temp.value, end=" ") 35 | temp = temp.next 36 | 37 | def bubblesort(self): 38 | head = self.head 39 | tail = None 40 | if self.head is None: 41 | return 42 | else: 43 | while head != None: 44 | tail = head.next 45 | while tail != None: 46 | if tail.value < head.value: 47 | head.value, tail.value = tail.value, head.value 48 | tail = tail.next 49 | head = head.next 50 | 51 | 52 | array = [4, 7, 45, 9, 33, 2, 8] 53 | linkedlist = MyList(array) 54 | linkedlist.bubblesort() 55 | linkedlist.showList() 56 | -------------------------------------------------------------------------------- /LAB 8/Task 6.py: -------------------------------------------------------------------------------- 1 | # task 6 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def sameornot(tree_node1, tree_node2): 12 | 13 | if tree_node1 == None and tree_node2 == None: 14 | return True 15 | 16 | if tree_node1 and tree_node2 is not None: 17 | return tree_node1.data == tree_node2.data and sameornot(tree_node1.left, tree_node2.left) and sameornot(tree_node1.right, tree_node2.right) 18 | 19 | return False 20 | 21 | 22 | root = Node(1) 23 | second_node = Node(2) 24 | thrd_node = Node(3) 25 | fourth_node = Node(4) 26 | fifth_node = Node(5) 27 | root.left = second_node 28 | root.right = thrd_node 29 | thrd_node.left = fourth_node 30 | thrd_node.right = fifth_node 31 | sixt_node = Node(6) 32 | seven_node = Node(7) 33 | eight_node = Node(8) 34 | fifth_node.left = sixt_node 35 | fifth_node.right = seven_node 36 | seven_node.left = eight_node 37 | 38 | 39 | root2 = Node(1) 40 | second_node2 = Node(2) 41 | thrd_node2 = Node(3) 42 | fourth_node2 = Node(4) 43 | fifth_node2 = Node(5) 44 | root2.left = second_node2 45 | root2.right = thrd_node2 46 | thrd_node2.left = fourth_node2 47 | thrd_node2.right = fifth_node2 48 | sixt_node2 = Node(6) 49 | seven_node2 = Node(7) 50 | eight_node2 = Node(8) 51 | fifth_node2.left = sixt_node2 52 | fifth_node2.right = seven_node2 53 | seven_node2.left = eight_node2 54 | 55 | 56 | if sameornot(root, root2) is True: 57 | print("Both two trees are exactly same") 58 | else: 59 | print("two trees are not exactly same") 60 | -------------------------------------------------------------------------------- /LAB 7/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | 4 | vowels = ["A", "E", "I", "O", "U"] 5 | consonants = ["B", "C", "D", "F", "G", "H", "J", "K", "L", 6 | "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"] 7 | 8 | 9 | class Hashstring: 10 | def __init__(self, str): 11 | self.str = str 12 | 13 | def string_get(self): 14 | return self.str 15 | 16 | def string_key(self): 17 | val = 0 18 | n = 0 19 | for x in self.str: 20 | if x in consonants: 21 | val = val + 1 22 | if x not in consonants and x not in vowels: 23 | n += int(x) 24 | s_key = ((val * 24) + n) 25 | return s_key 26 | 27 | 28 | class Hashtable: 29 | def __init__(self, size=9): 30 | self.size = size 31 | self.list = [0] * size 32 | 33 | def function(self, string_key): 34 | return string_key % self.size 35 | 36 | def add(self, s): 37 | val = s.string_key() 38 | position = self.function(val) 39 | for y in range(1, self.size): 40 | if self.list[position] == 0: 41 | self.list[position] = s.string_get() 42 | break 43 | else: 44 | position = (val+y) % self.size 45 | 46 | def print(self): 47 | for y in range(self.size): 48 | print(self.list[y]) 49 | 50 | 51 | hash_table = Hashtable() 52 | list = ["X9ZAR2JD6F7", "ZJO9V49NDOU", "G94F5SMC2DG", "KD9LNX46TT2", 53 | "WN2NY54F22D", "YVRFKVUOH6Y", "NJ89I7DN7V8", "5W8UWJSDCJH", "5YLHRBO79CT"] 54 | for x in list: 55 | v = Hashstring(x) 56 | hash_table.add(v) 57 | hash_table.print() 58 | -------------------------------------------------------------------------------- /LAB 6/Task 4.py: -------------------------------------------------------------------------------- 1 | # task 4 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.next = None 7 | 8 | 9 | class MyList: 10 | def __init__(self, a): 11 | self.head = None 12 | tail = None 13 | 14 | for x in a: 15 | new_node = Node(x) 16 | if self.head == None: 17 | self.head = new_node 18 | tail = new_node 19 | 20 | else: 21 | tail.next = new_node 22 | tail = new_node 23 | 24 | def showList(self): 25 | temp = self.head 26 | if temp is None: 27 | print("Empty List") 28 | else: 29 | while temp is not None: 30 | if temp.next is None: 31 | print(temp.value) 32 | break 33 | else: 34 | print(temp.value, end=" ") 35 | temp = temp.next 36 | 37 | def selection_sort(self): 38 | head = self.head 39 | if self.head is None: 40 | return 41 | else: 42 | while head.next != None: 43 | tail = head 44 | val = head.value 45 | next_val = head.next 46 | while next_val != None: 47 | if val > next_val.value: 48 | val = next_val.value 49 | tail = next_val 50 | next_val = next_val.next 51 | head.value, tail.value = tail.value, head.value 52 | head = head.next 53 | 54 | 55 | array = [4, 7, 45, 9, 33, 2, 8] 56 | linkedlist = MyList(array) 57 | linkedlist.selection_sort() 58 | linkedlist.showList() 59 | -------------------------------------------------------------------------------- /LAB 1/task 10.py: -------------------------------------------------------------------------------- 1 | # 2 Intersection 2 | def intersection(array_1, start1, size1, array_2, start2, size2): 3 | new_list1 = [] 4 | new_list2 = [] 5 | new_list3 = [] 6 | new_list4 = [] 7 | new_list5 = [] 8 | array1_lenght = len(array_1) 9 | array2_lenght = len(array_2) 10 | for values in range(1, array1_lenght): 11 | if array_1[values] == 0 and array_1[values-1] != 0: 12 | for x in range(values): 13 | new_list1.append(array_1[x]) 14 | 15 | for value in range(1, array1_lenght): 16 | if array_1[value] != 0 and array_1[value - 1] == 0: 17 | for x in range(value, array1_lenght): 18 | new_list2.append(array_1[x]) 19 | 20 | for values in range(1, array2_lenght): 21 | if array_2[values] == 0 and array_2[values-1] != 0: 22 | for y in range(values): 23 | new_list3.append(array_2[y]) 24 | 25 | for value in range(1, array2_lenght): 26 | if array_2[value] != 0 and array_2[value - 1] == 0: 27 | for y in range(value, array2_lenght): 28 | new_list4.append(array_2[y]) 29 | 30 | combined_list1 = new_list2 + new_list1 31 | combined_list2 = new_list4 + new_list3 32 | 33 | for value in range(len(combined_list1)): 34 | new = combined_list1[value] 35 | for values in range(len(combined_list2)): 36 | if new == combined_list2[values]: 37 | if new not in new_list5: 38 | new_list5.append(new) 39 | else: 40 | continue 41 | else: 42 | continue 43 | print(new_list5) 44 | 45 | 46 | intersection([40, 50, 0, 0, 0, 10, 20, 30], 5, 6, [ 47 | 10, 20, 5, 0, 0, 0, 0, 0, 5, 40, 15, 25], 8, 7) 48 | -------------------------------------------------------------------------------- /LAB 4/Task 1.py: -------------------------------------------------------------------------------- 1 | # Task 1 2 | 3 | stack = [] 4 | index = [] 5 | openingbrackets = ["[",'{',"("] 6 | closingbrackets = ["]","}",")"] 7 | 8 | 9 | def paranthesis_balancing(string): 10 | t = 0 11 | expression = True 12 | for x in range(0,len(string)): 13 | if string[x] in openingbrackets or string[x] in closingbrackets: 14 | if string[x] in openingbrackets: 15 | stack.append(string[x]) 16 | index.append(x+1) 17 | else: 18 | if len(stack) == 0 and string[x] in closingbrackets: 19 | t = string[x] 20 | index.append(x+1) 21 | expression = False 22 | break 23 | t = stack.pop() 24 | expression = bracket_checking(t,string[x]) 25 | if expression == True: 26 | index.pop() 27 | 28 | 29 | if expression is True: 30 | print("This expression is correct.") 31 | 32 | 33 | else: 34 | if len(stack) != 0: 35 | print('This expression is NOT correct.') 36 | print("Error at character # {0}. '{1}'- not closed.".format(index[-1], t)) 37 | 38 | else: 39 | if expression is False: 40 | print('This expression is NOT correct.') 41 | print("Error at character # {0}. '{1}'- not opened.".format(index[0], t)) 42 | 43 | 44 | 45 | 46 | 47 | def bracket_checking(a,b): 48 | if a == "(" and b == ")": 49 | return True 50 | elif a == "{" and b == "}": 51 | return True 52 | elif a == "[" and b =="]": 53 | return True 54 | else: 55 | return False 56 | 57 | string = input() 58 | paranthesis_balancing(string) 59 | -------------------------------------------------------------------------------- /LAB 4/Task 2.py: -------------------------------------------------------------------------------- 1 | # Task 2 2 | 3 | 4 | class Node: 5 | def __init__(self,data): 6 | self.data = data 7 | self.next = None 8 | 9 | class linkedlist: 10 | def __init__(self): 11 | self.head = None 12 | def push(self,element): 13 | if self.head == None: 14 | self.head = Node(element) 15 | else: 16 | n = self.head 17 | while n.next is not None: 18 | n = n.next 19 | n.next = Node(element) 20 | 21 | def pop(self): 22 | global list_status 23 | global available 24 | if self.head == None: 25 | list_status = "Empty" 26 | available = None 27 | else: 28 | if self.head.next == None: 29 | available = self.head.data 30 | self.head = None 31 | else: 32 | n = self.head 33 | while n.next.next is not None: 34 | n = n.next 35 | available = n.next.data 36 | n.next = None 37 | 38 | 39 | 40 | 41 | openingrackets = ["[",'{',"("] 42 | closingrackets = ["]","}",")"] 43 | 44 | def bracket_checking(a,b): 45 | if a == "(" and b == ")": 46 | return True 47 | elif a == "{" and b == "}": 48 | return True 49 | elif a == "[" and b == "]": 50 | return True 51 | else: 52 | return False 53 | 54 | check = linkedlist() 55 | string = input() 56 | expression = False 57 | index = [] 58 | t = 1 59 | x = 0 60 | y = "" 61 | 62 | for v in string: 63 | if v in openingrackets: 64 | check.push(v) 65 | index.append(t) 66 | if v in closingrackets: 67 | check.pop() 68 | if available == None: 69 | expression = False 70 | x = t 71 | y = v 72 | break 73 | else: 74 | expression = bracket_checking(available,v) 75 | if expression == True: 76 | index.pop() 77 | else: 78 | x = index 79 | y = available 80 | t = t+1 81 | 82 | if expression is True: 83 | print('This expression is correct.') 84 | else: 85 | if isinstance(x,list): #The isinstance() function returns True if the specified object is of the specified type, otherwise False 86 | print('This expression is NOT correct.') 87 | print("Error at character # {0}. '{1}'- not closed.".format(x[-1], y)) 88 | else: 89 | print('This expression is NOT correct.') 90 | print("Error at character # {0}. '{1}'- not opened.".format(x, y)) 91 | -------------------------------------------------------------------------------- /LAB 3/task1+2.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data, next, prev): 3 | self.data = data 4 | self.next = next 5 | self.prev = prev 6 | 7 | 8 | class Doublylist: 9 | def __init__(self, a): 10 | self.head = Node(a[0], None, None) 11 | tail = self.head 12 | for x in range(1, len(a)): 13 | new_node = Node(a[x], None, tail) 14 | tail.next = new_node 15 | tail = new_node 16 | 17 | def showList(self): 18 | if self.head.next is None: 19 | print("Empty list") 20 | else: 21 | head = self.head 22 | while self.head is not None: 23 | if head.next is None: 24 | print(head.data) 25 | break 26 | else: 27 | print(head.data, "->", end=" ") 28 | head = head.next 29 | 30 | def insert(self, newElement): 31 | head = self.head 32 | new_node = Node(newElement, None, None) 33 | while new_node.next is None: 34 | if head.data == new_node.data: 35 | print("key already exists and does not insert the key") 36 | break 37 | if head.next is None: 38 | head.next = new_node 39 | new_node.prev = head 40 | break 41 | head = head.next 42 | 43 | def insert_index(self, newElement, index): 44 | condition = False 45 | if index == 0: 46 | new_node = Node(newElement, self.head, None) 47 | self.head.prev = new_node 48 | self.head = new_node 49 | else: 50 | n = self.head 51 | counter = 0 52 | while n is not None: 53 | n = n.next 54 | if condition is False: 55 | n = self.head 56 | while n is not None: 57 | counter = counter + 1 58 | if n.data == newElement: 59 | condition = True 60 | print("key already exists and does not insert the key.") 61 | if counter == index: 62 | new_node = Node(newElement, n.next, n.prev) 63 | n.next.prev = new_node 64 | n.prev.next = new_node 65 | n = n.next 66 | 67 | def remove(self, index): 68 | if self.head == None: 69 | print("list is empty") 70 | else: 71 | 72 | counter = 0 73 | head = self.head 74 | while head is not None: 75 | 76 | if index == counter: 77 | head.next.prev = head.prev 78 | head.prev.next = head.next 79 | counter = counter+1 80 | head = head.next 81 | if index > counter: 82 | print("index out of range") 83 | 84 | def removeKey(self, key): 85 | head = self.head 86 | while head is not None: 87 | if head.data == key: 88 | head.next.prev = head.prev 89 | head.prev.next = head.next 90 | head = head.next 91 | 92 | return key 93 | 94 | 95 | a = [1, 2, 3, 4, 5] 96 | 97 | mylist = Doublylist(a) 98 | mylist.insert(6) 99 | mylist.insert_index(8, 4) 100 | # mylist.remove(2) 101 | # mylist.removeKey(3) 102 | mylist.showList() 103 | -------------------------------------------------------------------------------- /LAB 2/Task 2.py: -------------------------------------------------------------------------------- 1 | # Task 2 2 | 3 | 4 | class Node: 5 | def __init__(self, value): 6 | self.value = value 7 | self.next = None 8 | 9 | 10 | class MyList: 11 | def __init__(self, a): 12 | self.head = None 13 | tail = None 14 | 15 | for x in a: 16 | new_node = Node(x) 17 | if (self.head == None): 18 | self.head = new_node 19 | tail = new_node 20 | 21 | else: 22 | tail.next = new_node 23 | tail = new_node 24 | 25 | def printList(self): 26 | temp = self.head 27 | while temp is not None: 28 | print(temp.value) 29 | temp = temp.next 30 | 31 | def showList(self): 32 | temp = self.head 33 | if temp is None: 34 | print("Empty List") 35 | else: 36 | while temp is not None: 37 | if temp.next is None: 38 | print(temp.value) 39 | break 40 | else: 41 | print(temp.value, end=" -> ") 42 | temp = temp.next 43 | 44 | def isEmpty(self): 45 | temp = self.head 46 | statement = None 47 | if temp is None: 48 | statement = True 49 | else: 50 | statement = False 51 | print(statement) 52 | 53 | def clear(self): 54 | temp = self.head 55 | while temp is not None: 56 | temp.value = None 57 | temp = temp.next 58 | self.head = None 59 | 60 | def insert(self, newElement): 61 | new_node = Node(newElement) 62 | counter = None 63 | if self.head == None: 64 | self.head = new_node 65 | tail = self.head 66 | while True: 67 | if tail.next is None: 68 | break 69 | if tail.value == newElement: 70 | counter = True 71 | tail = tail.next 72 | if counter is True: 73 | print("Key already exists") 74 | else: 75 | tail.next = new_node 76 | 77 | def insert_index(self, newElement, index): 78 | temp = self.head 79 | counter = 1 80 | while counter < index-1 and temp is not None: 81 | temp = temp.next 82 | counter = counter + 1 83 | if temp is None: 84 | print("key already exists and does not insert the key") 85 | else: 86 | new_node = Node(newElement) 87 | new_node.next = temp.next 88 | temp.next = new_node 89 | 90 | def remove(self, values): 91 | temp = None 92 | if self.head == values: 93 | self.head = self.head.next 94 | temp = str(self.head) 95 | str_value = temp+"was removed from the linked list" 96 | return str_value 97 | else: 98 | temp1 = self.head 99 | while temp1.next is not None: 100 | if temp1.next.value == values: 101 | temp = temp1.next.value 102 | break 103 | temp1 = temp1.next 104 | if temp1.next is None: 105 | print("Element doesn't exist") 106 | else: 107 | temp1.next = temp1.next.next 108 | print(temp, "was removed from the linked list") 109 | 110 | 111 | a = [10, 20, 30, 40] 112 | a2 = [] 113 | list1 = MyList(a) 114 | list2 = MyList(a2) 115 | list1.showList() 116 | list2.showList() 117 | list1.isEmpty() 118 | list2.isEmpty() 119 | list1.insert(50) 120 | list1.showList() 121 | list1.insert(10) 122 | list1.insert_index(60, 5) 123 | list1.showList() 124 | list1.remove(20) 125 | list1.showList() 126 | list1.clear() 127 | list1.showList() 128 | -------------------------------------------------------------------------------- /LAB 2/task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | 4 | class Node: 5 | def __init__(self, value): 6 | self.value = value 7 | self.next = None 8 | 9 | 10 | class MyList: 11 | def __init__(self): 12 | self.head = None 13 | self.tail = None 14 | 15 | def createlinkedlist(self, num_of_nodes): 16 | node_inputs = int(input("Enter node values: ")) 17 | x = 0 18 | tail = None 19 | self.lenght = 0 20 | while x < num_of_nodes: 21 | if x == 0: 22 | self.head = Node(node_inputs) 23 | tail = self.head 24 | else: 25 | new_node = Node(int(input("Enter node values: "))) 26 | tail.next = new_node 27 | tail = new_node 28 | x = x + 1 29 | self.lenght += 1 30 | 31 | def showlist(self): 32 | temp = self.head 33 | if temp is None: 34 | print("Empty list") 35 | else: 36 | while temp is not None: 37 | if temp.next is None: 38 | print(temp.value) 39 | break 40 | else: 41 | print(temp.value, end=" -> ") 42 | temp = temp.next 43 | 44 | def even_number(self): 45 | temp = self.head 46 | new_head = None 47 | tail = None 48 | while temp is not None: 49 | if temp.value % 2 == 0: 50 | new_node = Node(temp.value) 51 | if new_head == None: 52 | new_head = new_node 53 | tail = new_node 54 | else: 55 | tail.next = new_node 56 | tail = tail.next 57 | temp = temp.next 58 | tail = new_head 59 | while True: 60 | if tail.next is None: 61 | print(tail.value) 62 | break 63 | else: 64 | print(tail.value, end=" -> ") 65 | tail = tail.next 66 | 67 | def find_out(self, value): 68 | temp = self.head 69 | statement = None 70 | while temp.next is None: 71 | if temp.value == value: 72 | statement = True 73 | 74 | if statement == True: 75 | print("True") 76 | else: 77 | print("False") 78 | 79 | def reverse(self): 80 | temp = self.head 81 | before = None 82 | while temp != None: 83 | tail = temp.next 84 | temp.next = before 85 | before = temp 86 | temp = tail 87 | self.head = before 88 | 89 | def sort(self): 90 | self.temp = self.head 91 | self.value = None 92 | 93 | if (self.head == None): 94 | print("empty List") 95 | else: 96 | for val in range(self.lenght): 97 | if self.temp is not None: 98 | self.value = self.temp.next 99 | 100 | for val in range(self.lenght - 1): 101 | if self.value is not None: 102 | if (self.temp.value > self.value.value): 103 | cur = self.temp.value 104 | self.temp.value = self.value.value 105 | self.value.value = cur 106 | self.value = self.value.next 107 | self.temp = self.temp.next 108 | 109 | def sum(self): 110 | new = self.head 111 | total_sum = 0 112 | while new is not None: 113 | total_sum += new.value 114 | new = new.next 115 | print(total_sum) 116 | 117 | def rotate(self, rotate_position, k): 118 | if rotate_position == "left": 119 | for x in range(k): 120 | temp = self.head 121 | self.head = self.head.next 122 | temp.next = None 123 | position = self.head 124 | while position.next != None: 125 | position = position.next 126 | position.next = temp 127 | 128 | elif rotate_position == "right": 129 | for x in range(k): 130 | temp = None 131 | position = self.head 132 | while position.next.next != None: 133 | position = position.next 134 | temp = position.next 135 | position.next = None 136 | temp.next = self.head 137 | self.head = temp 138 | else: 139 | print("Invalid") 140 | 141 | 142 | list1 = MyList() 143 | list2 = list1 144 | list1.createlinkedlist(int(input("Please Enter the Number of Nods: "))) 145 | list1.showlist() 146 | list1.even_number() 147 | list1.find_out(7) 148 | list1.reverse() 149 | list1.showlist() 150 | list1.sort() 151 | list1.showlist() 152 | list1.sum() 153 | list2.rotate("left", 2) 154 | list2.showlist() 155 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DATA-STRUCTURES-CSE-220-BRACU 2 | 3 | # LAB ASSIGNMENTS 4 | 5 |

Languages:

6 |

python

7 | 8 | 9 | # LAB 1 - Arrays 📝 10 | 11 | 1. Shift Left k Cells 12 | Consider an array named source. Write a method/function named shiftLeft( source, k) that shifts all the elements of the source array to the left by 'k' positions. You must execute the method by passing an array and number of cells to be shifted. After calling the method, print the array to show whether the elements have been shifted properly. 13 | Example: 14 | source=[10,20,30,40,50,60] 15 | shiftLeft(source,3) 16 | After calling shiftLeft(source,3), printing the array should give the output as: 17 | [ 40, 50, 60, 0, 0, 0 ] 18 | 19 | Ans: ⚡ Task 1
20 | 21 | 22 | 2. Rotate Left k cells 23 | Consider an array named source. Write a method/function named rotateLeft( source, k) that rotates all the elements of the source array to the left by 'k' positions. You must execute the method by passing an array and number of cells to be shifted. After calling the method, print the array to show whether the elements have been shifted properly. 24 | Example: 25 | source=[10,20,30,40,50,60] 26 | rotateLeft(source,3) 27 | After calling rotateLeft(source,3), printing the array should give the output as: 28 | [ 40, 50, 60, 10, 20, 30] 29 | 30 | Ans: ⚡ Task 2
31 | 32 | 3. Remove an element from an array 33 | Consider an array named source. Write a method/function named remove( source, size, idx) that removes the element in index idx of the source array. You must execute the method by passing an array, its size and the idx( that is the index of the element to be removed). After calling the method, print the array to show whether the element of that particular index has been removed properly. 34 | Example: 35 | source=[10,20,30,40,50,0,0] 36 | remove(source,5,2) 37 | After calling remove(source,5,2) , printing the array should give the output as: 38 | [ 10,20,40,50,0,0,0] 39 | 40 | Ans: ⚡ Task 3
41 | 42 | 4. Remove all occurrences of a particular element from an array 43 | Consider an array named source. Write a method/function named removeAll( source, size, element) that removes all the occurrences of the given element in the source array. You must execute the method by passing an array, its size and the element to be removed. After calling the method, print the array to show whether all the occurrences of the element have been removed properly. 44 | Example: 45 | source=[10,2,30,2,50,2,2,0,0] 46 | removeAll(source,7,2) 47 | After calling removeAll(source,7,2), all the occurrences of 2 must be removed. Printing the array afterwards should give the output as: 48 | [ 10,30,50,0,0,0,0,0,0] 49 | 50 | Ans: ⚡ Task 4
51 | 52 | 5. Splitting an Array 53 | Suppose the elements of an array A containing positive integers, denote the weights in kilograms. And we have a beam balance. We want to put the weights on both pans of the balance in such a way that for some index 0 < i < A.length - 1, all values starting from A[0], A[1], upto A[ i - 1], should be on the left pan. And all values starting from A[ i ] upto A[ A.length - 1] should be on the right pan and the left and right pan should be balanced. If such an i exists, return true. Else, return false. 54 | Input: [1, 1, 1, 2, 1] Output : true 55 | Explanation: (summation of [1, 1, 1] = summation of [2,1]) 56 | Input: [2, 1, 1, 2, 1] Output: false 57 | Input: [10, 3, 1, 2, 10] Output: true 58 | Explanation: (summation of [10, 3] = summation of [1,2,10])) 59 | 60 | Ans: ⚡ Task 5
61 | 62 | 6. Array series 63 | Write a method that takes an integer value n as a parameter. Inside the method, you should create an array of length n squared (n*n) and fill the array with the following pattern. Return the array at the end and print it. 64 | If, 65 | n=2: { 0,1, 2,1 } (spaces have been added to show two distinct groups). 66 | n=3 : { 0, 0, 1, 0, 2, 1, 3, 2, 1 } ((spaces have been added to show three distinct 67 | groups). 68 | n=4 : {0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1} (spaces have been added to show four distinct groups). 69 | 70 | Ans: ⚡ Task 6
71 | 72 | 7. Max Bunch Count 73 | A "bunch" in an array is a consecutive chain of two or more adjacent elements of the same value. Write a method that returns the number of elements in the largest bunch found in the given array. 74 | Input: [1, 2, 2, 3, 4, 4, 4] Output: 3 75 | Explanation: There are two bunches here {2,2} and {4,4,4}. The largest bunch is {4,4,4} containing 3 elements so 3 is returned. 76 | Input: [1,1,2, 2, 1, 1,1,1] Output:4 77 | Explanation: There are three bunches here {1,1} and {2,2} and {1,1,1,1}. The largest bunch is {1,1,1,1} containing 4 elements so 4 is returned. 78 | 79 | Ans: ⚡ Task 7
80 | 81 | 8. Repetition 82 | Write a method that takes in an array as a parameter and counts the repetition of each element. That is, if an element has appeared in the array more than once, then its ‘repetition’ is its number of occurrences. The method returns true if there are at least two elements with the same number of ‘repetition’. Otherwise, return false. 83 | Input: {4,5,6,6,4,3,6,4} Output: True 84 | Explanation: Two numbers repeat in this array: 4 and 6. 4 has a repetition of 3, 6 has a repetition of 3. Since two numbers have the same repetition output is True. 85 | Input: {3,4,6,3,4,7,4,6,8,6,6} Output: False 86 | Explanation: Three numbers repeat in this array:3,4 and 6 .3 has a repetition of 2, 4 has a repetition of 3, 6 has a repetition of 4. Since no two numbers have the same repetition output is False. 87 | 88 | Ans: ⚡ Task 8
89 | 90 | 9. Circular Array 91 | NB: You need to implement circular arrays to solve the following problems. You cannot convert them to linear arrays. 92 | Palindrome 93 | Write a method/function that takes in a circular array, its size and start index and finds whether the elements in the array form a palindrome or not. Return true if the elements form a palindrome, otherwise, return false. 94 | Example: 95 | 96 | Input: [20,10,0,0,0,10,20,30] (start =5, size=5) Output: True. 97 | 98 | Input:[10,20,0,0,0,10,20,30] (start =5, size=5) Output: False 99 | 100 | Ans: ⚡ Task 9
101 | 102 | 10. Intersection 103 | Write a method/function that takes two circular arrays, their sizes and start indexes and returns a linear array containing the common elements between the two circular arrays. 104 | Input: 105 | Circular array 1 : [40,50,0,0,0,10,20,30] (start_1 =5, size_1 =5) 106 | [10 20 30 40 50] 107 | Circular array 2 : [10,20,5,0,0,0,0,0,5,40,15,25] (start_2=8, size_2 =7) 108 | [5 40 15 25 10 20 5] 109 | Output:[10,20,40] 110 | 111 | Ans: ⚡ Task 10
112 | 113 | 11. Suppose you have been hired to develop a musical chair game. In this game there will be 7 participants and all of them will be moving clockwise around a set of 7 chairs organized in a circular manner while music will be played in the background. You will control the music using random numbers between 0-3.If the generated random number is 1, you will stop the music and if the number of participants who are still in the game is n, the participants at position (n/2) will be eliminated. Each time a participant is eliminated, a chair will be removed and you have to print the player names who are still in the game.The game will end when there will be only one participant left. At the end of the game,display the name of the winner. 114 | [Hint: You will need to invoke a method to generate a random number between 0 115 | (inclusive) to 3 (inclusive)] 116 | 117 | Ans: ⚡ Task 11
118 | 119 | 120 | 121 | # LAB 2 - Linked list 📝 122 | 123 | Linked List 124 | Task 1: 125 | i) Create a Node class which will hold two fields i.e an integer element and a reference to the next Node. 126 | ii) Create a Linked list Abstract Data Type (ADT)named MyList.The elements in the list are Nodes consisting of an integer type key (all keys are unique) and a reference to the next node. 127 | [You are not allowed to use any global variable other than head] 128 | 129 | Ans: ⚡ Task 1
130 | 131 | Task 2: (Basic operations) 20 marks 132 | Constructor: 133 | 134 | a. MyList (int [] a) or def __init__ (self, a) (5) 135 | 136 | Pre-condition: Array cannot be empty. 137 | Post-condition:This is the default constructor of MyList class. This constructor creates a list from an array. 138 | 139 | 140 | void showList ( ) or def showList(self) (2) 141 | 142 | Precondition: None. 143 | Postcondition: Outputs the keys of the elements of the order list. If the list is empty, outputs “Empty list”. 144 | boolean isEmpty ( ) or def isEmpty(self) (1) 145 | 146 | Pre-condition: None. 147 | Post-condition: Returns true if a list is empty. Otherwise, returns false. 148 | void clear ( ) or def clear(self) (1) 149 | 150 | Pre-condition: The list is not empty. 151 | Post-condition: Removes all the elements from a list. 152 | 153 | void insert (Node newElement) or def insert(self, newElement) (3) 154 | Pre-condition: None. 155 | Post-condition: This method inserts newElement at the tail of the list. If an element with the same key as newElement already exists in the list, then it concludes the key already exists and does not insert the key. 156 | 157 | void insert (int newElement, int index) or def insert(self, newElement, index) (4) 158 | 159 | Pre-condition: The list is not empty. 160 | Post-condition: This method inserts newElement at the given index of the list. If an element with the same key as newElement value already exists in the list, then it concludes the key already exists and does not insert the key. [You must also check the validity of the index]. 161 | Node remove (int deleteKey) or def remove(self, deletekey) (4) 162 | Pre-condition: List is not empty. 163 | Post-condition: Removes the element from a list that contains the deleteKey and returns the deleted key value. 164 | 165 | Ans: ⚡ Task 2
166 | 167 | Task 3: (Advanced operations) (20 marks) 168 | 169 | Write a function to find out the even numbers that are present in the list and output another list with those numbers. (3) 170 | 171 | Sample Input 172 | Sample Output 173 | 1 -> 2 -> 5 -> 3 -> 8 174 | 2 -> 8 175 | 101 -> 120 -> 25 -> 91-> 87 -> 1 176 | 120 177 | 178 | 179 | Write a function to find out if the element is in the list or not. (3) 180 | 181 | Sample Input 182 | Sample Output 183 | 1 -> 2 -> 5 -> 3-> 8 and 7 184 | False 185 | 101 -> 120 -> 25 -> 91-> 87 -> 1 and 87 186 | True 187 | 188 | 189 | Write a function to reverse the list. [You are not allowed to create any other list] (3) 190 | 191 | Sample Input 192 | Sample Output 193 | 1 -> 2 -> 5 -> 3-> 8 194 | 8 -> 3 -> 5 -> 2 -> 1 195 | 196 | 197 | Write a function to sort the list. [You are not allowed to create any other list] (3) 198 | 199 | Sample Input 200 | Sample Output 201 | 1 -> 2 -> 5 -> 3-> 8 202 | 1 -> 2 -> 3 -> 5 -> 8 203 | 204 | 205 | 206 | 207 | Write a function that prints the sum of the values in the list. (4) 208 | 209 | Sample Input 210 | Sample Output 211 | 1 -> 2 -> 5 -> 3-> 8 212 | 19 213 | 214 | 215 | Write a function that rotates the elements of the list k times. [You are not allowed to create any other list]. (4) 216 | Sample Input 217 | Sample Output 218 | 3 -> 2 -> 5 -> 1-> 8, left, 2 219 | 5 -> 1 -> 8 -> 3 -> 2 220 | 3 -> 2 -> 5 -> 1-> 8, right, 2 221 | 1 -> 8 -> 3 -> 2 -> 5 222 | 223 | Ans: ⚡ Task 3
224 | 225 | # LAB 3 - Doubly Linked List 📝 226 | 227 | 1. i) Create a Node class which will hold three fields i.e an integer element and a reference to the next Node along with a reference to the previous Node. 228 | ii) Create a Dummy Headed Doubly Circular Linked list Abstract Data Type (ADT)named DoublyList.The elements in the list are Nodes consisting of an integer type key (all keys are unique) and a reference to the next node and a reference to the previous Node. 229 | [You are not allowed to use any global variable other than head.] 230 | 231 | 2. (Basic operations) (20 marks) 232 | Constructors: (3) 233 | a. DoublyList (int [] a) or def __intit__(self,a) 234 | 235 | Pre-condition: Array cannot be empty. 236 | Post-condition:This is the default constructor of MyList class. This constructor creates a Dummy Headed Doubly Circular Linked list list from an array. 237 | 238 | 239 | void showList ( ) or def showList(self) (1) 240 | 241 | Precondition: None. 242 | Postcondition: Outputs the keys of the elements of the order list. If the list is empty, outputs “Empty list”. 243 | 244 | void insert (Node newElement ) or def insert(self, newElement) (4) 245 | Pre-condition: None. 246 | Post-condition: This method inserts newElement at the tail of the list. If an element with the same key as newElement already exists in the list, then it concludes the key already exists and does not insert the key. 247 | 248 | void insert (int newElement, int index) or def insert(self, newElement, index) (4) 249 | 250 | Pre-condition: The list is not empty. 251 | Post-condition: This method inserts newElement at the given index of the list. If an element with the same key as newElement value already exists in the list, then it concludes the key already exists and does not insert the key. [You must also check the validity of the index]. 252 | 253 | void remove (int index) or def remove(self, index) (4) 254 | 255 | Pre-condition: The list is not empty. 256 | Post-condition: This method removes the Node at the given index of the list.[You must also check the validity of the index]. 257 | int removeKey(int deleteKey) or def removeKey(self, deletekey) (4) 258 | Pre-condition: List is not empty. 259 | Post-condition: Removes the element from a list that contains the deleteKey and returns the deleted key value. 260 | 261 | Ans: ⚡ Task 1+2
262 | 263 | # LAB 4 - Stack-Parenthesis Balancing 📝 264 | 265 | Input 266 | Your program will take an arithmetic expression as a String input. For Example: 267 | 268 | “1+2*(3/4)” 269 | 270 | “1+2*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14” 271 | 272 | “1+2*[3*3+{4–5(6(7/8/9)+10)}–11+(12*8)/{13+13}]+14” 273 | 274 | Program 275 | 276 | Your program will determine whether the open brackets (the square brackets, curly braces and the parentheses) are closed in the correct order. 277 | 278 | Outputs: 279 | 280 | Output 1 281 | 282 | 1+2*(3/4) 283 | This expression is correct. 284 | 285 | Output 2 286 | 287 | 1+2*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14 288 | This expression is NOT correct. 289 | Error at character # 10. ‘{‘- not closed. 290 | 291 | Output 3 292 | 293 | 1+2*[3*3+{4–5(6(7/8/9)+10)}–11+(12*8)/{13+13}]+14 294 | This expression is correct. 295 | 296 | Output 4 297 | 298 | 1+2]*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14 299 | This expression is NOT correct. 300 | Error at character # 4. ‘]‘- not opened. 301 | Task 1 302 | Solve the above problem using an array based stack. 303 | 304 | Ans: ⚡ Task 1
305 | 306 | Task 2 307 | Solve the above problem using a linked list based stack. 308 | 309 | Ans: ⚡ Task 2
310 | 311 | # LAB 5 - Recursion 📝 312 | 313 | Assignment Tasks: 314 | 1. [Very Easy] [5 Marks each] 315 | 316 | Implement a recursive algorithm to find factorial of n. 317 | 318 | Ans: ⚡ Task 1 A
319 | 320 | Implement a recursive algorithm to find the n-th Fibonacci number. 321 | 322 | Ans: ⚡ Task 1 B
323 | 324 | Print all the elements of a given array recursively. 325 | 326 | Ans: ⚡ Task 1 C
327 | 328 | Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared). 329 | powerN(3, 1) → 3 330 | powerN(3, 2) → 9 331 | powerN(3, 3) → 27 332 | 333 | Ans: ⚡ Task 1 D
334 | 335 | 2. [Easy] [5 Marks each] 336 | a)Implement a recursive algorithm that takes a decimal number n and converts n to its corresponding (you may return as a string) binary number. 337 | 338 | Ans: ⚡ Task 2 A
339 | 340 | b) Implement a recursive algorithm to add all the elements of a non-dummy headed singly linked linear list. Only head of the list will be given as parameter where you may assume every node can contain only integer as its element. 341 | Note: you’ll need a Singly Node class for this code. 342 | 343 | Ans: ⚡ Task 2 B
344 | 345 | c) Implement a recursive algorithm which will print all the elements of a non-dummy headed singly linked linear list in reversed order. 346 | Example: if the linked list contains 10, 20, 30 and 40, the method will print 347 | 40 348 | 30 349 | 20 350 | 10 351 | Note: you’ll need a Singly Node class for this code. 352 | 353 | Ans: ⚡ Task 2 C
354 | 355 | 3. [Medium] [8 Marks] 356 | 357 | 358 | Suppose, you have been given a non-negative integer which is the height of a ‘house of cards’. To build such a house you at-least require 8 cards. To increase the level (or height) of that house, you would require four sides and a base for each level. Therefore, for the top level, you would require 8 cards and for each of the rest of the levels below you would require 5 extra cards. If you were asked to build level one only, you would require just 8 cards. Of course, the input can be zero; in that case, you do not build a house at all. Complete the recursive method below to calculate the number of cards required to build a ‘house of cards’ of specific height given by the parameter. 359 | 360 | public int hocBuilder (int height){ 361 | // TO DO 362 | } 363 | OR 364 | def hocBuilder(height): 365 | #TO DO 366 | 367 | Ans: ⚡ Task 3
368 | 369 | 370 | 4. [Hard] [ 10 + 10 = 20 Marks] 371 | a. Print the following pattern for the given input (you must use recursion): Sample Input: 5 372 | Sample Output: 373 | 1 374 | 375 | 1 2 376 | 377 | 1 2 3 378 | 379 | 1 2 3 4 380 | 381 | 1 2 3 4 5 382 | 383 | Ans: ⚡ Task 4 A
384 | 385 | b. Print the following pattern for the given input (you must use recursion): Sample Input: 5 386 | Sample Output: 387 | 388 | 1 389 | 390 | 1 2 391 | 392 | 1 2 3 393 | 394 | 1 2 3 4 395 | 396 | 1 2 3 4 5 397 | 398 | Ans: ⚡ Task 4 B
399 | 400 | 5. [Very Hard] [12 Marks] Suppose, you are working in a company ‘X’ where your job is to calculate the profit based on their investment. 401 | If the company invests 100,000 USD or less, their profit will be based on 75,000 USD as first 25,000 USD goes to set up the business in the first place. For the first 100,000 USD, the profit margin is low: 4.5%. Therefore, for every 100 dollar they spend, they get a profit of 4.5 dollar. 402 | For an investment greater than 100,000 USD, for the first 100,000 USD (actually on 75,000 USD as 25,000 is the setup cost), the profit margin is 4.5% where for the rest, it goes up to 8%. For example, if they invest 250,000 USD, they will get an 8% profit for the 150,000 USD. In addition, from the rest 100,000 USD, 25,000 is the setup cost and there will be a 4.5% profit on the rest 75,000. Investment will always be greater or equal to 25,000 and multiple of 100. 403 | Complete the RECURSIVE methods below that take an array of integers (investments) and an iterator (always sets to ZERO(‘0’) when the method is initially called) and prints the profit for the corresponding investment. You must avoid loop and multiplication (‘*’) operator. 404 | public class FinalQ{ 405 | public static void print(int[]array,int idx){ 406 | if(idxTask 5
448 | 449 | 450 | # LAB 6 - Searching and Sorting 📝 451 | 452 | 1. Sort an array RECURSIVELY using selection sort algorithm 453 | 454 | Ans: ⚡ Task 1
455 | 456 | 2. Sort an array RECURSIVELY using insertion sort algorithm. 457 | 458 | Ans: ⚡ Task 2
459 | 460 | 3. Sort a singly linked sequential list using bubble sort algorithm. 461 | 462 | Ans: ⚡ Task 3
463 | 464 | 4. Sort a singly linked sequential list using selection sort algorithm. 465 | 466 | Ans: ⚡ Task 4
467 | 468 | 5. Sort a DOUBLY linked sequential list using insertion sort algorithm. 469 | 470 | Ans: ⚡ Task 5
471 | 472 | 6. Implement binary search algorithm RECURSIVELY. 473 | 474 | Ans: ⚡ Task 6
475 | 476 | 7. Implement a recursive algorithm to find the n-th Fibonacci number using memoization. 477 | 478 | Ans: ⚡ Task 7
479 | 480 | # LAB 7 - Key Index Searching & Sorting, Hashing 📝 481 | 482 | Task 1 on Key index Searching & Sorting (25 marks) 483 | Create a KeyIndex class with the following properties : 484 | 485 | Fields: 486 | 487 | int [ ] k; 488 | 489 | Description 490 | An array of integers. 491 | 492 | Note: You may maintain another global variable(java)/instance variable(python) if needed (but you can’t use more than one). 493 | 494 | Constructor:(10 marks) 495 | KeyIndex(int [ ]a) 496 | 497 | 498 | Description: 499 | 500 | 501 | This constructor takes an array of integers a and populates array k with the element in a as indices into k. 502 | 503 | Note: make sure the build-up of your array k supports negative and non-distinct integers. 504 | Methods: 505 | 506 | search (int val) (5 marks) 507 | Description: 508 | This method searches for the value val within the array and returns true if found or false otherwise. 509 | 510 | sort () (10 marks) 511 | Description: 512 | 513 | This method will return the sorted form of the array that had been passed into the constructor. 514 | NOTE: Create a tester class or write tester statements to check whether the methods in your KeyIndex class work properly. You need to submit both the classes as a part of your assignment. (5 marks) 515 | 516 | 517 | 518 | Ans: ⚡ Task 1
519 | 520 | Task 2 on Hashing (15 marks) 521 | 522 | Given an array containing Strings, you need to write a code to store them in a hashtable. Assume that the Strings contain a combination of capital letters and numbers, and the String array will contain no more than 9 values.Use the hash function to be the 523 | (total number of consonants*24 + summation of the digits) %9. In case of a collision, use linear probing. 524 | For a String “ST1E89B8A32”, it’s hash function will produce the value=(3*24+(1+8+9+8+3+2))%9=4, hence it will be stored in index 4 of the hash table. 525 | 526 | Marks distribution: 527 | Hash function calculation, method properly written =10 marks 528 | Linear probing properly implemented= 5 marks 529 | 530 | Ans: ⚡ Task 2
531 | 532 | 533 | # LAB 8 - Trees and Graph 📝 534 | 535 | 536 | 1. RECURSIVELY calculate the height of a tree. 537 | 538 | Ans: ⚡ Task 1
539 | 540 | 2. RECURSIVELY calculate the level of a Node in a tree. 541 | 542 | Ans: ⚡ Task 2
543 | 544 | 3. Print elements of all the Nodes of a tree using Pre-order Traversal. 545 | 546 | Ans: ⚡ Task 3
547 | 548 | 4. Print elements of all the Nodes of a tree using In-order Traversal. 549 | 550 | Ans: ⚡ Task 4
551 | 552 | 5. Print elements of all the Nodes of a tree using Post-order Traversal. 553 | 554 | Ans: ⚡ Task 5
555 | 556 | 6. Write a method which will evaluate whether two trees are exactly same or not. 557 | 558 | Ans: ⚡ Task 6
559 | 560 | 7. Write a method which will return a copy (new tree) of a given tree. 561 | 562 | Ans: ⚡ Task 7 563 | 564 |
565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | --------------------------------------------------------------------------------