<jupyter_start><jupyter_text>Binary Search Tree**Constructor** sets three attributes: data, left subtree and right subtree.  **Insert** inserts a new subtree into the proper location.  **Find** finds a value. If value not found, returns False.  **Get_size** returns the number of nodes in the tree (excluding None nodes).  **Preorder** prints a preorder traversal of the tree.  **Inorder** prints an inorder traversal of the tree.<jupyter_code>class Tree:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right
    def insert(self, data):
        if self.data == data:
            return False # duplicate value
        elif self.data > data:
            if self.left is not None:
                return self.left.insert(data)
            else:
                self.left = Tree(data)
                return True
        else:
            if self.right is not None:
                return self.right.insert(data)
            else:
                self.right = Tree(data)
                return True
    def find(self, data):
        if self.data == data:
            return data
        elif self.data > data:
            if self.left is None:
                return False
            else:
                return self.left.find(data)
        elif self.data < data:
            if self.right is None:
                return False
            else:
                return self.right.find(data)
    def get_size(self):
        if self.left is not None and self.right is not None:
            return 1 + self.left.get_size() + self.right.get_size()
        elif self.left:
            return 1 + self.left.get_size()
        elif self.right:
            return 1 + self.right.get_size()
        else:
            return 1
    def preorder(self):
        if self is not None:
            print (self.data, end=' ')
            if self.left is not None:
                self.left.preorder()
            if self.right:
                self.right.preorder()
    def inorder(self):
        if self is not None:
            if self.left is not None:
                self.left.inorder()
            print (self.data, end=' ')
            if self.right is not None:
                self.right.inorder()<jupyter_output><empty_output><jupyter_text>Test CodeWe create a new tree, insert one value, insert a whole list of values, find all values from 1 to 15 (False for 0, 5 and 8 shows that those values are not in the tree), print the size of the tree, print preorder and postorder traversals.<jupyter_code>tree = Tree(7)
tree.insert(9)
for i in [15, 10, 2, 12, 3, 1, 13, 6, 11, 4, 14, 9]:
    tree.insert(i)
for i in range(16):
    print(tree.find(i), end=' ')
print('\n', tree.get_size())

tree.preorder()
print()
tree.inorder()
print()<jupyter_output>False 1 2 3 4 False 6 7 False 9 10 11 12 13 14 15 
 13
7 2 1 3 6 4 9 15 10 12 11 13 14 
1 2 3 4 6 7 9 10 11 12 13 14 15
{'fraction_non_alphanumeric': 0.09202453987730061, 'fraction_numerical': 0.03101567825494206, 'mean_word_length': 1.8302796528447445, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '207576', 'license': 'MIT', 'n_tokens_mistral': 944, 'n_tokens_neox': 842, 'n_words': 330, 'path': 'Section_4/Binary Search Tree.ipynb', 'repo_name': 'Anirach/udemy_data_structures', 'size': '2934'}