in pyrit/prompt_converter/codechameleon_converter.py [0:0]
def _encrypt_binary_tree(self, sentence):
class TreeNode:
"""A node in the binary tree."""
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def build_tree(words, start, end):
"""Builds the binary tree from the list of words."""
if start > end:
return None
mid = (start + end) // 2
node = TreeNode(words[mid])
node.left = build_tree(words, start, mid - 1)
node.right = build_tree(words, mid + 1, end)
return node
def tree_to_json(node):
"""Converts a tree to a JSON representation."""
if node is None:
return None
return {"value": node.value, "left": tree_to_json(node.left), "right": tree_to_json(node.right)}
words = sentence.split()
root = build_tree(words, 0, len(words) - 1)
tree_representation = tree_to_json(root)
return tree_representation