Question

In: Computer Science

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 its purpose.Illustrate the performance of the sortTraversal method

class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
  
class BSTree():
def __init__(self, rootdata):
self.root = Node(rootdata)
  
def insert(self, data, cur_node):
if data < cur_node.data:
if cur_node.left == None:
cur_node.left = Node(data)
else:
self.insert(data, cur_node.left)
  
elif data > cur_node.data:
if cur_node.right == None:
cur_node.right = Node(data)
else:
self.insert(data, cur_node.right)
else:
print("Duplicate value!")

Solutions

Expert Solution

# Welcome to sorted BST tree traversal code using Python 3

class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.data = key

/* We are using Node class to initialize the attributes of each node
 init method is a constructor here.
 Constructors are used to initialize the state of the object.
*/

class BSTree():
    def __init__(self, rootdata):
        self.root = Node(rootdata)

/* Above class BSTree is used to initialize the root node, the constructor inside is calling "Node" class and that will instantiate an object of type "Node" with rootdata as node data and left and right attributes with None
*/

def insert(data, cur_node):
    if data < cur_node.data:
        if cur_node.left == None:
            cur_node.left = Node(data)
        else:
            insert(data, cur_node.left)
    elif data > cur_node.data:
        if cur_node.right == None:
            cur_node.right = Node(data)
        else:
            insert(data, cur_node.right)
    else:
        print("Duplicate value!")

/* Above method will insert data by comparing the values with the current node value, if the new value is smaller than the current node it will try to insert the new data to its left. If left attribute of current node is None, it will insert the new node there else it will invoke a recursive call to same method with left of current node as current node value.

Similarly, if the new node value is greater than the current node value it will try to insert the new data to its right. If right attribute of current node is None, it will insert the new node there else it will invoke a recursive call to same method with right of current node as current node value.

*/

def sortTraversal(root,ordering):
    if ordering=='ascending':
        if root:
            sortTraversal(root.left,ordering)
            print(root.data)
            sortTraversal(root.right,ordering)
    elif ordering=='descending':
        if root:
            sortTraversal(root.right,ordering)
            print(root.data)
            sortTraversal(root.left,ordering)
​​​​​​​
/* Above method is used to print the BST in sorted order based on input. Method includes if-else block that will be printing the data based on 'ordering' value input.

If input is 'ascending' it will start with the leftmost node, covers the left subtree first, followed by root and finally the right subtree.
This method of traversal is also called Inorder traversal.

If input is 'descending', it will start in reverse way, it will start with rightmost node of tree, covers the right subtree, followed by root and then left subtree.
*/


if __name__ == '__main__':
    t=BSTree(10)
    arr=[5,2,3,1,7,13,12,15]
    for i in arr:
        insert(i,t.root)
    order = input("Please choose sorting order ascending/descending: ")
    sortTraversal(t.root,order)

/* In the above main() method, we are instantiating an object of BSTree class and it will initialize the root node with 10 as data value.
We are using an array to insert data in BST using insert method with array elements(i) and root node(t.root) as parameters.
Once BST is generated, we are taking user input for sorting order and then calling sortTraversal with root node and order values as parameters
*/ 

BST tree traversal needs to go through each and every node of the tree and in that case we can call the time complexity will be minimum of O(n).


Related Solutions

Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: 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...
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...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
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.
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: 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...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT