Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Following the given requirements, write a python code that outputs a binary tree similar to the one shown. The number of nodes should reflect any
Following the given requirements, write a python code that outputs a binary tree similar to the one shown. The number of nodes should reflect any email address, can use you123.drexeledu as an example.
PYTHON PROGRAM REQUIREMENTS 1. Your program must define a binary Tree class with the following specifications: 2 (a) Your binaryTree class must have as data attributes a dictionary called node Children that maps a node to its (left_child, right_child) nodes, a dictionary called node Parent that maps a node to its parent node, and a dictionary called treexy that maps a node to its (x,y) location. (b) Your binaryTree class must have a method called addTrio (parent, left_child, right_child ) that adds a new pair of nodes (left_child, right_child) to the tree. If parent is the root then parent is added to the tree as well. (c) Your binaryTree class must have a method called leaves that returns a list of all leaf nodes in the tree and a method called nodes that returns a list of all nodes in the tree. (d) Your binary Tree class must have a method called addNodes (nNodes) adds at most nNodes to the tree, plus an additional root node if the tree is empty. For example, calling addNodes (4) on an empty tree would result in a tree with 5 nodes. Calling addNodes (3) on a tree that already has 3 nodes also results in a tree with 5 nodes since we don't need to add a new root node in this case. Why does the number of nodes have to be odd? Calling addNodes nNodes) on a non-empty tree adds nNodes additional nodes to the tree. NOTE - the function addNodes in the template provided in Zybooks will implement this for you correctly. (e) Your binaryTree class must have a method called dfs Traverse that returns a depth first search ordered list of nodes in the tree. See Figure 1 and the code template in Zybooks for more detail. (f) Your binaryTree class must have a method called render that produces a plot of a binary tree with the number of nodes determined as described below. The leaf nodes are placed at integer locations in increasing order of their discovery by the depth first search. Parent nodes are set midway between each leaf node. Each node should have a vertical line extending from their node.y to node.y+1. Node labels should be printed at their (x,y) location, with left_child nodes right aligned, and right_child nodes left aligned. The root text label should be centered. 2. Your program must include a function at global scope called my NumberOfNodes that maps your drexel email address to the number of nodes in the plot that you will submit. def my NumberOf Nodes (email Address): # pass your email address (you123@drexel.edu) as the parameter hash=0 for cin email Address: hash+=ord (c) return 17+ hash%15 Be sure to put your own Drexel email in for the emailAddress parameter. 3 For this programming assignment, you will develop a Python class that implements the functions and data management associated with binary trees. The programming template contains a list of the class methods and member variables that you must implement. 1 2 3 4 5 6 Algorithm 1: Traversing a binary tree with depth first search to set (x,y) render locations for each node. Result: A list of node_ids in depth first search order, with x and y values for each node Initialize the binary tree structure with N nodes, initialize a variable XMAX to store the rightmost leaf node found so far, and call dfs Traverse initially on the root node. def dfs Traverse (node , XMAX=0 , y=0) : # if node has children # traverse left child first (dfs, XMAX) = dfs Traverse (node.left_child , XMAX, child_y) # we get to this line after all our descendents left_children have been processed (dfs , XMAX) = dfs Traverse (node.left_child , XMAX, child_y) # set parent node locations midway between children node .x = (node.left_child . x+node .right_child. x)/2.0 node . y=y # if node has children, set leaf location at next integer value node . x = XMAX increment y at each generation, so the root is at y=0, its children at y=1, etc...) node .y = y XMAX += 1 # after processing children, add node to dfs list dfs.append (node) return (dfs , XMAX) 7 8 9 10 no 11 12 # we are 13 14 15 16 17 Figure 1 - binary tree with 9 nodes 0 1 generation 3 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 PYTHON PROGRAM REQUIREMENTS 1. Your program must define a binary Tree class with the following specifications: 2 (a) Your binaryTree class must have as data attributes a dictionary called node Children that maps a node to its (left_child, right_child) nodes, a dictionary called node Parent that maps a node to its parent node, and a dictionary called treexy that maps a node to its (x,y) location. (b) Your binaryTree class must have a method called addTrio (parent, left_child, right_child ) that adds a new pair of nodes (left_child, right_child) to the tree. If parent is the root then parent is added to the tree as well. (c) Your binaryTree class must have a method called leaves that returns a list of all leaf nodes in the tree and a method called nodes that returns a list of all nodes in the tree. (d) Your binary Tree class must have a method called addNodes (nNodes) adds at most nNodes to the tree, plus an additional root node if the tree is empty. For example, calling addNodes (4) on an empty tree would result in a tree with 5 nodes. Calling addNodes (3) on a tree that already has 3 nodes also results in a tree with 5 nodes since we don't need to add a new root node in this case. Why does the number of nodes have to be odd? Calling addNodes nNodes) on a non-empty tree adds nNodes additional nodes to the tree. NOTE - the function addNodes in the template provided in Zybooks will implement this for you correctly. (e) Your binaryTree class must have a method called dfs Traverse that returns a depth first search ordered list of nodes in the tree. See Figure 1 and the code template in Zybooks for more detail. (f) Your binaryTree class must have a method called render that produces a plot of a binary tree with the number of nodes determined as described below. The leaf nodes are placed at integer locations in increasing order of their discovery by the depth first search. Parent nodes are set midway between each leaf node. Each node should have a vertical line extending from their node.y to node.y+1. Node labels should be printed at their (x,y) location, with left_child nodes right aligned, and right_child nodes left aligned. The root text label should be centered. 2. Your program must include a function at global scope called my NumberOfNodes that maps your drexel email address to the number of nodes in the plot that you will submit. def my NumberOf Nodes (email Address): # pass your email address (you123@drexel.edu) as the parameter hash=0 for cin email Address: hash+=ord (c) return 17+ hash%15 Be sure to put your own Drexel email in for the emailAddress parameter. 3 For this programming assignment, you will develop a Python class that implements the functions and data management associated with binary trees. The programming template contains a list of the class methods and member variables that you must implement. 1 2 3 4 5 6 Algorithm 1: Traversing a binary tree with depth first search to set (x,y) render locations for each node. Result: A list of node_ids in depth first search order, with x and y values for each node Initialize the binary tree structure with N nodes, initialize a variable XMAX to store the rightmost leaf node found so far, and call dfs Traverse initially on the root node. def dfs Traverse (node , XMAX=0 , y=0) : # if node has children # traverse left child first (dfs, XMAX) = dfs Traverse (node.left_child , XMAX, child_y) # we get to this line after all our descendents left_children have been processed (dfs , XMAX) = dfs Traverse (node.left_child , XMAX, child_y) # set parent node locations midway between children node .x = (node.left_child . x+node .right_child. x)/2.0 node . y=y # if node has children, set leaf location at next integer value node . x = XMAX increment y at each generation, so the root is at y=0, its children at y=1, etc...) node .y = y XMAX += 1 # after processing children, add node to dfs list dfs.append (node) return (dfs , XMAX) 7 8 9 10 no 11 12 # we are 13 14 15 16 17 Figure 1 - binary tree with 9 nodes 0 1 generation 3 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started