BST Successor Search #120
Replies: 2 comments
-
`######################################################### CODE INSTRUCTIONS:1) The method findInOrderSuccessor you're askedto implement is located at line 30.2) Use the helper code below to implement it.3) In a nutshell, the helper code allows you toto build a Binary Search Tree.4) Jump to line 88 to see an example for how thehelper code is used to test findInOrderSuccessor.######################################################### A nodeclass Node: Constructor to create a new nodedef init(self, key): A binary search treeclass BinarySearchTree: Constructor to create a new BSTdef init(self): def find_in_order_successor(self, inputNode):
Given a binary search tree and a number, inserts anew node with the given number in the correct placein the tree. Returns the new root pointer which thecaller should then use(the standard trick to avoidusing reference parameters)def insert(self, key):
Return a reference to a node in the BST by its key.Use this method when you need a node to test yourfindInOrderSuccessor function ondef getNodeByKey(self, key):
######################################### Driver program to test above function######################################### Create a Binary Search Treebst = BinarySearchTree() Get a reference to the node whose key is 9test = bst.getNodeByKey(5) Find the in order successor of testsucc = bst.find_in_order_successor(test) Print the key of the successor nodeif succ is not None: |
Beta Was this translation helpful? Give feedback.
-
`def find_in_order_successor(self, inputNode): inputNode has no right childif inputNode.right is None: inputNode has right childelse: |
Beta Was this translation helpful? Give feedback.
-
In a Binary Search Tree (BST), an Inorder Successor of a node is defined as the node with the smallest key greater than the key of the input node (see examples below). Given a node inputNode in a BST, you’re asked to write a function findInOrderSuccessor that returns the Inorder Successor of inputNode. If inputNode has no Inorder Successor, return null.
Explain your solution and analyze its time and space complexities.
In this diagram, the inorder successor of 9 is 11 and the inorder successor of 14 is 20.
Example:
In the diagram above, for inputNode whose key = 11
Your function would return:
The Inorder Successor node whose key = 12
Constraints:
[time limit] 5000ms
[input] Node inputNode
[output] Node
Beta Was this translation helpful? Give feedback.
All reactions