示例#1
0
def test_find():
	s = SLL()
	for x in [6,4,2]:
		s.place(x)

	for x in [5, 1]:
		s.place(x)

	s.place(0)
	s.place(7)

	assert s.find(0) == 0
	assert s.find(7) == 7
	assert s.find(1) == 1
	assert s.find(2) == 2
	assert s.find(4) == 4
	assert s.find(3) == None
	assert s.find(10) == None
	assert s.find(-1) == None

	# Test custom comparator
	s2  = SLL.fromList([(2,1), (1, 'a'), (2, 3), (0, 'c'), (4,1), (3,0)])
	assert s2.find((2,1)) == (2,1)
	assert s2.find((4,3), comparatorfn = lambda (a,b),(c,d): cmp(b,d)) == (2,3)

	# compare only the first item in the pair between two nodes
	assert s2.find((2,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c)) == (2,1)
	assert s2.find((0,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c)) == (0, 'c')
示例#2
0
def test_findMatchingNode():
	s = SLL()
	for x in [6,4,2]:
		s.place(x)

	for x in [5, 1]:
		s.place(x)

	s.place(0)
	s.place(7)

	assert s.findMatchingNode(0) == s.head
	assert s.findMatchingNode(7) == s.tail
	assert s.findMatchingNode(1) == s.head.next
	assert s.findMatchingNode(2) == s.head.next.next
	assert s.findMatchingNode(4) == s.head.next.next.next
	assert s.findMatchingNode(3) == None
	assert s.findMatchingNode(10) == None
	assert s.findMatchingNode(-1) == None

	# Test custom comparator
	s2  = SLL.fromList([(2,1), (1, 'a'), (2, 3), (0, 'c'), (4,1), (3,0)])
	assert s2.findMatchingNode((2,1)) == s2.head
	assert s2.findMatchingNode((2,3), comparatorfn = lambda (a,b),(c,d): cmp(b,d)) == s2.head.next.next

	# compare only the first item in the pair between two nodes
	assert s2.findMatchingNode((2,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c)) == s2.head
	node = s2.findMatchingNode((0,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c))

	# change node's value
	node.value = (0, 'e')
	assert node == s2.head.next.next.next
	assert node.value == (0, 'e')
	assert SLL.toList(s2) == [(2,1), (1, 'a'), (2, 3), (0, 'e'), (4,1), (3,0)]
示例#3
0
def test_find():
    s = SLL()
    for x in [6, 4, 2]:
        s.place(x)

    for x in [5, 1]:
        s.place(x)

    s.place(0)
    s.place(7)

    assert s.find(0) == 0
    assert s.find(7) == 7
    assert s.find(1) == 1
    assert s.find(2) == 2
    assert s.find(4) == 4
    assert s.find(3) == None
    assert s.find(10) == None
    assert s.find(-1) == None

    # Test custom comparator
    s2 = SLL.fromList([(2, 1), (1, 'a'), (2, 3), (0, 'c'), (4, 1), (3, 0)])
    assert s2.find((2, 1)) == (2, 1)
    assert s2.find((4, 3), comparatorfn=lambda (a, b),
                   (c, d): cmp(b, d)) == (2, 3)

    # compare only the first item in the pair between two nodes
    assert s2.find((2, 100), comparatorfn=lambda (a, b),
                   (c, d): cmp(a, c)) == (2, 1)
    assert s2.find((0, 100), comparatorfn=lambda (a, b),
                   (c, d): cmp(a, c)) == (0, 'c')
示例#4
0
    def top_view_LR(self, aggregate_fn=_default_printfn, **kwargs):
        if not self.root:
            return

        # Queue to help with level-order traversal
        q = Queue()

        # Store L/R width of a node and its value if it is visible in the top-view, in a sorted list
        # ordered by the width
        # at the end of the level-order traversal the list would contain
        # [(-max_left_width, node value), ...., (0, root.value), ... (max_right_width, node value)]
        slist = SLL()

        # pairs a(w1, node_val1) vs b(w2, node_val2) are valued against each other
        # depending on w1 vs w2 (where w1 and w2 are widths, so slist is kept sorted by the nodes' L/R widths)
        pair_comparator = lambda (w1, node_val1), (w2, node_val2): cmp(w1, w2)

        min_width = 0
        max_width = 0
        q.enqueue((0, self.root))

        # Top-view begins with the root node
        slist.place((0, self.root.value))
        while q.length() != 0:
            width, node = q.dequeue()

            if width < min_width:
                min_width = width
                slist.place((width, node.value))
            elif width > max_width:
                max_width = width
                slist.place((width, node.value))

            # NOTE: width 0, is root, would already be filled in at root,
            # and in a top-view is not going to replaced

            q.enqueue((width - 1, node.left)) if node.left else None
            q.enqueue((width + 1, node.right)) if node.right else None

        # At the end of the level-order traversal,
        # slist is ordered by width, so
        # (-max_left_width, ..., 0, ..., max_right_width)
        # Just retrieve the SLL L-R for the top view (L-R)
        while slist.size != 0:
            width, item = slist.pop_front()
            aggregate_fn(kwargs, item)
