Ejemplo n.º 1
0
 def cal_dc(self):
     #if self.dcfactor<=0 or self.dcfactor>1:
     self.cal_dc_index()
     #distance_list=sorted(self.distance,key=lambda x:x[1])
     bst_list=[]
     self.distance_bst=None
     
     for i,x in enumerate(self.X):
         #caution that i != j avoid the distance 0
         for j,y in enumerate(self.X[i+1:]):
             #distance function can be l2 cosine, or else.
             #recommand be simple
             #distance=distance(x,y)
             dis=distance.distance(x,y)
             if len(bst_list) <= self.dc_index:
                 bst_list.append(dis)
                 if len(bst_list) > self.dc_index:
                     #init the bst
                     self.distance_bst=bst.BST(bst_list)
                     #self.distance_bst.inOrderTraverse(self.distance_bst.root)
             else:
                 n,p=self.distance_bst.get_rightmost()
                 if n.data <= dis:
                     continue
                 else:
                     #print (' ')
                     #print ('del',n.data,'add',dis)
                     self.distance_bst.delete_node(n,p)
                     self.distance_bst.insert(dis)
                     
                     #self.distance_bst.inOrderTraverse(self.distance_bst.root)
             pass
         
     n,p=self.distance_bst.get_rightmost()
     self.dc=n.data
Ejemplo n.º 2
0
def test_randon_insert():
    data = bst.BST()

    for i in range(0, 100):
        data.put(random.randint(0, sys.maxsize), id_generator(size=60))

    assert (data.check())
Ejemplo n.º 3
0
def buildBTree():
    #will read from file and create a binary tree of words and there embeddings
    numWords = 0
    #foo item used to create the tree outside the loop
    #item will be deleted once the tree is created
    fooNode = WN.Word("fooItem 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17")
    myTree = bst.BST(fooNode)

    buildStart = dt.now()
    #1theFile = open('H:\Javis USB\CS2302\lab5\glove.6B.50d.txt', encoding = 'utf-8')
    theFile = open('G:\Javis USB\CS2302\lab5\glove.6B.50d.txt',
                   encoding='utf-8')
    #theFile = open('reduced.txt', encoding = 'utf-8')
    for line in theFile:
        numWords += 1
        tempNode = WN.Word(line)
        bst.Insert(myTree, tempNode)
    bst.Delete(myTree, fooNode)
    buildEnd = dt.now() - buildStart
    theFile.close()

    myTreeHeight = bst.MaxHeight(myTree)

    print()
    print('Binary Search Tree stats:')
    print('Number of nodes: ' + str(numWords))
    print('max height is: ' + str(myTreeHeight))
    print('Running time for binary search tree construction: ' + str(buildEnd))
    print()
    print('Reading word file to determine similarities')
    print()
    similarites_bst(myTree)
