<jupyter_start><jupyter_text>Export JSON to use with D3<jupyter_code>from networkx.readwrite import json_graph
import json
# json.dumps(json_graph.tree_data(G,root='*',  attrs={'children': 'children', 'id': 'name'}))
json.dumps(json_graph.tree_data(G,root=root,  attrs={'children': 'children', 'id': 'id'}))<jupyter_output><empty_output><jupyter_text>Using the information from above we can define four rules as follows:- If the current token is a '(', add a new node as the left child of the current node, and descend to the left child.- If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.- If the current token is a number, set the root value of the current node to the number and return to the parent.- If the current token is a ')', go to the parent of the current node.<jupyter_code>class BinaryTree:
    def __init__(self, val):
        self.value = val
        self.right = None
        self.left = None

    def get_value(self):
        return self.value

    def set_value(self, val):
        self.value = val

    def get_right(self):
        return self.right

    def get_left(self):
        return self.left

    def insert_left(self, v):
        t = BinaryTree(v)
        if self.left:
            t.left = self.left
        self.left = t
        return t

    def insert_right(self, v):
        t = BinaryTree(v)
        if self.right:
            t.right = self.right
        self.right = t
        return t


# from pythonds.basic.stack import Stack
# from pythonds.trees.binaryTree import BinaryTree

def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = []  # Stack()
    eTree = BinaryTree('')
    pStack.append(eTree)  # push(eTree)
    currentTree = eTree

    for i in fplist:
        if i == '(':
            currentTree.insert_left('')  # insertLeft('')
            pStack.append(currentTree)  # push(currentTree)
            currentTree = currentTree.get_left()  # getLeftChild()

        elif i in ['+', '-', '*', '/']:
            currentTree.set_value(i)  # setRootVal(i)
            currentTree.insert_right('')  # insertRight('')
            pStack.append(currentTree)  # push(currentTree)
            currentTree = currentTree.get_right()  # getRightChild()

        elif i == ')':
            currentTree = pStack.pop()

        elif i not in ['+', '-', '*', '/', ')']:
            try:
                currentTree.set_value(int(i))  # setRootVal(int(i))
                parent = pStack.pop()
                currentTree = parent

            except ValueError:
                raise ValueError("token '{}' is not a valid integer".format(i))

    return eTree


pt = buildParseTree("( ( 10 + 5 ) * ( 6 - 7 ) ")
G=nx.DiGraph()
root=build_graph(G,None,pt )

draw_graph(G)


def getId=

class No:
    def __init__(self, v):
        self.data=v
        self.left=None
        self.right=None
        
no=No(5)


import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)<jupyter_output>['GTK', 'GTKAgg', 'GTKCairo', 'MacOSX', 'Qt4Agg', 'Qt5Agg', 'TkAgg', 'WX', 'WXAgg', 'GTK3Cairo', 'GTK3Agg', 'WebAgg', 'nbAgg', 'agg', 'cairo', 'gdk', 'pdf', 'pgf', 'ps', 'svg', 'template']
{'fraction_non_alphanumeric': 0.13242424242424242, 'fraction_numerical': 0.0033333333333333335, 'mean_word_length': 2.623490669593853, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '509195', 'license': 'MIT', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1045, 'n_words': 304, 'path': 'coding/binary-tree.ipynb', 'repo_name': 'vadim-ivlev/STUDY', 'size': '3300'}