示例#5
0
def test_findMatchingNode():
    s = SLL()
    for x in [6, 4, 2]:
        s.place(x)

    for x in [5, 1]:
        s.place(x)

    s.place(0)
    s.place(7)

    assert s.findMatchingNode(0) == s.head
    assert s.findMatchingNode(7) == s.tail
    assert s.findMatchingNode(1) == s.head.next
    assert s.findMatchingNode(2) == s.head.next.next
    assert s.findMatchingNode(4) == s.head.next.next.next
    assert s.findMatchingNode(3) == None
    assert s.findMatchingNode(10) == None
    assert s.findMatchingNode(-1) == None

    # Test custom comparator
    s2 = SLL.fromList([(2, 1), (1, 'a'), (2, 3), (0, 'c'), (4, 1), (3, 0)])
    assert s2.findMatchingNode((2, 1)) == s2.head
    assert s2.findMatchingNode((2, 3),
                               comparatorfn=lambda (a, b),
                               (c, d): cmp(b, d)) == s2.head.next.next

    # compare only the first item in the pair between two nodes
    assert s2.findMatchingNode((2, 100),
                               comparatorfn=lambda (a, b),
                               (c, d): cmp(a, c)) == s2.head
    node = s2.findMatchingNode((0, 100),
                               comparatorfn=lambda (a, b), (c, d): cmp(a, c))

    # change node's value
    node.value = (0, 'e')
    assert node == s2.head.next.next.next
    assert node.value == (0, 'e')
    assert SLL.toList(s2) == [(2, 1), (1, 'a'), (2, 3), (0, 'e'), (4, 1),
                              (3, 0)]
示例#6
0
def test_place():
	s = SLL()
	for x in [6,4,2]:
		s.place(x)

	for x in [5, 3, 1]:
		s.place(x)

	s.place(0)
	s.place(7)

	# wont get added
	s.place(0, allowduplicates=False)
	s.place(4, allowduplicates=False)
	s.place(7, allowduplicates=False)

	l = []
	collate_fn = lambda kwargs, data : kwargs['lst'].append(data)
	s.traverse(collate_fn, lst=l)
	assert(l == range(8))

	# Test custom comparator
	s2 = SLL()
	test_list = [(2,1), (1, 'a'), (2, 3), (0, 'c'), (4,1), (3,0)]
	for x in test_list:
		s2.place(x, lambda (a,b),(c,d): cmp(a, c))

	# Duplicates, won't get added
	s2.place((0, 'c'), allowduplicates=False)
	s2.place((2, 1), allowduplicates=False)
	s2.place((4, 1), allowduplicates=False)

	l = []
	collate_fn = lambda xargs, data : xargs['lst'].append(data)
	s2.traverse(collate_fn, lst=l)
	assert(l == [(0, 'c'), (1, 'a'), (2, 1), (2, 3), (3, 0), (4, 1)])

	# Test custom comparator2
	s3 = SLL()
	test_list = [(2,1), (1, 'a'), (2, 3), (0, 'c'), (4,1), (3,0)]
	for x in test_list:
		s3.place(x, lambda (a,b),(c,d): cmp(b, d))

	l = []
	collate_fn = lambda xargs, data : xargs['lst'].append(data)
	s3.traverse(collate_fn, lst=l)
	assert l == [(3, 0), (2, 1), (4, 1), (2, 3), (1, 'a'), (0, 'c')]
示例#7
0
def test_place():
    s = SLL()
    for x in [6, 4, 2]:
        s.place(x)

    for x in [5, 3, 1]:
        s.place(x)

    s.place(0)
    s.place(7)

    # wont get added
    s.place(0, allowduplicates=False)
    s.place(4, allowduplicates=False)
    s.place(7, allowduplicates=False)

    l = []
    collate_fn = lambda kwargs, data: kwargs['lst'].append(data)
    s.traverse(collate_fn, lst=l)
    assert (l == range(8))

    # Test custom comparator
    s2 = SLL()
    test_list = [(2, 1), (1, 'a'), (2, 3), (0, 'c'), (4, 1), (3, 0)]
    for x in test_list:
        s2.place(x, lambda (a, b), (c, d): cmp(a, c))

    # Duplicates, won't get added
    s2.place((0, 'c'), allowduplicates=False)
    s2.place((2, 1), allowduplicates=False)
    s2.place((4, 1), allowduplicates=False)

    l = []
    collate_fn = lambda xargs, data: xargs['lst'].append(data)
    s2.traverse(collate_fn, lst=l)
    assert (l == [(0, 'c'), (1, 'a'), (2, 1), (2, 3), (3, 0), (4, 1)])

    # Test custom comparator2
    s3 = SLL()
    test_list = [(2, 1), (1, 'a'), (2, 3), (0, 'c'), (4, 1), (3, 0)]
    for x in test_list:
        s3.place(x, lambda (a, b), (c, d): cmp(b, d))

    l = []
    collate_fn = lambda xargs, data: xargs['lst'].append(data)
    s3.traverse(collate_fn, lst=l)
    assert l == [(3, 0), (2, 1), (4, 1), (2, 3), (1, 'a'), (0, 'c')]