Ejemplo n.º 4
0
    def merge_clu(self):
        """
        合并现有的clu
        从clu里不断地挑2个最相似的,合并为1个
        不断重复此步骤,知道达到clu_num要求

        不断地合并和一次性全合并有什么区别
        如果没有 就记录下来 一次全部合并 完事了
        knn只能记录几个距离,不能确定有几个点。所以不能这么做
        """
        if len(self.center_list) < 1:
            # 第一次合并clu 初始化
            for i, x in enumerate(self.cluCenter):
                self.center_list.append(x[0])
        # 算所有clu_center之间的距离 将距离最短的合并为一个
        # 从此之后cluCenter 有3个元素,0一个是中心,1一个是包含点,2一个是候选中心
        merge_num = len(self.cluCenter) - self.clu_num
        # print(merge_num)
        if merge_num < 1:  # 此时不需要 合并
            return
        clu_min_dis_bst = bst.BST([], merge_num, 'min')

        tmp_clu_center = self.cluCenter
        while len(tmp_clu_center) > self.clu_num:
            min_dis = float("inf")
            min_i = -1
            min_j = -1
            for i, x in enumerate(tmp_clu_center):
                candidate_x = get_candidate_from_a_clu(x)
                for j, y in enumerate(tmp_clu_center[i:]):
                    if i != i + j:
                        candidate_y = get_candidate_from_a_clu(y)
                        dis = self.cal_min_dis_bet2candi(
                            candidate_x, candidate_y
                        )  # element in candidate are all position
                        if dis < min_dis:
                            min_dis = dis
                            min_i = i
                            min_j = i + j
            # 1次合并i j 修改i,并把j抬出pop
            # 注意这里的ij不再是角标 为了方便变成了min i和min j
            # i = min_i
            # j = min_j
            # print(min_i, min_j, len(tmp_clu_center))
            tmp_clu_center[min_i] = [
                tmp_clu_center[min_i][0],
                tmp_clu_center[min_i][1] + tmp_clu_center[min_j][1],
                get_candidate_from_a_clu(tmp_clu_center[min_i]) +
                get_candidate_from_a_clu(tmp_clu_center[min_j])
            ]
            tmp_clu_center.pop(min_j)
        self.cluCenter = tmp_clu_center
        # data类要记录 相关的position
        # dis_data = bst.BstDisData(dis, i, j)  # 这里i j放的是cluCenter的角标
        # clu_min_dis_bst.try_add(dis_data)
        # dis_list = clu_min_dis_bst.postOrderTraverse(clu_min_dis_bst.root)
        # new_clu_center = create_new_clu_from_old_and_dis_list(self.cluCenter, dis_list)  # 获得新的clu center
        # self.cluCenter = new_clu_center

        pass
def findMode(arr):
    """ find the modal max frequency and unique values in an array of n values"""

    #*********************************************************************
    #@param : an array of n elements in read from prompt                 *
    #@return : the value that appears the most in the tree, it frequency *
    #          and also the number of unique numbers                     *
    #*********************************************************************

    tree = bst.BST()
    modalVal = 0
    maxFreq = 0
    uniques = 0
    for k in arr:
        node = tree.search(k)
        if node == None:
            tree.insert(k, 1)
            uniques += 1
        else:
            if node.key == k:
                node.value += 1
                if node.value > maxFreq:
                    maxFreq = node.value
                    modalVal = k
    return (modalVal, maxFreq, uniques)
Ejemplo n.º 6
0
def test_nooby():
    data = bst.BST()
    data.put(1, "A")
    data.put(2, "B")

    for k in data.get_all_keys():
        print(k, data.get(k))

    assert (data.check())
Ejemplo n.º 7
0
 def __init__(self, mode):
     '''
     Select BST mode by passing "bst" as argument; otherwise select AVL mode.
     '''
     if mode == "bst":
         logging.info("running in BST mode")
         self._tree = bst.BST()
     else:
         logging.info("running in AVL mode")
         self._tree = avl.AVL()
Ejemplo n.º 8
0
    def test_augmentations_on_random(self):
        values = [random.randint(0, 1000) for i in xrange(500)]
        bst = bstmod.BST()
        for value in values:
            bst.insert(value)
            self.check_augmentations(bst.root)

        for value in values:
            bst.delete(bst.root)
            self.check_augmentations(bst.root)
Ejemplo n.º 9
0
def test_insert():
    data = bst.BST()
    keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    for i in keys:
        data.put(i, str(i))

    j = 0
    for i in data.get_all_keys():
        assert (i == keys[j])
        j += 1
Ejemplo n.º 10
0
def test_insert_inverse():
    data = bst.BST()
    keys = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    for i in keys:
        data.put(i, str(i))

    j = 0
    for i in data.get_all_keys():
        assert (i == keys[10 - j])
        j += 1
Ejemplo n.º 11
0
def list2Tree(L):
    if len(L) == 0:
        return None
    mid = len(L) // 2
    tree = bst.BST()
    tree.insert(L[mid])
    root = tree.root
    root.left = list2Tree(L[:mid])
    root.right = list2Tree(L[mid + 1:])
    return root
Ejemplo n.º 12
0
 def test_augmentations_on_insert(self):
     bst = bstmod.BST()
     bst.insert(7)
     # 7
     self.assertEqual(bst.root.height, 1)
     self.assertEqual(bst.root.weight, 1)
     bst.insert(3)
     #   7
     #  /
     # 3
     self.assertEqual(bst.root.height, 2)
     self.assertEqual(bst.root.weight, 2)
     self.assertEqual(bst.root.left_child.height, 1)
     self.assertEqual(bst.root.left_child.weight, 1)
     bst.insert(10)
     #   7
     #  / \
     # 3   10
     self.assertEqual(bst.root.height, 2)
     self.assertEqual(bst.root.weight, 3)
     self.assertEqual(bst.root.left_child.height, 1)
     self.assertEqual(bst.root.left_child.weight, 1)
     self.assertEqual(bst.root.right_child.height, 1)
     self.assertEqual(bst.root.right_child.weight, 1)
     bst.insert(1)
     #      7
     #     / \
     #    3   10
     #   /
     #  1
     self.assertEqual(bst.root.height, 3)
     self.assertEqual(bst.root.weight, 4)
     self.assertEqual(bst.root.left_child.height, 2)
     self.assertEqual(bst.root.left_child.weight, 2)
     self.assertEqual(bst.root.left_child.left_child.height, 1)
     self.assertEqual(bst.root.left_child.left_child.weight, 1)
     self.assertEqual(bst.root.right_child.height, 1)
     self.assertEqual(bst.root.right_child.weight, 1)
     bst.insert(4)
     #      7
     #     / \
     #    3   10
     #   / \
     #  1   4
     self.assertEqual(bst.root.height, 3)
     self.assertEqual(bst.root.weight, 5)
     self.assertEqual(bst.root.left_child.height, 2)
     self.assertEqual(bst.root.left_child.weight, 3)
     self.assertEqual(bst.root.left_child.left_child.height, 1)
     self.assertEqual(bst.root.left_child.left_child.weight, 1)
     self.assertEqual(bst.root.left_child.right_child.height, 1)
     self.assertEqual(bst.root.left_child.right_child.weight, 1)
     self.assertEqual(bst.root.right_child.height, 1)
     self.assertEqual(bst.root.right_child.weight, 1)
Ejemplo n.º 13
0
def ListToTree(L, T):

    #will take a sorted list and create a balanced BST
    if len(L) > 0:
        head = int(len(L) / 2)
        T = bst.BST(L[head])
        #T.item = L[head]
        #to create right sub tree
        T.right = ListToTree(L[head + 1:], T.right)
        #to create left sub tree
        T.left = ListToTree(L[:head], T.left)
    return T
 def testInsert(self):
     testBST = b.BST()
     testBST.insert(3)
     testBST.insert(5)
     testBST.insert(4)
     testBST.insert(2)
     testBST.insert(1)
     testBST.insert(1)
     testBST.insert(14)
     testBST.insert(12)
     testBST.insert(11)
     self.assertEqual(testBST.items[0], 3)
Ejemplo n.º 15
0
 def test_insert(self):
     bst = bstmod.BST()
     self.assertIsNone(bst.root)
     bst.insert(8)
     self.assertEqual(bst.root.data, 8)
     bst.insert(4)
     self.assertEqual(bst.root.left_child.data, 4)
     self.assertIsNone(bst.root.right_child)
     bst.insert(6)
     self.assertEqual(bst.root.left_child.right_child.data, 6)
     bst.insert(12)
     self.assertEqual(bst.root.right_child.data, 12)
