Beispiel #1
0
    def setUp(self):
        self.my_bst = BSTree()

        self.solo_tree = BSTree()
        self.solo_tree.insert(10)

        self.left_tree = BSTree()
        self.left_tree.insert(10)
        self.left_tree.insert(5)

        self.right_tree = BSTree()
        self.right_tree.insert(10)
        self.right_tree.insert(15)

        self.filled_tree = BSTree()
        self.filled_tree.insert(10)
        self.filled_tree.insert(5)
        self.filled_tree.insert(15)
        self.filled_tree.insert(2)
        self.filled_tree.insert(12)
        self.filled_tree.insert(7)
        self.filled_tree.insert(17)
        self.filled_tree.insert(1)
        self.filled_tree.insert(4)
        self.filled_tree.insert(6)
        self.filled_tree.insert(9)
        self.filled_tree.insert(11)
        self.filled_tree.insert(14)
        self.filled_tree.insert(16)
        self.filled_tree.insert(23)
Beispiel #2
0
    def setUp(self):
        self.my_bst = BSTree()

        self.filled_tree = BSTree()
        self.filled_tree.insert(10)
        self.filled_tree.insert(5)
        self.filled_tree.insert(15)
        self.filled_tree.insert(2)
        self.filled_tree.insert(12)
        self.filled_tree.insert(7)
        self.filled_tree.insert(17)
        self.filled_tree.insert(1)
        self.filled_tree.insert(4)
        self.filled_tree.insert(6)
        self.filled_tree.insert(9)
        self.filled_tree.insert(11)
        self.filled_tree.insert(14)
        self.filled_tree.insert(16)
        self.filled_tree.insert(23)

        self.imbalanced_tree = BSTree()
        self.imbalanced_tree.insert(10)
        self.imbalanced_tree.insert(5)
        self.imbalanced_tree.insert(15)
        self.imbalanced_tree.insert(2)
        self.imbalanced_tree.insert(12)
        self.imbalanced_tree.insert(7)
        self.imbalanced_tree.insert(17)
        self.imbalanced_tree.insert(4)
        self.imbalanced_tree.insert(6)
        self.imbalanced_tree.insert(9)
        self.imbalanced_tree.insert(14)
        self.imbalanced_tree.insert(23)
        self.number_catcher = []
Beispiel #3
0
 def setUp(self):
     self.my_bst = BSTree()
     self.filled_tree = BSTree()
     self.filled_tree.insert(5)
     self.filled_tree.insert(3)
     self.filled_tree.insert(6)
     self.filled_tree.insert(8)
     self.filled_tree.insert(11)
     self.filled_tree.insert(2)
def test_rebalance_rotate_right():
    tree = BSTree(3)
    tree.left = BSTree(2, tree)
    tree.left.left = BSTree(1, tree.left)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [2, 1, 3]
    assert tree.depth() == 2
    assert tree.balance() == 0
    assert actual == expected
Beispiel #5
0
 def setUp(self):
     self.my_bst = BSTree()
     self.filled_tree = BSTree()
     self.filled_tree.insert(5)
     self.filled_tree.insert(3)
     self.filled_tree.insert(6)
     self.filled_tree.insert(8)
     self.filled_tree.insert(11)
     self.filled_tree.insert(2)
     self.filled_tree.insert(9)
     # i just realized how unbalanced that tree is. bad design on my part.
     # i'll fix it for the following.
     self.number_catcher = []
def traverse_ex():
    tree = BSTree(8)
    tree.left = BSTree(3, tree)
    tree.right = BSTree(10, tree)
    tree.left.left = BSTree(1, tree.left)
    tree.left.right = BSTree(6, tree.left)
    tree.right.right = BSTree(14, tree.right)
    tree.left.right.left = BSTree(4, tree.left.right)
    tree.left.right.right = BSTree(7, tree.left.right)
    tree.right.right.left = BSTree(13, tree.right.right)
    return tree
Beispiel #7
0
    def __init__(self, layer):
        """Verifier for a layer of wires.
    
    Once created, the verifier can list the crossings between wires (the 
    wire_crossings method) or count the crossings (count_crossings)."""

        self.horizontal = []
        self.verticalLines = []

        self.events = []
        self._events_from_layer(layer)
        self.events.sort()  #array sort o(nlogn)
        self.horizontal.sort()
        arr = self.horizontal[:1]

        for i in range(1, len(self.horizontal)):
            if self.horizontal[i][0] != arr[len(arr) - 1][0]:
                arr.append(self.horizontal[i])
            else:
                arr[len(arr) -
                    1][1] = arr[len(arr) - 1][1] + self.horizontal[i][1]

        self.horizontalLines = BSTree(arr)

        # for i in range(0, len(arr)):
        #   print repr(arr[i]) + '\n'

        # print self.horizontalLines.displayTree()

        self.index = RangeIndex()
        self.result_set = ResultSet()
        self.performed = False
