Help with an ASSIGNMENT! Please solve in JAVA using Binary Tree + Run Output wit
ID: 3834413 • Letter: H
Question
Help with an ASSIGNMENT!
Please solve in JAVA using Binary Tree + Run Output with screenshot. THANKS
EX 1: Reconstruct a Binary Search Tree (BST) with the given traversal sequence. The first value is the root.
88 50 22 223 125 43 400 31 300 12 60 55
EX 2: Given the following preorder and inorder of a BT, rebuild the BT.
Inorder: 89 28 32 46 78 57 60 53 98
Preorder: 78 32 89 28 46 60 57 98 53
EX 3: A social networking website stores information about its users in a binary search tree. In addition to the components that provide access to the left and right branches of the tree, each node in the tree contains a unique user number (the key value), the user's name, and a list of the names of each of the user's friends.
1. Write the data definitions needed for this binary search tree.
2. Write a method addNewUser which takes a user number, a user name, a list of friends, and adds a new user with the given information to the binary search tree. Make sure that the tree that is produced is a binary search tree. You may assume that the user number does not already exist in the given tree.
3. Write a method friendCount which takes a user number and returns the number of friends the person with the given user number has. You may assume the user number exists in the tree. Your function should be written efficiently, such that it performs as few comparisons in the tree as is necessary.
4. Write the main method to test your program
Please solve in JAVA using Binary Tree + Run Output with screenshot. THANKS
Explanation / Answer
public class BinaryTree { private int data; private BinaryTree left; private BinaryTree right; public BinaryTree(int num) { this.data = num; this.left = null; this.right = null; } // As a convention, if the key to be inserted is less than the key of root node, then key is inserted in // left sub-tree; If key is greater, it is inserted in right sub-tree. If it is equal, as a convention, it // is inserted in right sub-tree public void addNode(int num) { if (numRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.