Ejemplo n.º 16
0
 def testInsert(self):
     tree = self.ExampleTree()
     node4 = bt.Node(4)
     tree.insert(node4)
     self.assertEqual(tree.root.left.left.right.left, node4)
     tree = self.ExampleTree()
     node54 = bt.Node(54)
     tree.insert(node54)
     self.assertEqual(tree.root.right.right.right.right, node54)
     # Check inserting into empty tree
     tree = bst.BST()
     tree.insert(node4)
     self.assertEqual(tree.root, node4)
Ejemplo n.º 17
0
def buildParseTree(expr):
    Tree = bst.BST()
    li = []
    print(expr)
    for i in expr:
        if i is '(':
            li.append(bst.TreeNode(i))
        elif i is ')':
            right = bst.TreeNode(li.pop())
            mid = bst.TreeNode(li.pop())
            left = bst.TreeNode(li.pop())
            mid.left = left
            mid.right = right
            left.parent = right.parent = mid
            li.pop()
            li.append(mid)
        else:
            li.append(bst.TreeNode(i))

    t = bst.BST()
    t.root = li[0]
    return t
 def testTreeWithDFT(self):
     testBST = b.BST()
     testBST.insert(3)
     testBST.insert(5)
     testBST.insert(4)
     testBST.insert(2)
     testBST.insert(1)
     testBST.insert(1)
     testBST.insert(14)
     testBST.insert(12)
     testBST.insert(11)
     res = testBST.treeWithDFT()
     self.assertEqual(res, "3,2,1,5,4,14,12,11,")
Ejemplo n.º 19
0
def test_insert_delete_2():
    data = bst.BST()
    keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    for i in keys:
        data.put(i, str(i))

    data.delete(10)
    assert (data.isBalanced())
    data.put(10, str(10))
    j = 0
    for i in data.get_all_keys():
        assert (i == keys[j])
        j += 1
Ejemplo n.º 20
0
 def testRemove(self):
     # Remove leaf node
     tree = self.ExampleTree()
     self.assertTrue(tree.root.right.left.right.left.right)
     tree.remove_value(31)
     self.assertTrue(tree.root.right.left.right.left.right is None)
     # Remove node with one child
     tree = self.ExampleTree()
     self.assertEqual(tree.root.right.left.right.left.value, 29)
     tree.remove_value(29)
     self.assertEqual(tree.root.right.left.right.left.value, 31)
     # Remove node with two children
     tree = self.ExampleTree()
     self.assertEqual(tree.root.right.value, 43)
     tree.remove_value(43)
     self.assertEqual(tree.root.right.value, 47)
     self.assertEqual(tree.root.right.right.value, 53)
     self.assertEqual(tree.root.right.left.value, 23)
     # Remove root node
     tree = self.ExampleTree()
     self.assertEqual(tree.root.value, 19)
     self.assertEqual(tree.root.left.value, 7)
     self.assertEqual(tree.root.right.value, 43)
     tree.remove_value(19)
     self.assertEqual(tree.root.value, 23)
     self.assertEqual(tree.root.left.value, 7)
     self.assertEqual(tree.root.right.value, 43)
     # Remove root node from otherwise empty tree
     tree = bst.BST(bt.Node(19))
     self.assertEqual(tree.root.value, 19)
     tree.remove_value(19)
     self.assertEqual(tree.root, None)
     # Remove root node from lopsided right-only tree
     tree = self.ExampleTree()
     tree.root.left = None
     self.assertEqual(tree.root.value, 19)
     self.assertEqual(tree.root.right.value, 43)
     tree.remove_value(19)
     self.assertEqual(tree.root.value, 43)
     self.assertEqual(tree.root.left.value, 23)
     self.assertEqual(tree.root.right.value, 47)
     # Remove root node from lopsided right-only tree
     tree = self.ExampleTree()
     tree.root.right = None
     self.assertEqual(tree.root.value, 19)
     tree.remove_value(19)
     self.assertEqual(tree.root.value, 7)
     self.assertEqual(tree.root.left.value, 3)
     self.assertEqual(tree.root.right.value, 11)