def test_insert():
    tree = BSTree(4)
    tree.insert(2)
    tree.insert(6)
    tree.insert(1)
    tree.insert(3)
    tree.insert(5)
    tree.insert(7)
    tree.insert(8)
    tree.insert(0)
    assert tree.key == 4
    assert tree.left.key == 2
    assert tree.left.parent == tree
    assert tree.right.key == 6
    assert tree.right.parent == tree
    assert tree.left.left.key == 1
    assert tree.left.left.parent == tree.left
    assert tree.left.right.key == 3
    assert tree.left.right.parent == tree.left
    assert tree.right.left.key == 5
    assert tree.right.left.parent == tree.right
    assert tree.right.right.key == 7
    assert tree.right.right.parent == tree.right
    assert tree.right.right.right.key == 8
    assert tree.left.left.left.key == 0
def right_heavy():
    tree = BSTree(4)
    tree.insert(2)
    tree.insert(6)
    tree.insert(5)
    tree.insert(7)
    return tree
Beispiel #10
0
def test_init_bstree():
    tree = BSTree(4)
    assert tree.key == 4
    assert tree.parent is None
    assert tree.left is None
    assert tree.right is None
    assert tree.size() == 1
Beispiel #11
0
def left_heavy():
    tree = BSTree(4)
    tree.insert(2)
    tree.insert(6)
    tree.insert(1)
    tree.insert(3)
    return tree
Beispiel #12
0
def perfect_tree():
    tree = BSTree(4)
    tree.insert(2)
    tree.insert(6)
    tree.insert(1)
    tree.insert(3)
    tree.insert(5)
    tree.insert(7)
    return tree
Beispiel #13
0
def test_insert_avl_single_left_branch():
    tree = BSTree()
    for i in xrange(7, 0, -1):
        tree.insert(i)
    actual = [n.key for n in tree.breadth_first()]
    expected = [4, 2, 6, 1, 3, 5, 7]
    assert tree.depth() == 3
    assert tree.balance() == 0
    assert actual == expected
Beispiel #14
0
def test_depth():
    tree = BSTree(4)
    assert tree.depth() == 1
    tree.insert(2)
    assert tree.depth() == 2
    tree.insert(6)
    assert tree.depth() == 2
    tree.insert(1)
    tree.insert(3)
    tree.insert(5)
    tree.insert(7)
    assert tree.depth() == 3
Beispiel #15
0
def test_size():
    tree = BSTree(4)
    assert tree.size() == 1
    tree.insert(2)
    assert tree.size() == 2
    tree.insert(6)
    assert tree.size() == 3
    tree.insert(1)
    assert tree.size() == 4
    tree.insert(3)
    assert tree.size() == 5
    tree.insert(5)
    assert tree.size() == 6
    tree.insert(7)
    assert tree.size() == 7
Beispiel #16
0
def tree_rebalance_right_right():
    tree = BSTree(2)
    tree.left = BSTree(1, tree)
    tree.right = BSTree(4, tree)
    tree.right.left = BSTree(3, tree.right)
    tree.right.right = BSTree(6, tree.right)
    tree.right.right.left = BSTree(5, tree.right.right)
    tree.right.right.right = BSTree(7, tree.right.right)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [4, 2, 6, 1, 3, 5, 7]
    assert tree.depth() == 3
    assert tree.balance() == 0
    assert actual == expected
Beispiel #17
0
def test_insert_already_present():
    tree = BSTree(4)
    tree.insert(4)
    assert tree.size() == 1
Beispiel #18
0
 def setUp(self):
     self.my_bst = BSTree()
Beispiel #19
0
from bst import BSTree

tr1 = BSTree()
print "addition..."
print tr1.AddValue(100)
print tr1.AddValue(50)
print tr1.AddValue(150)
print tr1.AddValue(30)
print tr1.AddValue(60)
print tr1.AddValue(20)
print tr1.AddValue(40)
print tr1.AddValue(55)
print tr1.AddValue(70)
print tr1.AddValue(10)
print tr1.AddValue(33)
print tr1.AddValue(57)
print tr1.AddValue(15)
print tr1.AddValue(35)
print "\nsearch..."
print tr1.SearchValue(10)
print tr1.SearchValue(23)
print tr1.SearchValue(50)
print tr1.SearchValue(12)
print "\nPre order Traverse"
tr1.TraversePreOrder(tr1.root)
print "\n\ndeleting..."
print tr1.DeleteValue(23)
print tr1.DeleteValue(30)
print tr1.DeleteValue(50)
print "\nPre order Traverse"
tr1.TraversePreOrder(tr1.root)
Beispiel #20
0
def compress(text):
    tree = BSTree()
    tree.root = huffman_coding(text)
    tree.print(tree.root)