findInOrderSuccessor in a Binary Search Tree python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I would like to ask code review for the following problem that I solved.



https://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/



#########################################################
# CODE INSTRUCTIONS: #
# 1) The method findInOrderSuccessor you're asked #
# to implement is located at line 30. #
# 2) Use the helper code below to implement it. #
# 3) In a nutshell, the helper code allows you to #
# to build a Binary Search Tree. #
# 4) Jump to line 88 to see an example for how the #
# helper code is used to test findInOrderSuccessor. #
#########################################################


# A node
class Node:
# Constructor to create a new node
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.parent = None


# A binary search tree
class BinarySearchTree:
# Constructor to create a new BST
def __init__(self):
self.root = None

def find_in_order_successor(self, inputNode):
# find the node in right tree
# if no right tree
# try to find in parent
if inputNode.right:
inputNode = inputNode.right
while inputNode.left:
inputNode = inputNode.left
return inputNode
while inputNode.parent and inputNode.parent.right == inputNode:
inputNode = inputNode.parent
return inputNode.parent

# Given a binary search tree and a number, inserts a
# new node with the given number in the correct place
# in the tree. Returns the new root pointer which the
# caller should then use(the standard trick to avoid
# using reference parameters)
def insert(self, key):

# 1) If tree is empty, create the root
if (self.root is None):
self.root = Node(key)
return

# 2) Otherwise, create a node with the key
# and traverse down the tree to find where to
# to insert the new node
currentNode = self.root
newNode = Node(key)
while (currentNode is not None):

if (key < currentNode.key):
if (currentNode.left is None):
currentNode.left = newNode;
newNode.parent = currentNode;
break
else:
currentNode = currentNode.left;
else:
if (currentNode.right is None):
currentNode.right = newNode;
newNode.parent = currentNode;
break
else:
currentNode = currentNode.right;

# Return a reference to a node in the BST by its key.
# Use this method when you need a node to test your
# findInOrderSuccessor function on
def getNodeByKey(self, key):

currentNode = self.root
while (currentNode is not None):
if (key == currentNode.key):
return currentNode

if (key < currentNode.key):
currentNode = currentNode.left
else:
currentNode = currentNode.right

return None


#########################################
# Driver program to test above function #
#########################################

# Create a Binary Search Tree
bst = BinarySearchTree()
bst.insert(20)
bst.insert(9);
bst.insert(25);
bst.insert(5);
bst.insert(12);
bst.insert(11);
bst.insert(14);

# Get a reference to the node whose key is 9 ???
test = bst.getNodeByKey(9) # testing with 9?

# Find the in order successor of test
succ = bst.find_in_order_successor(test)

# Print the key of the successor node
if succ is not None:
print("nInorder Successor of %d is %d " % (test.key, succ.key))
else:
print("nInorder Successor doesn't exist")






