The Binary Search Tree(BST) provided below: {# Class to represent Tree node class Node: # A function to create a new node def __init__(self, key): self.data = key self.left = None self.right = None root = Node(4) root.left = Node(2) root.right = Node(6) root.left.left = Node(1) root.left.right = Node(3) root.right.left = Node(5) root.right.right = Node(7) } Write a complete Python program to only return the third smallest element in the Binary Search Tree. The value of the third smallest element is ”3”. You may choose either a recursive or an iterative solution. You are NOT allowed to actively maintain an extra data-structure for storage of values of the tree elements to perform a linear scan later to return the required value. HINT: A certain BST Traversal is ordered.
The Binary Search Tree(BST) provided below:
{# Class to represent Tree node
class Node:
# A function to create a new node
def __init__(self, key):
self.data = key
self.left = None
self.right = None
root = Node(4)
root.left = Node(2)
root.right = Node(6)
root.left.left = Node(1)
root.left.right = Node(3)
root.right.left = Node(5)
root.right.right = Node(7)
}
Write a complete Python program to only return the third smallest element in the Binary Search Tree. The value of the third smallest element
is ”3”. You may choose either a recursive or an iterative solution. You
are NOT allowed to actively maintain an extra data-structure for storage
of values of the tree elements to perform a linear scan later to return the
required value.
HINT: A certain BST Traversal is ordered.
Step by step
Solved in 5 steps with 3 images