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.value.leftChild is not None): customqueue.enqueue(root.value.leftChild) if (root.value.rightChild is not None): customqueue.enqueue(root.value.rightChild) deepestNode = root.value return deepestNode
def searchBT(rootNode, nodevalue): if not rootNode: return " the bt does not exists" 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 "Unsuccess"
def deleteNodeBT(rootNode, node): if not rootNode: return "root node 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 "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"
def insertNode(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 if (root.value.rightChild is not None): customqueue.enqueue(root.value.rightChild) else: root.value.rightChild = newNode return
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 if root.value.rightChild: if root.value.rightChild is dNode: root.value.rightChild = None else: customqueue.enqueue(root.value.rightChild) if root.value.leftChild: if root.value.leftChild is dNode: root.value.leftChild = None else: customqueue.enqueue(root.value.leftChild)