Ejemplo n.º 21
0
 def ExampleTree(self):
     a = bt.Node(19)
     b = bt.Node(7)
     c = bt.Node(3)
     d = bt.Node(2)
     e = bt.Node(5)
     f = bt.Node(11)
     g = bt.Node(17)
     h = bt.Node(13)
     i = bt.Node(43)
     j = bt.Node(23)
     k = bt.Node(37)
     l = bt.Node(29)
     m = bt.Node(31)
     n = bt.Node(41)
     o = bt.Node(47)
     p = bt.Node(53)
     tree = bst.BST(a)
     a.left = b
     b.parent = a
     b.left = c
     c.parent = b
     c.left = d
     d.parent = c
     c.right = e
     e.parent = c
     b.right = f
     f.parent = b
     f.right = g
     g.parent = f
     g.left = h
     h.parent = g
     a.right = i
     i.parent = a
     i.left = j
     j.parent = i
     j.right = k
     k.parent = j
     k.left = l
     l.parent = k
     l.right = m
     m.parent = l
     k.right = n
     n.parent = k
     i.right = o
     o.parent = i
     o.right = p
     p.parent = o
     return tree
 def testFind(self):
     testBST = b.BST()
     testBST.insert(3)
     testBST.insert(5)
     testBST.insert(4)
     testBST.insert(2)
     testBST.insert(1)
     testBST.insert(1)
     testBST.insert(14)
     testBST.insert(12)
     testBST.insert(11)
     self.assertTrue(testBST.find(3))
     self.assertTrue(testBST.find(5))
     self.assertTrue(testBST.find(4))
     self.assertFalse(testBST.find(1337))
     self.assertTrue(testBST.find(2))
Ejemplo n.º 23
0
def main():
    # a = Student.Student("Henry", "Jones", "123-45-7899", "*****@*****.**", "40")
    # b = Student.Student("Anakin", "Skywalker", "357-89-7642", "*****@*****.**", "30")
    # c = Student.Student("Anakin", "Skywalker", "357-89-7642", "*****@*****.**", "30")
    #
    # database = bst.BST()
    # database.Insert(a)
    # database.Insert(b)
    # database.Insert(c)


    database = bst.BST()
    print "Insert Duration: " + str(timeSomething(InsertAllStudents, database, "InsertNames.txt"))
    global gAgeTotal
    print "Traverse Duration: " + str(timeSomething(TraverseAllStudents, database))
    print "Delete Duration: " + str(timeSomething(DeleteStudents, database, "DeleteNames.txt"))
    print "Retrieve Duration: " + str(timeSomething(RetrieveNames, database, "RetrieveNames.txt"))
Ejemplo n.º 24
0
def main():
    n = int(input("ENTER THE NUMBER OF STUDENTS: \n"))
    b = bst.BST()
    for i in range(n):
        print("\nENTER THE DETAILS OF STUDENT ", i + 1, ":")
        key = int(input("Enter the roll num:        "))
        name = input("Enter name:        ")
        add = input("Enter the address:        ")
        mob = int(input("Enter the mobile number:        "))
        course = input("Enter the course:        ")
        print('Enter the marks in three subjects:')
        m1 = int(input("First subject:    "))
        m2 = int(input("Second subject:    "))
        m3 = int(input("Third subject:    "))
        total = m1 + m2 + m3
        percent = total / 3
        print("Total Marks:   ", total)
        print("Average marks:   ", percent)
        b.insert(key)
    print("\nBST TRAVERSAL\n")
    b.inorder()
    print("DO YOU  WANT TO DELETE ANY KEYS IN THE TREE ?")
    print("IF YES ENTER y  OR Y\n")
    ch = input()
    if ch == 'y' or ch == 'Y':
        while ch == 'y' or ch == 'Y':
            print("\nEnter the number to be deleted\n")
            num = int(input())
            b.remove(num)
            print("Enter y or Y to continue:\n")
            ch = input()
        print("AFTER THE DELETION :\n")
        b.inorder()
    print("\nDO YOU  WANT TO SEARCH ANY KEYS IN THE TREE ? ")
    print("IF YES ENTER y  OR Y\n")
    ch = input()
    while ch == 'y' or ch == 'Y':
        print("\nEnter the key to be searched :\n")
        num = int(input())
        b.search(num)
        print("Enter y or Y to continue:\n")
        ch = input()
    print(
        "\n*************************END OF BST OPERATIONS*******************************\n"
    )