share|improve this question



























    up vote
    0
    down vote

    favorite












    I would like to ask code review for the following problem that I solved.



    https://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/



    #########################################################
    # CODE INSTRUCTIONS: #
    # 1) The method findInOrderSuccessor you're asked #
    # to implement is located at line 30. #
    # 2) Use the helper code below to implement it. #
    # 3) In a nutshell, the helper code allows you to #
    # to build a Binary Search Tree. #
    # 4) Jump to line 88 to see an example for how the #
    # helper code is used to test findInOrderSuccessor. #
    #########################################################


    # A node
    class Node:
    # Constructor to create a new node
    def __init__(self, key):
    self.key = key
    self.left = None
    self.right = None
    self.parent = None


    # A binary search tree
    class BinarySearchTree:
    # Constructor to create a new BST
    def __init__(self):
    self.root = None

    def find_in_order_successor(self, inputNode):
    # find the node in right tree
    # if no right tree
    # try to find in parent
    if inputNode.right:
    inputNode = inputNode.right
    while inputNode.left:
    inputNode = inputNode.left
    return inputNode
    while inputNode.parent and inputNode.parent.right == inputNode:
    inputNode = inputNode.parent
    return inputNode.parent

    # Given a binary search tree and a number, inserts a
    # new node with the given number in the correct place
    # in the tree. Returns the new root pointer which the
    # caller should then use(the standard trick to avoid
    # using reference parameters)
    def insert(self, key):

    # 1) If tree is empty, create the root
    if (self.root is None):
    self.root = Node(key)
    return

    # 2) Otherwise, create a node with the key
    # and traverse down the tree to find where to
    # to insert the new node
    currentNode = self.root
    newNode = Node(key)
    while (currentNode is not None):

    if (key < currentNode.key):
    if (currentNode.left is None):
    currentNode.left = newNode;
    newNode.parent = currentNode;
    break
    else:
    currentNode = currentNode.left;
    else:
    if (currentNode.right is None):
    currentNode.right = newNode;
    newNode.parent = currentNode;
    break
    else:
    currentNode = currentNode.right;

    # Return a reference to a node in the BST by its key.
    # Use this method when you need a node to test your
    # findInOrderSuccessor function on
    def getNodeByKey(self, key):

    currentNode = self.root
    while (currentNode is not None):
    if (key == currentNode.key):
    return currentNode

    if (key < currentNode.key):
    currentNode = currentNode.left
    else:
    currentNode = currentNode.right

    return None


    #########################################
    # Driver program to test above function #
    #########################################

    # Create a Binary Search Tree
    bst = BinarySearchTree()
    bst.insert(20)
    bst.insert(9);
    bst.insert(25);
    bst.insert(5);
    bst.insert(12);
    bst.insert(11);
    bst.insert(14);

    # Get a reference to the node whose key is 9 ???
    test = bst.getNodeByKey(9) # testing with 9?

    # Find the in order successor of test
    succ = bst.find_in_order_successor(test)

    # Print the key of the successor node
    if succ is not None:
    print("nInorder Successor of %d is %d " % (test.key, succ.key))
    else:
    print("nInorder Successor doesn't exist")






    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to ask code review for the following problem that I solved.



      https://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/



      #########################################################
      # CODE INSTRUCTIONS: #
      # 1) The method findInOrderSuccessor you're asked #
      # to implement is located at line 30. #
      # 2) Use the helper code below to implement it. #
      # 3) In a nutshell, the helper code allows you to #
      # to build a Binary Search Tree. #
      # 4) Jump to line 88 to see an example for how the #
      # helper code is used to test findInOrderSuccessor. #
      #########################################################


      # A node
      class Node:
      # Constructor to create a new node
      def __init__(self, key):
      self.key = key
      self.left = None
      self.right = None
      self.parent = None


      # A binary search tree
      class BinarySearchTree:
      # Constructor to create a new BST
      def __init__(self):
      self.root = None

      def find_in_order_successor(self, inputNode):
      # find the node in right tree
      # if no right tree
      # try to find in parent
      if inputNode.right:
      inputNode = inputNode.right
      while inputNode.left:
      inputNode = inputNode.left
      return inputNode
      while inputNode.parent and inputNode.parent.right == inputNode:
      inputNode = inputNode.parent
      return inputNode.parent

      # Given a binary search tree and a number, inserts a
      # new node with the given number in the correct place
      # in the tree. Returns the new root pointer which the
      # caller should then use(the standard trick to avoid
      # using reference parameters)
      def insert(self, key):

      # 1) If tree is empty, create the root
      if (self.root is None):
      self.root = Node(key)
      return

      # 2) Otherwise, create a node with the key
      # and traverse down the tree to find where to
      # to insert the new node
      currentNode = self.root
      newNode = Node(key)
      while (currentNode is not None):

      if (key < currentNode.key):
      if (currentNode.left is None):
      currentNode.left = newNode;
      newNode.parent = currentNode;
      break
      else:
      currentNode = currentNode.left;
      else:
      if (currentNode.right is None):
      currentNode.right = newNode;
      newNode.parent = currentNode;
      break
      else:
      currentNode = currentNode.right;

      # Return a reference to a node in the BST by its key.
      # Use this method when you need a node to test your
      # findInOrderSuccessor function on
      def getNodeByKey(self, key):

      currentNode = self.root
      while (currentNode is not None):
      if (key == currentNode.key):
      return currentNode

      if (key < currentNode.key):
      currentNode = currentNode.left
      else:
      currentNode = currentNode.right

      return None


      #########################################
      # Driver program to test above function #
      #########################################

      # Create a Binary Search Tree
      bst = BinarySearchTree()
      bst.insert(20)
      bst.insert(9);
      bst.insert(25);
      bst.insert(5);
      bst.insert(12);
      bst.insert(11);
      bst.insert(14);

      # Get a reference to the node whose key is 9 ???
      test = bst.getNodeByKey(9) # testing with 9?

      # Find the in order successor of test
      succ = bst.find_in_order_successor(test)

      # Print the key of the successor node
      if succ is not None:
      print("nInorder Successor of %d is %d " % (test.key, succ.key))
      else:
      print("nInorder Successor doesn't exist")






      share|improve this question













      I would like to ask code review for the following problem that I solved.



      https://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/



      #########################################################
      # CODE INSTRUCTIONS: #
      # 1) The method findInOrderSuccessor you're asked #
      # to implement is located at line 30. #
      # 2) Use the helper code below to implement it. #
      # 3) In a nutshell, the helper code allows you to #
      # to build a Binary Search Tree. #
      # 4) Jump to line 88 to see an example for how the #
      # helper code is used to test findInOrderSuccessor. #
      #########################################################


      # A node
      class Node:
      # Constructor to create a new node
      def __init__(self, key):
      self.key = key
      self.left = None
      self.right = None
      self.parent = None


      # A binary search tree
      class BinarySearchTree:
      # Constructor to create a new BST
      def __init__(self):
      self.root = None

      def find_in_order_successor(self, inputNode):
      # find the node in right tree
      # if no right tree
      # try to find in parent
      if inputNode.right:
      inputNode = inputNode.right
      while inputNode.left:
      inputNode = inputNode.left
      return inputNode
      while inputNode.parent and inputNode.parent.right == inputNode:
      inputNode = inputNode.parent
      return inputNode.parent

      # Given a binary search tree and a number, inserts a
      # new node with the given number in the correct place
      # in the tree. Returns the new root pointer which the
      # caller should then use(the standard trick to avoid
      # using reference parameters)
      def insert(self, key):

      # 1) If tree is empty, create the root
      if (self.root is None):
      self.root = Node(key)
      return

      # 2) Otherwise, create a node with the key
      # and traverse down the tree to find where to
      # to insert the new node
      currentNode = self.root
      newNode = Node(key)
      while (currentNode is not None):

      if (key < currentNode.key):
      if (currentNode.left is None):
      currentNode.left = newNode;
      newNode.parent = currentNode;
      break
      else:
      currentNode = currentNode.left;
      else:
      if (currentNode.right is None):
      currentNode.right = newNode;
      newNode.parent = currentNode;
      break
      else:
      currentNode = currentNode.right;

      # Return a reference to a node in the BST by its key.
      # Use this method when you need a node to test your
      # findInOrderSuccessor function on
      def getNodeByKey(self, key):

      currentNode = self.root
      while (currentNode is not None):
      if (key == currentNode.key):
      return currentNode

      if (key < currentNode.key):
      currentNode = currentNode.left
      else:
      currentNode = currentNode.right

      return None


      #########################################
      # Driver program to test above function #
      #########################################

      # Create a Binary Search Tree
      bst = BinarySearchTree()
      bst.insert(20)
      bst.insert(9);
      bst.insert(25);
      bst.insert(5);
      bst.insert(12);
      bst.insert(11);
      bst.insert(14);

      # Get a reference to the node whose key is 9 ???
      test = bst.getNodeByKey(9) # testing with 9?

      # Find the in order successor of test
      succ = bst.find_in_order_successor(test)

      # Print the key of the successor node
      if succ is not None:
      print("nInorder Successor of %d is %d " % (test.key, succ.key))
      else:
      print("nInorder Successor doesn't exist")








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 4 at 19:00









      πάντα ῥεῖ

      3,82431126




      3,82431126









      asked Apr 4 at 17:44









      NinjaG

      754221




      754221

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191265%2ffindinordersuccessor-in-a-binary-search-tree-python%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191265%2ffindinordersuccessor-in-a-binary-search-tree-python%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?