MIT 6.100L Introduction to CS and Programming Using Python, Fall 2022 · Problem set 4, 1.1) Data Representation Practice — tree1, tree2, tree3 · Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Build the three trees described below as Node objects and store them in the variables tree1, tree2 and tree3.
A tree is a hierarchical data structure composed of linked nodes. The highest node is called the root, which has branches that link it to other nodes, which are themselves roots of their respective subtrees. MIT's simple example is a tree of eight nodes labelled by their type: a "root" with two "child" nodes; the left "child" has a "child" of its own (with two "leaf" nodes under it) and a "leaf", and the right "child" has a single "leaf".
We can make a few observations:
The Node class. In this problem set, we will be using a provided Node object to represent trees; it is already at the top of your editor. The tree whose root 1 has the left child 2 and the right child 5, where 5 has the left child 7 and the right child 8, can be initialized with the Node object as follows:
example_tree = Node(1, Node(2), Node(5, Node(7), Node(8))) print(example_tree.get_value()) # 1 print(example_tree.get_left_child().get_value()) # 2 print(example_tree.get_right_child().get_right_child().get_value()) # 8 print(example_tree.get_left_child().get_left_child()) # None
A brief explanation of the Node class:
Node(value, left_child, right_child). value holds the value held in the node, left_child optionally holds the Node constructing the left subtree, and right_child does the same for the right subtree. If there is not a subtree, either do not input that parameter or pass in None.Node object holding the left or right subtrees with get_left_child() or get_right_child() respectively. If there is no child, this function returns None.Node with get_value().Node draws its tree one level per line, with / and a backslash for the branches. print(example_tree) displays:1 / \ 2 5 / \ 7 8
The trees to build. Create objects accurately representing the data, and put them into the variables tree1, tree2 and tree3. Every node holds an integer, and left and right matter.
8. Its left child is 2, whose left child is 1 and right child is 6. Its right child is 10, a leaf.7. Its left child is 2; that 2 has the leaf 1 as its left child and 5 as its right child, and 5's children are 3 (left) and 6 (right). The root's right child is 9, whose left child is 8 and right child is 10.5. Its left child is 3, whose left child is 2 and right child is 4. Its right child is 14; that 14 has the leaf 12 as its left child and 21 as its right child, and 21's children are 20 (left) and 26 (right).With the trees right, print(tree1) displays
8 / \ 2 10 / \ 1 6
print(tree2) displays
7 / \ 2 9 / \ / \ 1 5 8 10 / \ 3 6
and print(tree3) displays
5 / \ 3 14 / \ / \ 2 4 12 21 / \ 20 26
Adapted for the browser: the Node class is pasted into the editor instead of imported from tree.py, and MIT's pictures of the trees are given in words and as what print displays.