Example #1
0
def find_intersection(segments):
  points=[]

  for i in range(len(segments)):
    if segments[i][0][0] == segments[i][1][0]:
      points.append((segments[i][0], 'v', segments[i])) #i ID'li VERTICAL line'a ait nokta

    elif segments[i][0][1] == segments[i][1][1]:
      points.append((segments[i][0], 'h')) #i ID'li HORIZONTAL line'a ait nokta 
      points.append((segments[i][1], 'h')) 

  points = sorted(points, key=lambda x: x[0][0])
  #points.sort() #sorting points with respect to the x-coordinate values 

  # set root to be an empty tree
  root = None

  if len(points) == 1: 
    return None

  for i in range(len(points)):
    #print points
    #if point belongs to horizontal segments
    
    if points[i][1] == 'h':
      if search(root,points[i][0][1]) == None:
        #if not there then add it
        root = insert(root, points[i][0][1])

      else:
        #if there then delete it
        root = delete(root, points[i][0][1])

    #if vertical line
    else:

      #seg is the vertical line this point belongs to
      seg = points[i][2]

      y1 = seg[0][1]
      y2 = seg[1][1]

      ymin = min(y1,y2)
      ymax = max(y1,y2)

      least = get_smallest_at_least(root, ymin)

      if least == None: 
        continue
      elif least.key <= ymax:
        return (seg[0][0], least.key)

  return None

# asegment = [((1,4),(1,7)), ((1,5),(3,5))]


# print find_intersection(asegment)
Example #2
0
def run_time_trial(root, maxvalue, ncount = 100):

    start = time.process_time()

    # do the thing
    for _ in range(ncount):
        search_value = random.random() * maxvalue
        _ = bst.search(root, search_value)

    end = time.process_time()

    trial_time = end - start

    print(f"# Ran {ncount} trials over {trial_time} seconds.\n")
Example #3
0
    def test_create_from_list(self):
        """This method creates a fully-populated binary-search-tree of depth 4, on the numbers: [0, 30]"""

        #
        #                 ___________________15_____________________
        #                /                                          \
        #         ______7_______                           __________23_________
        #        /              \                         /                     \
        #     __3__           ___11___               ____19___               ____27___
        #    /     \         /        \             /         \             /         \
        #   1       5       9         _13         _17         _21         _25         _29
        #  / \     / \     / \       /   \       /   \       /   \       /   \       /   \
        # 0   2   4   6   8   10    12    14    16    18    20    22    24    26    28    30
        #

        # If we add the above values in the correct order, the tree is balanced, for free.
        root = bst.new([
            15, 7, 23, 3, 11, 19, 27, 1, 5, 9, 13, 17, 21, 25, 29, 0, 2, 4, 6,
            8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
        ])
        # DEBUG
        print(str(bst))

        # spot check -- *not full* check:
        self.assertEqual(root.value, 15)
        self.assertEqual(root.right.value, 23)
        self.assertEqual(root.right.left.value, 19)
        self.assertEqual(root.right.left.left.value, 17)
        self.assertEqual(root.right.left.left.left.value, 16)
        self.assertEqual(root.right.left.left.right.value, 18)

        # again spot checks -- verify that the tree can find values it contains
        self.assertEqual(bst.search(root, 8), 8)
        self.assertEqual(bst.search(root, 16), 16)
        self.assertEqual(bst.search(root, 18), 18)
        self.assertEqual(bst.search(root, 24), 24)
Example #4
0
import bst

bst = bst.BST([4, 5, 10, 9, 3, 7, 1, 0, 49])

bst.dump()

print(bst.search(4))
            node = node.left

'''


'''

root = None
root = insert(root, 1)
root = insert(root, 3)
root = insert(root, 5)
root = insert(root, 14)
root = insert(root, 2)
root = insert(root, 6)

print get_successor(search(root, 2)).key
# should print 5
'''



'''
find_intersection: finds an intersection of the given line segments, or decides
that there is no intersection
ARGS:
  segments - list of tuples of the form ((x1, y1), (x2, y2)), where (x1, y1) and
  (x2, y2) are the endpoints of the line segment; detailed specifications can be
  found in the problem statement
RETURN:
  if there are intersections between the given line segments, return one such
  intersection as a tuple (x, y); otherwise return None