def test_11_stress(self): l = [88, 69, 68, 83, 24, 37, 96, 38, 53, 31, 4, 82, 10, 77, 59, 79, 32, 65, 23, 48] #random.sample(xrange(100), 40) tree = Avl.from_list(l) tree.traverse(self.check) for i in l[:-1]: #print "about to delete %d" % i, tree.delete(i) #print " ... deleted %d" % i tree.traverse(self.check) l2 = [25, 94, 43, 82, 11, 32, 14, 22, 74, 65, 5, 0, 2, 68, 89, 40, 19, 31, 8, 49, 96, 58, 10, 1, 36, 60, 28, 41, 84, 30, 83, 12, 77, 86, 18, 45, 26, 44, 53, 66] cl = [48] for i in l2[:30]: tree.insert(i) cl.append(i) tree.traverse(self.check) self.assertItemsEqual(tree.to_dict().keys(), cl) for i in l2[10:30]: #print "delete >> %d" % i del cl[cl.index(i)] tree.delete(i) #print tree.to_list() tree.traverse(self.check) self.assertItemsEqual(tree.to_dict().keys(), cl) for i in l2[30:]: cl.append(i) tree.insert(i) tree.traverse(self.check) self.assertItemsEqual(tree.to_dict().keys(), cl) for i in l2[:10]+l2[30:]: self.assertIn(i, tree)
def test_remove_right_left_case(self): tree = Avl() tree.add(ROOT_DATA) tree.add(LEFT_DATA) tree.add(RIGHT_DATA) tree.add(RL_DATA) self.assertEquals(tree.root.getdata(), ROOT_DATA) self.assertEquals(tree.root.getleft().getdata(), LEFT_DATA) self.assertEquals(tree.root.getright().getdata(), RIGHT_DATA) self.assertEquals(tree.root.getright().getleft().getdata(), RL_DATA) self.assertEquals(tree.getsize(), 4) val = tree.remove(LEFT_DATA) self.assertEquals(val, LEFT_DATA) self.assertEquals(tree.getsize(), 3) self.assertNotEquals(tree.root.getdata(), ROOT_DATA) self.assertEquals(tree.root.getdata(), RL_DATA) self.assertEquals(tree.root.getleft().getdata(), ROOT_DATA) self.assertEquals(tree.root.getright().getdata(), RIGHT_DATA)
def left_chain_tree(): tree = Avl() # Add root tree.add(ROOT_DATA) # Add left child tree.add(LEFT_DATA) # Add left's child tree.add(LL_DATA) return tree
def test_add_left_right_case(self): tree = Avl() tree.add(ROOT_DATA) self.assertEquals(tree.root.getdata(), ROOT_DATA) tree.add(LEFT_DATA) self.assertEquals(tree.root.getdata(), ROOT_DATA) tree.add(LR_DATA) self.assertNotEquals(tree.root.getdata(), ROOT_DATA) self.assertEquals(tree.root.getdata(), LR_DATA) self.assertEquals(tree.root.getleft().getdata(), LEFT_DATA) self.assertEquals(tree.root.getright().getdata(), ROOT_DATA)
def root_only(): tree = Avl() tree.add(ROOT_DATA) return tree
def balanced_tree(): tree = Avl() # Add root tree.add(ROOT_DATA) # Add children tree.add(LEFT_DATA) tree.add(RIGHT_DATA) # Add left's children tree.add(LL_DATA) tree.add(LR_DATA) # Add right's children tree.add(RL_DATA) tree.add(RR_DATA) return tree
def test(filename): """ run bentley ottmann """ adjuster, segments = load_segments(filename) #segments = sorted(segments, key=lambda x: min(x.endpoints[0].coordinates[1], # x.endpoints[0].coordinates[1])) events = SortedList() for s in segments: events.add((min(s.endpoints), "in", s)) events.add((max(s.endpoints), "out", s)) sweep = Avl() result = [] print("Events (init):", events) print("\n========\n LOOP \n========\n\n ") while True: try: current, event_type, segment = events.pop(0) Segment.point = current if DEBUG: print("Current:", current, event_type, segment) if DEBUG: print("Events:", events) if DEBUG: print("SL:", sweep) tmp_sweep = Avl() for node in sweep: tmp_sweep.add(node.value) sweep = tmp_sweep if DEBUG: print("SL:", sweep) if event_type == "in": node = sweep.add(segment) left = node.predecessor() if left: left = left.value intrsctn = segment.intersection_with(left) if intrsctn is not None and intrsctn.coordinates[1] <= current.coordinates[1] \ and intrsctn.coordinates[0] != current.coordinates[0]: events.add((intrsctn, "x", (left, segment))) right = node.successor() if right: right = right.value intrsctn = segment.intersection_with(right) if intrsctn is not None and intrsctn.coordinates[1] <= current.coordinates[1] \ and intrsctn.coordinates[0] != current.coordinates[0]: events.add((intrsctn, "x", (segment, right))) elif event_type == "out": node = sweep.search(segment) left = node.predecessor() right = node.successor() if left and right: left = left.value right = right.value intrsctn = left.intersection_with(right) if intrsctn is not None and intrsctn.coordinates[1] <= current.coordinates[1] \ and intrsctn.coordinates[0] != current.coordinates[0]: events.add((intrsctn, "x", (left, right))) sweep.delete(segment) else: #event_type == "x" result.append(current) u = sweep.search(segment[0]) right = u.successor() if right: intrsctn = u.value.intersection_with(right.value) if intrsctn is not None and intrsctn.coordinates[1] <= current.coordinates[1] \ and intrsctn.coordinates[0] != current.coordinates[0]: events.add((intrsctn, "x", (u.value, right.value))) v = sweep.search(segment[1]) left = v.predecessor() if left: intrsctn = v.value.intersection_with(left.value) if intrsctn is not None and intrsctn.coordinates[1] <= current.coordinates[1] \ and intrsctn.coordinates[0] != current.coordinates[0]: events.add((intrsctn, "x", (left.value, v.value))) if DEBUG: print("Events:", events) if DEBUG: print("SL:", len(sweep), sweep) tycat(segments, result, current) input("Press [ENTER] to continue...\n") except IndexError: break print("\n\n=========\n THE END\n=========") print("Events:", events) print("SL:", sweep) print("IL:", result) tycat(segments, result)
def test_12_insert(self): sl = (68, (58, (49, None, None), (65, None, None)), (74, None, None)) t = Avl.from_list_raw(sl) t.insert(60) t.traverse(self.check)