Question

Questions Binary Tree Problem You need to organize guests at a wedding based on last name,...

Questions

Binary Tree

Problem

You need to organize guests at a wedding based on last name, and you need to balance the room as well as possible as they arrive.

This program will demonstrate the following:

  • How to create a binary tree,
  • How to use a tree to organize data.

Solving the Problem

Step 1

To create a tree structure in Python, a class must be created to represent nodes in the tree. The initial node, called the root, will act as the pivotal point in the tree, and each new node added will be added to the left or right of the tree based on the value held in that node.

Step 2

You can now create the root node and give it an initial value to sort the guests on. Guests with names that start with letters less than this will be sorted to the left, and those with first letters that are greater will be sorted to the right.

Step 3

The easiest way to manipulate tree structures is with recursive functions. You will need to create a recursive function to add nodes to the tree based on the first letter of the guest’s name. The function will take in the root node and the guest to be added. It then traverses the tree, making comparisons and adding a new guest. If there is no node found, a new one is created. If there is a node found, then the function recurses further down into the tree until an empty node is found. If the names are the same, then the guest is added to the existing node’s data member.

Step 4

You can now add items to the tree, and they will be sorted to the left and right based on the value of the name given.

Step 5

To print the tree, another recursive function needs to be created to traverse the tree. In this code, a node is entered into the function, and the data are printed for that node if it is not null. The left and right nodes of the current node are then passed to the recursive call.

Step 6

You can now print either the entire tree or each side of the tree. In this case, you will print out each side separately.

Documentation Guidelines:

Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the '<' and '>' angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program's sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.

Deliverable(s):

Your deliverable should be a Word document with screenshots showing the source code and running results, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them for all of the programs listed above as well as the inputs and outputs from running them. Submit a cover sheet with the hardcopy of your work.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class TreeNode:
#constructor
def __init__(self,value):
self.value=value
#setting left and right nodes to None
self.left=None
self.right=None

#main method to print the tree
def print_tree(self):
#displaying a heading and printing left side
print('Left Side')
self.print_tree_rec(self.left)
#displaying a heading and printing right side, including root node
print('Right Side')
print("['{}']".format(self.value))
self.print_tree_rec(self.right)

#recursive method to print the tree
def print_tree_rec(self,node):
#checking if node is None (base condition)
if node!=None:
#printing node's value
print("['{}']".format(node.value))
#printing left subtree
self.print_tree_rec(node.left)
#printing right subtree
self.print_tree_rec(node.right)
#this type of traversal is called pre order traversal

#recursive method to add an element to the tree
def add(self, value):
#checking if value is less than this node's value
if self.value>value:
#if left is None, adding as left
if self.left==None:
self.left=TreeNode(value)
else:
#else, moving to left subtree recursively, and adding at proper position
self.left.add(value)
else:
#doing the same for right subtree
if self.right==None:
self.right=TreeNode(value)
else:
self.right.add(value)


#----test code----#
#creating an empty tree
tree=None
#reading 6 guest names and adding to tree
for i in range(6):
name=input('Add the guests name: ')
if tree==None:
tree=TreeNode(name)
else:
tree.add(name)
#printing tree
tree.print_tree()

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Questions Binary Tree Problem You need to organize guests at a wedding based on last name,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT