Beispiel #1
0
def arrayBST(l,start,end):
	mid=(start+end)/2
	if start<=end:
		tree=BinaryTree(l[mid])
		tree.left=arrayBST(l,start,mid-1)
		tree.right=arrayBST(l,mid+1,end)
		return tree
def arrayBST(l, start, end):
    mid = (start + end) / 2
    if start <= end:
        tree = BinaryTree(l[mid])
        tree.left = arrayBST(l, start, mid - 1)
        tree.right = arrayBST(l, mid + 1, end)
        return tree
def in_pre(inorder, preorder, start, end):
    if start > end:
        return None

    tree = BinaryTree(preorder[in_pre.index])
    in_pre.index += 1

    if start == end:
        return tree

    i = inorder.index(tree.key)

    tree.left = in_pre(inorder, preorder, start, i - 1)
    tree.right = in_pre(inorder, preorder, i + 1, end)

    return tree
Beispiel #4
0
    def _build_tree(self, graph):
        print("Building the tree")
        # for clusters in each upper level besides 0
        print ("The height of clusters is ", self._height_of_cluster)

        uncontained_clusters = []
        for i in (range(2, self._height_of_cluster+1)):
        # for i in (range(2, 3)):
            # for each cluster in this level
            print (i)
            # print("At level ", 3, " there are ", len(self._network.clusters_by_level(3)), " clusters.")
            # self._network.print_cluster_by_level(3)

            # print("At level ", 4, " there are ", len(self._network.clusters_by_level(4)), " clusters.")
            # self._network.print_cluster_by_level(4)

            # print("At level ", 5, " there are ", len(self._network.clusters_by_level(5)), " clusters.")
            # self._network.print_cluster_by_level(5)
            # at first, gather bottom level clusters which are contained in the upper level
            cluster_contain_in_upper_level_map = {}
            # list of clusters that were added to some other parent level clusters
            ignore_cluster_list = []

            for current_cluster in self._network.clusters_by_level(i):

                # just checking
                clustered_peers_list = []
                print("Just checking Assert redundant clusters BEFORE")
                for cluster in self._network.clusters_by_level(i):
                    for node in cluster.graph.nodes:
                        clustered_peers_list.append(node)
                print(clustered_peers_list)
                assert (self.at_least_one(Counter(clustered_peers_list)) < 0)
                assert (self.at_most_logn(Counter(clustered_peers_list)) < 0)


                # ignore intermediate clusters

                print(current_cluster.cluster_id)
                cluster_contain_in_upper_level_map[current_cluster.cluster_id] = []
                skip_cluster = False
                lower_level_clusters = self._network.clusters_by_level(i - 1)
                print(len(lower_level_clusters))
                print(len(uncontained_clusters))

                for uncontained_cluster in uncontained_clusters:
                    lower_level_clusters.append(uncontained_cluster)
                print(len(lower_level_clusters))
                print("LOWER LEVEL CLUSTERS",lower_level_clusters)
                # for lower_level_cluster in self._network.clusters_by_level(i - 1):
                for lower_level_cluster in lower_level_clusters:
                    if lower_level_cluster.intermediate:
                        print(current_cluster.cluster_id)
                        print(current_cluster.peers)
                    else:
                        if lower_level_cluster.cluster_id not in ignore_cluster_list:
                            # check to see for duplicates
                            for key in cluster_contain_in_upper_level_map[current_cluster.cluster_id]:
                                # print(Counter(self._network.find_cluster_by_id(key).graph.nodes) == Counter(lower_level_cluster.graph.nodes))
                                if Counter(self._network.find_cluster_by_id(key).graph.nodes) == Counter(
                                        lower_level_cluster.graph.nodes):
                                    print("DUPLICATE FOUND")
                                    skip_cluster = True
                                    continue

                                for cluster_to_ignore in ignore_cluster_list:
                                    # print("CLUSTER TO IGNORE ID")
                                    # print(cluster_to_ignore)
                                    # print(lower_level_cluster.cluster_id)
                                    if Counter(lower_level_cluster.graph.nodes) == Counter(
                                            self._network.find_cluster_by_id(cluster_to_ignore).graph.nodes):
                                        print("DUPLICATE ENTRIES FOUND")
                                        skip_cluster = True
                                        continue

                            # check if same members

                            if skip_cluster == True:
                                continue
                            # print("Current cluster id ", current_cluster.cluster_id, " with nodes ", current_cluster.graph.nodes)
                            # print("Lower level cluster id ", lower_level_cluster.cluster_id, " with nodes ", lower_level_cluster.graph.nodes)
                            result = set(lower_level_cluster.graph.nodes).issubset(current_cluster.graph.nodes)
                            # print("CONTAIN ", result)

                            # print("THE MAP IS ", cluster_contain_in_upper_level_map)
                            if result:
                                cluster_contain_in_upper_level_map[current_cluster.cluster_id].append(
                                    lower_level_cluster.cluster_id)
                                ignore_cluster_list.append(lower_level_cluster.cluster_id)

            print("SIZE LOWER LEVEL CLUSTERS ", len(self._network.clusters_by_level(i-1)))
            print("IGNORE CLUSTER LIST SIZE",len(ignore_cluster_list))
            print (ignore_cluster_list)

            # delete all uncontained_clusters that are in ignore_cluster_list
            temp_uncontained_clusters = copy.deepcopy(uncontained_clusters)
            print("BEFORE",uncontained_clusters)
            print(len(uncontained_clusters))
            for cluster in uncontained_clusters:
                print("PRINTING A CLUSTER")
                print(cluster.cluster_id)

            print(len(uncontained_clusters))
            for cluster in uncontained_clusters:
                print("CHECKING CHECKING")
                print(cluster.cluster_id)
                print(ignore_cluster_list)
                print(cluster.cluster_id in ignore_cluster_list)
                if cluster.cluster_id in ignore_cluster_list:
                    print("FOUND CLUSTER", cluster.cluster_id)
                    print("REMOVING")
                    index_to_delete = None
                    for index in range(0,len(temp_uncontained_clusters)):
                        if temp_uncontained_clusters[index].cluster_id == cluster.cluster_id:
                            index_to_delete = index
                            print("index_TO_DELETE IS ", index_to_delete)
                            break
                    del(temp_uncontained_clusters[index_to_delete])
                    # temp_uncontained_clusters.remove(cluster)
            uncontained_clusters = copy.deepcopy(temp_uncontained_clusters)
            print("AFTERE",uncontained_clusters)
            for cluster in uncontained_clusters:
                print(cluster.cluster_id)

            for lower_level_cluster in self._network.clusters_by_level(i-1):
                if lower_level_cluster.cluster_id not in ignore_cluster_list:
                    if not lower_level_cluster.intermediate:
                        uncontained_clusters.append(lower_level_cluster)


            print("PREPARED CLUSTER CONTAIN MAP", cluster_contain_in_upper_level_map)

            # build the binary trees
            current_tree = BinaryTree('temp')

            for root_cluster_key in cluster_contain_in_upper_level_map:

                tree_leafs = []
                current_tree = BinaryTree(root_cluster_key)
                print("ROOT CLUSTER IS ", root_cluster_key)
                print (len(cluster_contain_in_upper_level_map[root_cluster_key]))
                height_of_tree = int(math.log(1 if len(cluster_contain_in_upper_level_map[root_cluster_key]) == 0 else 2**(len(cluster_contain_in_upper_level_map[root_cluster_key]) - 1).bit_length(), 2) )


                if len(cluster_contain_in_upper_level_map[root_cluster_key])== 1:
                    height_of_tree = 1
                print("HEIGHT OF TREE IS ", height_of_tree + 1)

                tree_nodes = cluster_contain_in_upper_level_map[root_cluster_key]

                lower_level_trees = []
                for bottommmost_level_cluster in cluster_contain_in_upper_level_map[root_cluster_key]:
                    print("REACHED and count is ")
                    print(bottommmost_level_cluster)
                    temp_tree = BinaryTree(bottommmost_level_cluster)

                    temp_tree.set_data(self._network.find_cluster_by_id(bottommmost_level_cluster).peers)
                    lower_level_trees.append(temp_tree)

                for level in range(height_of_tree):
                # for level in range(1):
                    new_lower_level_trees = []

                    print ("LEVEL ",level, "of the tree")
                    print("NEW LEVEL NODE COUNT ", len(lower_level_trees))

                    total_nodes_in_this_level = len(lower_level_trees)
                    for node in range(total_nodes_in_this_level):
                        if len(lower_level_trees) >= 2:
                            # name the cluster root different
                            tree_name = ''
                            if level + 1 == height_of_tree:
                                tree_name = root_cluster_key
                            else:
                                tree_name = lower_level_trees[0].getNodeValue() + "_" + lower_level_trees[1].getNodeValue()
                            temp_tree = BinaryTree(tree_name)
                            temp_data = []
                            temp_data.extend(lower_level_trees[0].get_data())
                            temp_data.extend(lower_level_trees[1].get_data())
                            temp_tree.set_data(temp_data)

                            temp_tree.left = lower_level_trees[0]
                            temp_tree.right = lower_level_trees[1]

                            # set the parents of lower level trees

                            lower_level_trees[0].set_parent(temp_tree)
                            lower_level_trees[1].set_parent(temp_tree)

                            lower_level_trees.pop(0)
                            lower_level_trees.pop(0)

                            # create a cluster out of the tree
                            if level+1 != height_of_tree:
                                # no cluster for the root cluster
                                print("KOKOKOKOKOKO",temp_tree.get_data())
                                cluster_graph = graph.subgraph([str(s) for s in temp_tree.get_data()])
                                cluster = Cluster(tree_name, cluster_graph, i)
                                cluster.set_intermediate()
                                self._network.add_cluster(i, cluster)

                            new_lower_level_trees.append(temp_tree)

                            print(level+1)
                            print(height_of_tree)
                            if level+1 == height_of_tree:
                                current_tree = temp_tree
                                print("PRINTING TREE")
                                printTree(temp_tree)
                                print("printed tree")
                        elif len(lower_level_trees) == 1:
                            # todo ignore same single cluster in multiple levels
                            if level + 1 == height_of_tree:
                                print("SHOULD REACH ONCE")
                                print(lower_level_trees[0].getNodeValue())
                                tree_name = ''
                                if level + 1 == height_of_tree:
                                    tree_name = root_cluster_key
                                else:
                                    tree_name = lower_level_trees[0].getNodeValue()

                                temp_tree = BinaryTree(tree_name)
                                temp_tree.set_data(lower_level_trees[0].get_data())
                                temp_tree.left = lower_level_trees[0]

                                # set the parents of lower level trees
                                # set parent only if not same node in lower level

                                lower_level_trees[0].set_parent(temp_tree)

                                lower_level_trees.pop(0)

                                # create a cluster out of the tree
                                if level + 1 != height_of_tree:
                                    # no cluster for the root cluster
                                    cluster_graph = graph.subgraph([str(s) for s in temp_tree.get_data()])
                                    cluster = Cluster(tree_name, cluster_graph, i)
                                    cluster.set_intermediate()
                                    self._network.add_cluster(i, cluster)

                                new_lower_level_trees.append(temp_tree)
                                if level + 1 == height_of_tree:
                                    current_tree = temp_tree
                            else:
                                new_lower_level_trees.append(lower_level_trees.pop(0))

                        print("AND NEW LEVEL NODE COUNT ", len(lower_level_trees))

                    print("NEW LEVEL NODE COUNT ", len(new_lower_level_trees))
                    lower_level_trees = new_lower_level_trees

                print("Tree for cluster ", root_cluster_key, " is ")
                (self._network.find_cluster_by_id(root_cluster_key)).set_tree(current_tree)
                # set the tree children here, traversing starts from the children

                printTree(current_tree)
                (self._network.find_cluster_by_id(root_cluster_key)).set_tree_leafs(get_leafs(current_tree))

            print("KO")
            for clus in self._network.clusters_by_level(i):
                clus.print()
                # clus.tree.display_tree()
        print("UNCONTAINED CLUSTERS ")
        for cluster in uncontained_clusters:
            cluster.print()
        assert len(uncontained_clusters) == 0
    # def _sort_clusters_in_a_level(self):
    #     for i in (range(0, self._height_of_cluster + 1)):
    #         index = 0
    #         for clus in self._network.clusters_by_level(i):
    #             clus.setIndex(index)
    #             index = index+1
    #     print("PRINTING CLUSTERS")
    #     for i in (range(0, self._height_of_cluster + 1)):
    #         for clus in self._network.clusters_by_level(i):
    #             clus.print()
        print("Height of clusters", self._height_of_cluster)
        for i in range(0,self._height_of_cluster+1):
            print("At level ", i, " there are ", len(self._network.clusters_by_level(i)), " clusters.")
            self._network.print_cluster_by_level(i)