Question

## Codes must be in Python ## In a binary search tree What is worst case time complexity of the binary_search function? Provide an example binary search tree that exhibits worst case running time of...

## Codes must be in Python ##

In a binary search tree

  1. What is worst case time complexity of the binary_search function?
  2. Provide an example binary search tree that exhibits worst case running time of binary_search function
  3. Write a function that prints elements in binary search tree in order
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Python program

# worst case time complexity of the binary_search function = O(n) n is number of elements in BST

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

def insert_element(root,node):
if root is None:
root = node
else:
if root.val < node.val:
if root.right is None:
root.right = node
else:
insert_element(root.right, node)
else:
if root.left is None:
root.left = node
else:
insert_element(root.left, node)
  
def binary_search(root,key):
print(root.val)
if root is None or root.val == key:
return root
  
if root.val < key:
return binary_search(root.right,key)
return binary_search(root.left,key)
  

r = Node(10)
insert_element(r,Node(20))
insert_element(r,Node(30))
insert_element(r,Node(40))
insert_element(r,Node(50))
insert_element(r,Node(60))
insert_element(r,Node(80))
binary_search(r,80)

Add a comment
Know the answer?
Add Answer to:
## Codes must be in Python ## In a binary search tree What is worst case time complexity of the binary_search function? Provide an example binary search tree that exhibits worst case running time of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT