0% found this document useful (0 votes)
2 views3 pages

Trees

The document contains Python code for various functions related to binary search trees (BST). It includes methods for finding the minimum value, performing in-order traversal, finding the k-th maximum value, retrieving ancestors of a node, calculating the height of the tree, and finding k nodes at a specific depth. Each function is defined with comments indicating where to write the code and provides a structure for implementing BST operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Trees

The document contains Python code for various functions related to binary search trees (BST). It includes methods for finding the minimum value, performing in-order traversal, finding the k-th maximum value, retrieving ancestors of a node, calculating the height of the tree, and finding k nodes at a specific depth. Each function is defined with comments indicating where to write the code and provides a structure for implementing BST operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The Node and BST classes remain unchanged for all the questions.

Just copy paste


these after the BST class.

Q1:

def find_min(root):
#Write your code from here
current = BST.root

# loop down to find the leftmost leaf


while(current.left_child is not None):
current = current.left_child

return current.val

Q2:

def in_order_traverse(node, tree):


# Helper recursive function to traverse the tree inorder
if node is not None: # check if node exists
in_order_traverse(node.left_child, tree) # traverse left sub-tree
if len(tree) is 0:
# Append if empty tree
tree.append(node.val)
elif tree[-1] is not node.val:
# Ensure not a duplicate
tree.append(node.val) # add current node value
in_order_traverse(node.right_child, tree)
return tree

def find_kth_max(root, k):


#Write your code
tree = []
return in_order_traverse(BST.root, tree)[-k]
Q3:

def find_ancestors_helper(root, k, ancestors):


#Write your code here
if root == None:
return False

if root.val == k:
return True

# If target is present in either left or right subtree


# of this node, then print this node
if (find_ancestors_helper(root.left_child, k, ancestors) or
find_ancestors_helper(root.right_child, k, ancestors)):
ancestors.append(root.val)

return ancestors

def find_ancestors(root,k):
ancestors = []
return find_ancestors_helper(root, k, ancestors)

Q4:

def find_height(root):
# Write your code here
if root is None:
return -1;
left_height = find_height(root.left_child);
right_height = find_height(root.right_child);
if left_height > right_height:
return left_height + 1
else:
return right_height + 1

Q5:

def find_k(root, k, res):


if root is None: # return if root does not exist
return
if k == 0:
res.append(root.val) # append as root is kth node
else:
# check recursively in both sub-tree for kth node
find_k(root.left_child, k - 1, res)
find_k(root.right_child, k - 1, res)
return res

def find_k_nodes(root, k):


res = []
#Write your code from here
return find_k(root, k, res)

You might also like