Question

In: Computer Science

Creat a python method for a binary tree class that has an inputted lookup value and...

Creat a python method for a binary tree class that has an inputted lookup value and will output if the lookup values exist within the Binary Tree by returning True or False. Exists within the binary tree -True. No exist within the binary tree - false.Test the method in your code

Solutions

Expert Solution

Following is the code snippet to Search a node in Binary Tree. In following code ifNodeExists is the required method, see comments for explanation. I have also added screen shot of my code execution.


# Function to create a new tree node 
class newNode: 

        # Constructor for newNode
        def __init__(node, nodeValue): 
                node.nodeValue = nodeValue 
                node.left = None
                node.right = None

# Function to check if the given node exists in it
# input parameter node is root node of binary tree
# input paramenter key means value to look for.
 
def ifNodeExists(node, key):

        if (node == None): 
                return False

        if (node.nodeValue == key): 
                return True

        # then recursively call ifNodeExists on left tree
        res1 = ifNodeExists(node.left, key) 
        # node found
        if res1:
                return True

        # Node not found in left node so now
        # recursively call ifNodeExists on right tree
        res2 = ifNodeExists(node.right, key) 

        return res2
        
# Main code
if __name__ == '__main__':
        root = newNode(0) 
        root.left = newNode(10) 
        root.left.left = newNode(30) 
        root.left.left.left = newNode(70) 
        root.left.right = newNode(40) 
        root.left.right.left = newNode(80) 
        root.left.right.right = newNode(90) 
        root.right = newNode(20) 
        root.right.left = newNode(50) 
        root.right.right = newNode(60) 
        
        # value to look for
        key = 20
        
        # adding messages as given in question
        if (ifNodeExists(root, key)): 
                print("Exists within the binary tree - True.")
        else:
                print("No exist within the binary tree - False.")


Related Solutions

Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
Python: Create two Binary Tree class methods that can return the maximum value within a Binary...
Python: Create two Binary Tree class methods that can return the maximum value within a Binary Tree and the minimum value within a Binary Tree. Test the methods in your code Example syntax to call these methods: MyTree.Max() MyTree.Min()
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose.Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 , 991)...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose. Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 ,...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT