MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / build_a_tree

Method build_a_tree

data_structures/binary_tree/is_sum_tree.py:109–131  ·  view source on GitHub ↗

r""" Create a binary tree with the specified structure: 11 / \ 2 29 / \ / \ 1 7 15 40 \ 35 >>> list(BinaryTree.build_a_tree()) [1, 2, 7, 11, 15, 29, 35, 40]

(cls)

Source from the content-addressed store, hash-verified

107
108 @classmethod
109 def build_a_tree(cls) -> BinaryTree:
110 r"""
111 Create a binary tree with the specified structure:
112 11
113 / \
114 2 29
115 / \ / \
116 1 7 15 40
117 \
118 35
119 >>> list(BinaryTree.build_a_tree())
120 [1, 2, 7, 11, 15, 29, 35, 40]
121 """
122 tree = BinaryTree(Node(11))
123 root = tree.root
124 root.left = Node(2)
125 root.right = Node(29)
126 root.left.left = Node(1)
127 root.left.right = Node(7)
128 root.right.left = Node(15)
129 root.right.right = Node(40)
130 root.right.right.left = Node(35)
131 return tree
132
133 @classmethod
134 def build_a_sum_tree(cls) -> BinaryTree:

Callers 1

is_sum_tree.pyFile · 0.80

Calls 2

BinaryTreeClass · 0.70
NodeClass · 0.70

Tested by

no test coverage detected