class PrimeQueue: utility = Utility() queue = QueueLinkedList() var_list = [] non_annagram = [] index = 0 prime_arr = utility.primenumber(1000) for x in range(0, len(prime_arr) - 1): for y in range(x + 1, len(prime_arr)): if (prime_arr[x] != None) and (prime_arr[y] != None): flag = utility.annagram(prime_arr[x], prime_arr[y]) if (flag): var_list.append(prime_arr[x]) var_list.append(prime_arr[y]) print("\nAnnagrams : ") for x in range(0, len(var_list) - 1, 2): print(str(var_list[x]) + " " + str(var_list[x + 1])), queue.add_rear(var_list[x]) queue.add_rear(var_list[x + 1]) print("\n") for x in range(0, len(var_list) - 1): status = utility.check(prime_arr[x], var_list) if (status): non_annagram.append(prime_arr[x]) print("After adding in queue : ") queue.display_que()
def levelorderTraversal(rootNode): if not rootNode: return else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() print(root.value.data) if root.value.leftChild is not None: q.enqueue(root.value.leftChild) if root.value.rightChild is not None: q.enqueue(root.value.rightChild)
def level_order(root): if not root: return else: new_q = Q.Queue() new_q.enqueue(root) while not (new_q.isEmpty()): r = new_q.dequeue() print(r.value.data) if r.value.left is not None: new_q.enqueue(r.value.left) if r.value.right is not None: new_q.enqueue(r.value.right)
def levelOrderTraversal(rootNode): if not rootNode: return else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() print(root.value.data) if root.value.leftChild is not None: customQueue.enqueue(root.value.leftChild) if root.value.rightChild is not None: customQueue.enqueue(root.value.rightChild)
def getDeepestNode(rootNode): if not rootNode: return else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if (root.leftChild is not None): customQueue.enqueue(root.leftChild) if (root.rightChild is not None): customQueue.enqueue(root.rightChild) deepestNode = root.data return deepestNode
def getDeepestNode(rootNode): if not rootNode: return else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() if root.value.leftChild is not None: q.enqueue(root.value.leftChild) if root.value.rightChild is not None: q.enqueue(root.value.rightChild) deepestNode = root.value return deepestNode
def levelorderTraversal(rootNode): if not rootNode: return else: q = queue.Queue() q.enqueue(rootNode) while not (q.isEmpty()): root = q.dequeue() # The value parameter is a part of the Node class print(root.value.data) if root.value.leftChild is not None: q.enqueue(root.value.leftChild) if root.value.rightChild is not None: q.enqueue(root.value.rightChild)
def searchBT(rootNode, nodeValue): #time and space is O(N) if not rootNode: return "The Binary Tree doesn't exist." else: customQueue = q.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value.data == nodeValue: return "Successfully found the node!" if (root.value.leftChild is not None): customQueue.enqueue(root.value.leftChild) if (root.value.rightChild is not None): customQueue.enqueue(root.value.rightChild) return "Value not found!"
def searchTree(rootNode, nodeValue): if not rootNode: return "BT DNE" else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() if root.value.data == nodeValue: return "Node Exists" if root.value.leftChild is not None: q.enqueue(root.value.leftChild) if root.value.rightChild is not None: q.enqueue(root.value.rightChild) return "Node DNE"
def search_node(root, val): if not root: return "The tree doesn't exist!" else: new_q = Q.Queue() new_q.enqueue(root) while not new_q.isEmpty(): r = new_q.dequeue() if r.value.data == val: return "FOUND!" if r.value.left is not None: new_q.enqueue(r.value.left) if r.value.right is not None: new_q.enqueue(r.value.right) return "THERE'S SUCH NODE IN THE TREE"
def searchBT(rootNode, nodeValue): if not rootNode: return "The BT does not exist" else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value.data == nodeValue: return "Success" if (root.value.leftChild is not None): customQueue.enqueue(root.value.leftChild) if (root.value.rightChild is not None): customQueue.enqueue(root.value.rightChild) return "Not found"
def insert_node(root, new_node): if not root: root = new_node else: new_q = Q.Queue() new_q.enqueue(root) while not new_q.isEmpty(): r = new_q.dequeue() if r.value.left is not None: new_q.enqueue(r.value.left) else: r.value.left = new_node return "Successfully inserted" if r.value.right is not None: new_q.enqueue(r.value.right) else: r.value.right = new_node return "Successfully inserted"
def deleteNodeBT(rootNode, node): #time and space is O(N) if not rootNode: return "The Binary Tree doesn't exist." else: customQueue = q.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if (root.value.data == node): dNode = getDeepestNode(rootNode) root.value.data = dNode.data deleteDeepestNode(rootNode, dNode) return "The node has been successfully deleted." if (root.value.leftChild is not None): customQueue.enqueue(root.value.leftChild) if (root.value.rightChild is not None): customQueue.enqueue(root.value.rightChild) return "Failed to delete the node!"
def insertNode(rootNode, newNode): if not rootNode: rootNode = newNode else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() if root.value.leftChild is not None: q.enqueue(root.value.leftChild) else: root.value.leftChild = newNode return "Node Inserted" if root.value.rightChild is not None: q.enqueue(root.value.rightChild) else: root.value.rightChild = newNode return "Node Inserted"
def insertNodeBT(rootNode, newNode): if not rootNode: rootNode = newNode else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value.leftChild is not None: customQueue.enqueue(root.value.leftChild) else: root.value.leftChild = newNode return "Successfully Inserted" if root.value.rightChild is not None: customQueue.enqueue(root.value.rightChild) else: root.value.rightChild = newNode return "Successfully Inserted"
def deleteNode(rootNode, node): if not rootNode: return "BT DNE" else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() if root.value.data == node: dNode = getDeepestNode(rootNode) root.value.data = dNode.data deleteDeepestNode(rootNode, dNode) return "Node Deleted" if root.value.leftChild is not None: q.enqueue(root.value.leftChild) if root.value.rightChild is not None: q.enqueue(root.value.rightChild) return "Deletion Failed"
def deleteNodeBT(rootNode, node): if not rootNode: return "The BT does not exist" else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value.data == node: dNode = getDeepestNode(rootNode) root.value.data = dNode.data deleteDeepestNode(rootNode, dNode) return "The node has been successfully deleted" if (root.value.leftChild is not None): customQueue.enqueue(root.value.leftChild) if (root.value.rightChild is not None): customQueue.enqueue(root.value.rightChild) return "Failed to delete"
def insertNodeBT(rootNode, newNode): #time and space is O(N) #base if not rootNode: rootNode = newNode #use level-order traversal and insert newnode in left child of 1st vacant node of that level else: customQueue = q.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value.leftChild is not None: customQueue.enqueue(root.value.leftChild) else: root.value.leftChild = newNode return "Successfully inserted newnode!" if root.value.rightChild is not None: customQueue.enqueue(root.value.rightChild) else: root.value.rightChild = newNode return "Successfully inserted newnode!"
class CalenderQueue: utility = Utility() que = QueueLinkedList() que2 = Deque() print("enter a month : ") var_month = utility.input_int_data() print("enter a year : ") var_year = utility.input_int_data() if (var_month <= 0) or (var_year <= 0) or (var_month > 12): print("check the entered input") else: months_array = [ "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] days_array = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if (var_month == 2) and (utility.is_leap_year(var_year)): days_array[var_month] = 29 print("\t\t---------------------" + str(months_array[var_month]) + " " + str(var_year) + "---------------------\n") week_arr = ["S", "M", "TU", "W", "TH", "F", "SA"] for x in range(0, len(week_arr)): value = format(str(week_arr[x]), " >8s") que.add_rear(value) que.add_rear("\n") var_day = utility.day_of_week(1, var_month, var_year) for x in range(0, var_day): value = format("", ">8s") que.add_rear(value) for x in range(1, days_array[var_month] + 1): value = format(str(x), " >8s") que.add_rear(value) que2.add_rear(value) if ((x + var_day) % 7 == 0) or (x == days_array[var_month]): que.add_rear("\n") que.display_que()
def insertNodeBT(rootNode, newNode): if not rootNode: rootNode = newNode return "node added to the tree" else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.leftChild is not None: customQueue.enqueue(root.leftChild) else: newLeftChild = TreeNode(newNode) root.leftChild = newLeftChild return "node added to the tree" if root.rightChild is not None: customQueue.enqueue(root.rightChild) else: newRightChild = TreeNode(newNode) root.rightChild = newRightChild return "node added to the tree"
def deleteDeepestNode(rootNode, deepestNode): if not rootNode: return else: q = queue.Queue() q.enqueue(rootNode) while not q.isEmpty(): root = q.dequeue() if root.value is deepestNode: root.value = None return if root.value.rightChild: if root.value.rightChild is deepestNode: root.value.rightChild = None return else: q.enqueue(root.value.rightChild) if root.value.leftChild: if root.value.leftChild is deepestNode: root.value.leftChild = None return else: q.enqueue(root.value.leftChild)
def deleteDeepestNode(rootNode, dNode): if not rootNode: return else: customQueue = queue.Queue() customQueue.enqueue(rootNode) while not (customQueue.isEmpty()): root = customQueue.dequeue() if root.value is dNode: root.value = None return if root.value.rightChild: if root.value.rightChild is dNode: root.value.rightChild = None return else: customQueue.enqueue(root.value.rightChild) if root.value.leftChild: if root.value.leftChild is dNode: root.value.leftChild = None return else: customQueue.enqueue(root.value.leftChild)