Exemple #1
0
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)
Exemple #3
0
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)
Exemple #4
0
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
Exemple #5
0
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)
Exemple #6
0
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
Exemple #7
0
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"
Exemple #8
0
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"
Exemple #9
0
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!"
Exemple #10
0
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"
Exemple #11
0
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"
Exemple #12
0
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!"
Exemple #13
0
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"
Exemple #14
0
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"
Exemple #15
0
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"
Exemple #16
0
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"
Exemple #17
0
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!"
Exemple #18
0
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"
Exemple #19
0
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)
Exemple #20
0
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)