code: def _build_kth_tree(self):
          
          neg_tau = -1.0 * abs(self.tau_matrix)
          edges = self.previous_tree.edges
          visited = set([0])
          unvisited = set(range(self.n_nodes))
  
          while len(visited) != self.n_nodes:
              adj_set = set()
              for x in visited:
                  for k in range(self.n_nodes):
                      # check if (x,k) is a valid edge in the vine
                      if k not in visited and k != x and self._check_contraint(edges[x], edges[k]):
                          adj_set.add((x, k))
  
              # find edge with maximum tau
              if len(adj_set) == 0:
                  visited.add(list(unvisited)[0])
                  continue
  
              pairs = sorted(adj_set, key=lambda e: neg_tau[e[0]][e[1]])[0]
              left_parent, right_parent = Edge.sort_edge([edges[pairs[0]], edges[pairs[1]]])
  
              new_edge = Edge.get_child_edge(len(visited) - 1, left_parent, right_parent)
              new_edge.tau = self.tau_matrix[pairs[0], pairs[1]]
              self.edges.append(new_edge)
  
              visited.add(pairs[1])
              unvisited.remove(pairs[1])
docs: Build tree for level k.