<jupyter_start><jupyter_text>二叉树<jupyter_code>class BinaryTree:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right
        
    def addLeft(self, tree):
        self.left = tree
        
    def addright(self, tree):
        self.right = tree
        
    def preorderTraversal(self, function):
        function(self)
        if self.left:
            self.left.preorderTraversal()
        if self.right:
            self.right.preorderTraversal()
            
    def inorderTraversal(self, function):
        if self.left:
            self.left.inorderTraversal()
        function(self)
        if self.right:
            self.right.inorderTraversal()
            
    def postorderTraversal(self, function):
        if self.left:
            self.left.postorderTraversal()
        if self.right:
            self.right.postorderTraversal()
        function(self)<jupyter_output><empty_output><jupyter_text>列表实现方法<jupyter_code>class BinaryTree:
    def __init__(self, data, left=None, right=None):
        self.treeList=[data, left, right]
        
    def addLeft(self, tree):
        self.treeList[1] = tree.treeList
    
    def addright(self, tree):
        self.treeList[2] = tree.treeList<jupyter_output><empty_output><jupyter_text>文本二叉树<jupyter_code>class BinaryTree:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right
        
    def addLeft(self, tree):
        self.left = tree
        
    def addright(self, tree):
        self.right = tree
        
    def preorderTraversal(self, function):
        function(self)
        if self.left:
            self.left.preorderTraversal()
        if self.right:
            self.right.preorderTraversal()
            
    def inorderTraversal(self, function):
        if self.left:
            self.left.inorderTraversal()
        function(self)
        if self.right:
            self.right.inorderTraversal()
            
    def postorderTraversal(self, function):
        if self.left:
            self.left.postorderTraversal()
        if self.right:
            self.right.postorderTraversal()
        function(self)
        
def buildTree(nodes):
    nodesPtr=0
    def build(level):
        nonlocal nodesPtr
        tree=BinaryTree(nodes[nodesPtr][1])
        nodesPtr+=1
        if nodesPtr<len(nodes) and nodes[nodesPtr][0]==level+1:
            if nodes[nodesPtr][1] != '0':
                tree.addLeft(build(level+1))
            else:
                nodesPtr+=1
        
        if nodesPtr<len(nodes) and nodes[nodesPtr][0]==level+1:
            tree.addright(build(level+1))
        return tree
    
    return build(0)

nodes=[]

def driver():
    while True:
        try:
            s=input().rstrip()
            nodes.append((len(s)-1, s.strip()))
        except:
            break
        
    tree=buildTree(nodes)
    tree.preorderTraversal(function=lambda x: print(x.data, end=''))
    print()
    tree.inorderTraversal(function=lambda x: print(x.data, end=''))
    print()
    tree.postorderTraversal(function=lambda x: print(x.data, end=''))<jupyter_output><empty_output><jupyter_text>生成器<jupyter_code>def test_yield():
    yield 1
    yield 2
    yield (1,2)
    
for x in test_yield():
    print(x)
def fib(n):
    a,b,counter=0,1,0
    while counter<=n:
        yield a
        a, b = b, a+b
        counter += 1
    
for x in fib(10):
    print(x,end=',')<jupyter_output>0,1,1,2,3,5,8,13,21,34,55,<jupyter_text>二叉树的非递归遍历<jupyter_code>class BinaryTree:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right
        
    def addLeft(self, tree):
        self.left = tree
        
    def addright(self, tree):
        self.right = tree
        
    def preorderTraversal(self):
        stack=[self]
        while len(stack)>0:
            node=stack.pop()
            if node is None:
                continue
            yield node
            stack.append(node.right)
            stack.append(node.left)
    
    def inorderTraversal(self, function):
        stack=[[self, False]] ## False means its left child has not been visited
        while len(stack)>0:
            node=stack[-1]
            if node[0] is None:
                stack.pop()
                continue
            if node[1] is False:
                stack.append([node[0].left, False])
                node[1]=True
            elif node[1] is True:
                function(node[0])
                stack.pop()
                stack.append([node[0].right, False])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11265201621506293, 'fraction_numerical': 0.01088116065713676, 'mean_word_length': 1.8938271604938273, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '122733', 'license': 'MIT', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1470, 'n_words': 283, 'path': 'Guowei/Week5/Week5.ipynb', 'repo_name': 'PKUfudawei/Data-Structure-Algorithm', 'size': '4687'}