Ejemplo n.º 25
0
def test_insert_delete_4():
    data = bst.BST()
    keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    keyso = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10]

    for i in keys:
        data.put(i, str(i))

    data.delete(9)
    assert (data.isBalanced())

    for i in data.get_all_keys():
        print(i)

    j = 0
    for i in data.get_all_keys():
        assert (i == keyso[j])
        j += 1
Ejemplo n.º 26
0
    def test_node_param(self):
        bst = bstmod.BST()
        bst.insert(7)
        self.assertIsInstance(bst.root, bstmod.BstNode)

        class NewNode(bstmod.BstNode):
            pass

        class NewBst(bstmod.BST):
            NodeClass = NewNode

        bst = NewBst()
        bst.insert(7)
        self.assertIsInstance(bst.root, NewNode)
        bst.insert(8)
        bst.delete(bst.root)
        bst.delete(bst.root)
        self.assertIsNone(bst.root)
Ejemplo n.º 27
0
def InsertNames():
    lines = open("./InsertNamesMedium.txt", "r").readlines()
    start = datetime.now()
    students = bst.BST()
    failures = 0
    for line in lines:
        data = line.split()
        student = Student(data[0], data[1], data[2], data[3], data[4])
        if not students.insert(student):
            failures += 1
    end = datetime.now()
    time = end.timestamp() - start.timestamp()
    prCyan('====================================')
    prMagenta("Inserting")
    prCyan('====================================')
    prYellow("time of insertion: %s seg" % round(time, 2))
    prGreen("Success: %s" % students.getSize())
    prRed("Failures: %s" % failures)

    return students
Ejemplo n.º 28
0
    while not tempQueue.empty():
        currNode = tempQueue.get()
        print(currNode.data, end=' ')

        if currNode.left is not None:
            tempQueue.put(currNode.left)
        if currNode.right is not None:
            tempQueue.put(currNode.right)


if __name__ == "__main__":

    A = [11, 6, 7, 16, 17, 2, 4, 18, 14, 8, 15, 1, 20, 13]
    B = [8, 15, 5, 13, 11, 6, 7, 2, 4, 18, 14]

    T = bst.BST()
    T2 = bst.BST()

    for a in A:
        T.insert(a)

    for b in B:
        T2.insert(b)

    T.inOrder()
    plt.close('all')
    T.draw()
    T2.draw()

    print(smallest(T.root))  # 1
    print(largest(T.root))  # 20
Ejemplo n.º 29
0
import sys
import bst
import string
import random
import json
import util

data = bst.BST()

def test_insert_performance():
    with open("../data/material.json") as f:
        material = json.loads(f.read())

    for item in material:
        data.put(util.date_to_float(item['Master'].get('Timestamp', None)), item)

def test_get_all_keys():
    with open("../data/material_l.json", "w") as f:
        f.write(json.dumps(data.get_data_all_keys()))

def test_get_keys_ceiling():
    with open("../data/material_d.json", "w") as f:
        f.write(json.dumps(data.get_data_range_keys(data.ceiling(554980170.533),
                                                   data.get_max())))
Ejemplo n.º 30
0
def list2Tree(L): # Wrapper for list2Tree_n
    T = bst.BST()
    T.root = list2Tree_n(L)
    T.size = len(L)
    return T