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: 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 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 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 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 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 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 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 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 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 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 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 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!"
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)