Help me with my assignment please

How would I print out a given range of numbers from a binary tree in an ascending order.
Like how can I get the range [3;6] from the picture without traversing the whole tree checking if it's within that range.

A Conspiracy Theorist Is Talking Shirt $21.68

It's All Fucked Shirt $22.14

A Conspiracy Theorist Is Talking Shirt $21.68

  1. 2 months ago
    Anonymous

    [...]

  2. 2 months ago
    Anonymous

    It's impossible.
    It's an unsolved problem and I gave up on it years ago.

    • 2 months ago
      Anonymous

      cur <- root
      for 3...6 -> i
      while cur is not null
      if cur == i
      print i
      else if i > cur
      cur <- cur.right
      else
      cur <- cur.left

  3. 2 months ago
    Anonymous

    >Like how can I get the range [3;6] from the picture without traversing the whole tree checking if it's within that range.
    are you asking how to check of every node value of the tree is >= 3 and <= 6 ?

  4. 2 months ago
    Anonymous

    You do a regular search to find the 3. (If current == 3 then done else if current < 3 then recurse with left child else recurse with right child.)
    You do a regular search to find the 6.
    You do a regular inorder traversal starting from the 3 until you reach the 6.

    • 2 months ago
      Anonymous

      cur <- root
      for 3...6 -> i
      while cur is not null
      if cur == i
      print i
      else if i > cur
      cur <- cur.right
      else
      cur <- cur.left

      you search for 3
      then you traverse until you hit 6 (or a number greater therethan)
      and then you kys

      I should have clarified that the minimum and maximum values of the range don't need to be and probably aren't in the tree.
      So if the nodes in the pic were 20 30 and I'm looking for 25. And now the problem is that the the node closest to 25 could be at the end of branch and all of the parents of the leaf are lower or bigger than the range which would make checks for a change from curr > low to curr < low useless.

      • 2 months ago
        Anonymous

        >t.doesn't understand a binary tree NGMI
        If the tree isn't sorted then you have to sort first. If the tree is sorted then your comment is dumb and you need to look into how a binary tree actually works.

      • 2 months ago
        Anonymous

        If you are just trying to optimize then just do this
        while min > root and max > root
        root <- root.right
        while min < root and max < root
        root <- root.left
        for i <- min ... max
        cur <- root
        while cur is not none
        if cur == i
        print i
        else if cur > i
        cur <- cur.right
        else
        cur <- cur.left

      • 2 months ago
        Anonymous

        Even more optimization
        while min > root and max > root
        root <- root.right
        while min < root and max < root
        root <- root.left
        for i <- min ... max
        if root > i
        root<- root.right
        cur<- root
        else
        cur <- root.left
        while cur is not none
        if cur > i
        cur <- cur.right
        else if cur < i
        cur <- cur.left
        else
        print i
        break

      • 2 months ago
        Anonymous

        It doesn't matter whether the start and end nodes are in the tree or not. In the process of looking for the start node you'll also find the place where it should've been, so your traversal simply starts from there. Similarly your traversal will end when you reach a node that is >= the end value, so either the value is in the tree and you stop when you == it, or the value is not in the tree and you stop when you > it.

        • 2 months ago
          Anonymous

          map<int> tree;
          auto begIt = tree.lower_bound(3);
          auto endIt = tree.lower_bound(6);
          for (auto it = begIt; it != endIt; ++it) cout << @it;

          Even without access to C++ standard library, algorithm holds:
          1. create 'getNext' to get from one node to another
          2. find first node
          3. find last node
          4. traverse

          Hope that helps, anon. You are the best!

          Even more optimization
          while min > root and max > root
          root <- root.right
          while min < root and max < root
          root <- root.left
          for i <- min ... max
          if root > i
          root<- root.right
          cur<- root
          else
          cur <- root.left
          while cur is not none
          if cur > i
          cur <- cur.right
          else if cur < i
          cur <- cur.left
          else
          print i
          break

          I came up with somewhat good enough solution involving three recursive functions first gets into the interval than the second one traverses through the interval and if it hits a node which is out of bounds it calls a third functions which traverses the whole subtree of that node (right or left child depending on the side) .
          Not the best but it's good enough I suppose. I found the process of looking for the min and max node a bit too complex to implement. But I might try it.

          Thanks for the help guys

      • 2 months ago
        Anonymous

        >I should have clarified that the minimum and maximum values of the range don't need to be and probably aren't in the tree.
        So basically the problem is finding if yes or no any node of the tree is in the given range?
        fricking hell, try to think more than 10s before asking a question next time

  5. 2 months ago
    Anonymous

    I could solve this problem for you, if I was paid to.

    • 2 months ago
      Anonymous

      found the chinese student

  6. 2 months ago
    Anonymous

    cur <- root
    for 3...6 -> i
    while cur is not null
    if cur == i
    print i
    else if i > cur
    cur <- cur.right
    else
    cur <- cur.left

  7. 2 months ago
    Anonymous

    ngmi.
    change your major.
    the dancer degree will be about right.

  8. 2 months ago
    Anonymous

    you search for 3
    then you traverse until you hit 6 (or a number greater therethan)
    and then you kys

  9. 2 months ago
    Anonymous

    Why are you wasting your time trying to learn this shit? There are no jobs.

  10. 2 months ago
    Anonymous

    inorder traversal with if statement to check current value

  11. 2 months ago
    Anonymous

    >without traversing the whole tree checking if it's within that range.
    it's not possible to guarantee that your range isn't going to be at the end of your search algorithm if you want it in ascending order. the best you can do is o(n).

    • 2 months ago
      Anonymous

      >the best you can do is o(n)
      actually O(M), or O(max(lg N, M)) if you want to be really pedantic.
      where N is the number of nodes in the tree, and M is the number of nodes containing numbers within the specified range. You only need to traverse the subtree containing the specified range.
      As a thought experiment for this, consider that you can re-root the tree such that the lowest valued node in the range is the root - this is doable in O(lg N). Cut off the left branch in O(1). Re-root the tree such that the highest valued node in the range is the root in O(lg N). Cut off the right branch in O(1). Now walk the remaining tree printing out the values in O(M) since this tree now only contains elements in the requested range. Now you can reassemble the tree by splicing in the right branch, rerooting at the min node and splicing back in the left branch.
      This is a lot of stuff to code up, so it's nice that the properties of an ordered tree means that the rerooting and cutting/splicing code isn't actually needed, it's a matter of your traversal/printing algorithm maintaining the proper perspective of the tree as it's walking through it.
      If you've already got a library of splaying functions in hand, and don't care about reader safety in a multithreaded environment then the thought experiment version probably the best solution simply because it's completely linear and trivially proven correct while having the same big O as the custom solution.

  12. 2 months ago
    Anonymous

    you can make a map function that requires a position. With the position you can get the next most efficient moves to your destination

  13. 2 months ago
    Anonymous

    map<int> tree;
    auto begIt = tree.lower_bound(3);
    auto endIt = tree.lower_bound(6);
    for (auto it = begIt; it != endIt; ++it) cout << @it;

    Even without access to C++ standard library, algorithm holds:
    1. create 'getNext' to get from one node to another
    2. find first node
    3. find last node
    4. traverse

    Hope that helps, anon. You are the best!

Your email address will not be published. Required fields are marked *