Verify binary search tree with duplicate keys











up vote
0
down vote

favorite












Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right



def IsBinarySearchTree(tree):
# Implement correct algorithm here
min = -sys.maxsize
max = sys.maxsize
key_hash = {}

def is_bst_until(idx_node,maxi,mini, is_left = False):
if idx_node == -1:
return True

node = tree[idx_node]
key = node[0]
idx_left = node[1]
idx_right = node[2]

if (key>maxi) or (key<mini):
return False

if is_left:
if (key==mini):
return False

return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))

if len(tree) == 0:
return True
return is_bst_until(0, max, min)


def main():
nodes = int(sys.stdin.readline().strip())
tree =
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if IsBinarySearchTree(tree):
print("CORRECT")
else:
print("INCORRECT")

threading.Thread(target=main).start()


Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right



Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.



Examples:



3
2 1 2
2 -1 -1
3 -1 -1
#Incorrect

5
1 -1 1
2 -1 2
3 -1 3
4 -1 4
5 -1 -1
#Correct


Frankly speaking I don't think my solution is a good one. I'm concerned about is_left



I'll appreciate if you suggest me an alternative solution









share


























    up vote
    0
    down vote

    favorite












    Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right



    def IsBinarySearchTree(tree):
    # Implement correct algorithm here
    min = -sys.maxsize
    max = sys.maxsize
    key_hash = {}

    def is_bst_until(idx_node,maxi,mini, is_left = False):
    if idx_node == -1:
    return True

    node = tree[idx_node]
    key = node[0]
    idx_left = node[1]
    idx_right = node[2]

    if (key>maxi) or (key<mini):
    return False

    if is_left:
    if (key==mini):
    return False

    return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))

    if len(tree) == 0:
    return True
    return is_bst_until(0, max, min)


    def main():
    nodes = int(sys.stdin.readline().strip())
    tree =
    for i in range(nodes):
    tree.append(list(map(int, sys.stdin.readline().strip().split())))
    if IsBinarySearchTree(tree):
    print("CORRECT")
    else:
    print("INCORRECT")

    threading.Thread(target=main).start()


    Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right



    Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.



    Examples:



    3
    2 1 2
    2 -1 -1
    3 -1 -1
    #Incorrect

    5
    1 -1 1
    2 -1 2
    3 -1 3
    4 -1 4
    5 -1 -1
    #Correct


    Frankly speaking I don't think my solution is a good one. I'm concerned about is_left



    I'll appreciate if you suggest me an alternative solution









    share
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right



      def IsBinarySearchTree(tree):
      # Implement correct algorithm here
      min = -sys.maxsize
      max = sys.maxsize
      key_hash = {}

      def is_bst_until(idx_node,maxi,mini, is_left = False):
      if idx_node == -1:
      return True

      node = tree[idx_node]
      key = node[0]
      idx_left = node[1]
      idx_right = node[2]

      if (key>maxi) or (key<mini):
      return False

      if is_left:
      if (key==mini):
      return False

      return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))

      if len(tree) == 0:
      return True
      return is_bst_until(0, max, min)


      def main():
      nodes = int(sys.stdin.readline().strip())
      tree =
      for i in range(nodes):
      tree.append(list(map(int, sys.stdin.readline().strip().split())))
      if IsBinarySearchTree(tree):
      print("CORRECT")
      else:
      print("INCORRECT")

      threading.Thread(target=main).start()


      Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right



      Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.



      Examples:



      3
      2 1 2
      2 -1 -1
      3 -1 -1
      #Incorrect

      5
      1 -1 1
      2 -1 2
      3 -1 3
      4 -1 4
      5 -1 -1
      #Correct


      Frankly speaking I don't think my solution is a good one. I'm concerned about is_left



      I'll appreciate if you suggest me an alternative solution









      share













      Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right



      def IsBinarySearchTree(tree):
      # Implement correct algorithm here
      min = -sys.maxsize
      max = sys.maxsize
      key_hash = {}

      def is_bst_until(idx_node,maxi,mini, is_left = False):
      if idx_node == -1:
      return True

      node = tree[idx_node]
      key = node[0]
      idx_left = node[1]
      idx_right = node[2]

      if (key>maxi) or (key<mini):
      return False

      if is_left:
      if (key==mini):
      return False

      return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))

      if len(tree) == 0:
      return True
      return is_bst_until(0, max, min)


      def main():
      nodes = int(sys.stdin.readline().strip())
      tree =
      for i in range(nodes):
      tree.append(list(map(int, sys.stdin.readline().strip().split())))
      if IsBinarySearchTree(tree):
      print("CORRECT")
      else:
      print("INCORRECT")

      threading.Thread(target=main).start()


      Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right



      Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.



      Examples:



      3
      2 1 2
      2 -1 -1
      3 -1 -1
      #Incorrect

      5
      1 -1 1
      2 -1 2
      3 -1 3
      4 -1 4
      5 -1 -1
      #Correct


      Frankly speaking I don't think my solution is a good one. I'm concerned about is_left



      I'll appreciate if you suggest me an alternative solution







      python algorithm binary-search





      share












      share










      share



      share










      asked 2 mins ago









      Daniel Chepenko

      1335




      1335



























          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          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%2f208588%2fverify-binary-search-tree-with-duplicate-keys%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          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%2f208588%2fverify-binary-search-tree-with-duplicate-keys%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python