you are going to create a Binary Search Tree (BST) with a minimal interface. You don't...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
you are going to create a Binary Search Tree (BST) with a minimal interface. You don't have to balance the tree. In fact, don't even try it yet. We are creating a very basic tree class. For the sake of testing, we are going to restrict this tree to use Integers rather than the generic Object class. Like all programming assignments, work on it in parts. Part 1: Node Class Overview In recursively defined structures, like trees, all the coding (and complexity) is found in the recursive structure itself. In the case of trees, the node will contain the vast amount of the logic and behavior. The node will use a Key-Value system. The key is used to store and find nodes. The value field is completely passive.. Interface Create a very basic node class. All this requires is a left and right link (to another node) and a generic data field. It should also have a few constructors. In particular, you need one that will create a node with links to two other nodes. public class Node Node Node int void Node (int key, string value) void left right string value key print (int indent) add (int key, String value) Constructor that assigns the value and key. The key. This key will be used to store the node into the tree. The value that the node contains. This method will the structure of the tree. One node will be printed per line. You should use preorder tree traversal. Feel free to redirect the stream if you like. Please see the pseudocode below. Adds the key to the correct position in the BST. If the key already exists, do nothing. Pseudocode Class Node Node left Node right int key string value All your methods go here End Class Adding a Key To add a key, you will write a recursive method that will either recurse to the left or right - depending on the key. Whenever it can no longer recurse left or right, a new node is simply added. Method add (int newKey, string newValue) If this node's key < newKey then If left isn't null else End If left.add(newkey, newValue) End If else create a new left child for this node. If this node's key > newKey then If right isn't null End If End Method right.add(newkey, newValue) create a right left child for this node. End If Printing the Tree To print the tree, you will use recursion using a preorder depth-first traversal. Don't worry, it is not hard. Function Print (int indent) Print spaces for indent Print the "+" Print this.value and a newline left.print(indent + 1) right.print (indent + 1) End Function Part 2: BinarySearchTree Class Overview The main class will contain virtually node code. Instead, this class is used to start recursion on the root node. All key/values are added to the using the add() method. It will find the correct position in the tree and store it there. Interface Just like before, the BinarySearch Tree's methods will start recursion on the Node class. public class BinarySearchTree string void void string Pseudocode BinarySearchTree () about () end class print() add (int key, string value) find (int key) class BinarySearchTree private Node root All your methods go here Constructor. Returns text about you - the author of this class. Start recursion from the root node. Note: It starts with an indent of 0. Adds the key to the correct position in the BST. If the key already exists, do nothing. So, basically, you are creating a proper-set of numbers. Finds a node with the key and returns its value. If the node is not found, you can return an empty string. Some Examples For example, if the following numbers are added to the Binary Search Tree. 10, 40, 30, 5, 74, 7, 17, 35, 3 It will result in the following tree. I've displayed the tree in both the text (using the Print method) and graphical version. +10 +-- 5 +3 + 7 +40 +30 +17 +35 +-- 74 40, 10, 30, 5, 74, 7, 17, 35, 3 +40 +10 + 5 + + +30 3 7 Binary Search Trees are extremely sensitive to the order that data is fed into them. In fact, once node is added, it's position in the tree will never change. In the example below, I've added the same numbers, but I have switched the position of the first two. +17 +35 +-- 74 3 Observe that, this minor change of order, has had a profound impact on the structure of the tree. The first key added will always become the root. And it will remain the root. w 7 7 10 17 10 30 17 40 40 35 30 74 35 4 74 Part 3: Testing Once you have finished your code, you need to test it using some good test data. Now you can see why you wrote the PrintTree method. It is vital to seeing if your methods are working correctly. For example, the following is a basic demonstration of the add() method. Binary SearchTree tree = new BinarySearchTree (); tree.add (10, "Buffalo Wings"); tree.add(43, "Cheez Whiz"); tree.add(18, "Root beer"); tree.add (6, "Pringles"); tree.add(50, "Ice Cream"); tree.add (8, "Chocolate"); tree.printTree (); Should produce the following tree: +- 10: Buffalo Wings +6: Pringles +8: Chocolate +43: Cheez Whiz +18: Root beer + 50: Ice Cream you are going to create a Binary Search Tree (BST) with a minimal interface. You don't have to balance the tree. In fact, don't even try it yet. We are creating a very basic tree class. For the sake of testing, we are going to restrict this tree to use Integers rather than the generic Object class. Like all programming assignments, work on it in parts. Part 1: Node Class Overview In recursively defined structures, like trees, all the coding (and complexity) is found in the recursive structure itself. In the case of trees, the node will contain the vast amount of the logic and behavior. The node will use a Key-Value system. The key is used to store and find nodes. The value field is completely passive.. Interface Create a very basic node class. All this requires is a left and right link (to another node) and a generic data field. It should also have a few constructors. In particular, you need one that will create a node with links to two other nodes. public class Node Node Node int void Node (int key, string value) void left right string value key print (int indent) add (int key, String value) Constructor that assigns the value and key. The key. This key will be used to store the node into the tree. The value that the node contains. This method will the structure of the tree. One node will be printed per line. You should use preorder tree traversal. Feel free to redirect the stream if you like. Please see the pseudocode below. Adds the key to the correct position in the BST. If the key already exists, do nothing. you are going to create a Binary Search Tree (BST) with a minimal interface. You don't have to balance the tree. In fact, don't even try it yet. We are creating a very basic tree class. For the sake of testing, we are going to restrict this tree to use Integers rather than the generic Object class. Like all programming assignments, work on it in parts. Part 1: Node Class Overview In recursively defined structures, like trees, all the coding (and complexity) is found in the recursive structure itself. In the case of trees, the node will contain the vast amount of the logic and behavior. The node will use a Key-Value system. The key is used to store and find nodes. The value field is completely passive.. Interface Create a very basic node class. All this requires is a left and right link (to another node) and a generic data field. It should also have a few constructors. In particular, you need one that will create a node with links to two other nodes. public class Node Node Node int void Node (int key, string value) void left right string value key print (int indent) add (int key, String value) Constructor that assigns the value and key. The key. This key will be used to store the node into the tree. The value that the node contains. This method will the structure of the tree. One node will be printed per line. You should use preorder tree traversal. Feel free to redirect the stream if you like. Please see the pseudocode below. Adds the key to the correct position in the BST. If the key already exists, do nothing. Pseudocode Class Node Node left Node right int key string value All your methods go here End Class Adding a Key To add a key, you will write a recursive method that will either recurse to the left or right - depending on the key. Whenever it can no longer recurse left or right, a new node is simply added. Method add (int newKey, string newValue) If this node's key < newKey then If left isn't null else End If left.add(newkey, newValue) End If else create a new left child for this node. If this node's key > newKey then If right isn't null End If End Method right.add(newkey, newValue) create a right left child for this node. End If Pseudocode Class Node Node left Node right int key string value All your methods go here End Class Adding a Key To add a key, you will write a recursive method that will either recurse to the left or right - depending on the key. Whenever it can no longer recurse left or right, a new node is simply added. Method add (int newKey, string newValue) If this node's key < newKey then If left isn't null else End If left.add(newkey, newValue) End If else create a new left child for this node. If this node's key > newKey then If right isn't null End If End Method right.add(newkey, newValue) create a right left child for this node. End If Printing the Tree To print the tree, you will use recursion using a preorder depth-first traversal. Don't worry, it is not hard. Function Print (int indent) Print spaces for indent Print the "+" Print this.value and a newline left.print(indent + 1) right.print (indent + 1) End Function Part 2: BinarySearchTree Class Overview The main class will contain virtually node code. Instead, this class is used to start recursion on the root node. All key/values are added to the using the add() method. It will find the correct position in the tree and store it there. Interface Just like before, the BinarySearch Tree's methods will start recursion on the Node class. public class BinarySearchTree string void void string Pseudocode BinarySearchTree () about () end class print() add (int key, string value) find (int key) class BinarySearchTree private Node root All your methods go here Constructor. Returns text about you - the author of this class. Start recursion from the root node. Note: It starts with an indent of 0. Adds the key to the correct position in the BST. If the key already exists, do nothing. So, basically, you are creating a proper-set of numbers. Finds a node with the key and returns its value. If the node is not found, you can return an empty string. Printing the Tree To print the tree, you will use recursion using a preorder depth-first traversal. Don't worry, it is not hard. Function Print (int indent) Print spaces for indent Print the "+" Print this.value and a newline left.print(indent + 1) right.print (indent + 1) End Function Part 2: BinarySearchTree Class Overview The main class will contain virtually node code. Instead, this class is used to start recursion on the root node. All key/values are added to the using the add() method. It will find the correct position in the tree and store it there. Interface Just like before, the BinarySearch Tree's methods will start recursion on the Node class. public class BinarySearchTree string void void string Pseudocode BinarySearchTree () about () end class print() add (int key, string value) find (int key) class BinarySearchTree private Node root All your methods go here Constructor. Returns text about you - the author of this class. Start recursion from the root node. Note: It starts with an indent of 0. Adds the key to the correct position in the BST. If the key already exists, do nothing. So, basically, you are creating a proper-set of numbers. Finds a node with the key and returns its value. If the node is not found, you can return an empty string. Some Examples For example, if the following numbers are added to the Binary Search Tree. 10, 40, 30, 5, 74, 7, 17, 35, 3 It will result in the following tree. I've displayed the tree in both the text (using the Print method) and graphical version. +10 +-- 5 +3 + 7 +40 +30 +17 +35 +-- 74 40, 10, 30, 5, 74, 7, 17, 35, 3 +40 +10 + 5 + + +30 3 7 Binary Search Trees are extremely sensitive to the order that data is fed into them. In fact, once node is added, it's position in the tree will never change. In the example below, I've added the same numbers, but I have switched the position of the first two. +17 +35 +-- 74 3 Observe that, this minor change of order, has had a profound impact on the structure of the tree. The first key added will always become the root. And it will remain the root. w 7 7 10 17 10 30 17 40 40 35 30 74 35 4 74 Some Examples For example, if the following numbers are added to the Binary Search Tree. 10, 40, 30, 5, 74, 7, 17, 35, 3 It will result in the following tree. I've displayed the tree in both the text (using the Print method) and graphical version. +10 +-- 5 +3 + 7 +40 +30 +17 +35 +-- 74 40, 10, 30, 5, 74, 7, 17, 35, 3 +40 +10 + 5 + + +30 3 7 Binary Search Trees are extremely sensitive to the order that data is fed into them. In fact, once node is added, it's position in the tree will never change. In the example below, I've added the same numbers, but I have switched the position of the first two. +17 +35 +-- 74 3 Observe that, this minor change of order, has had a profound impact on the structure of the tree. The first key added will always become the root. And it will remain the root. w 7 7 10 17 10 30 17 40 40 35 30 74 35 4 74 Part 3: Testing Once you have finished your code, you need to test it using some good test data. Now you can see why you wrote the PrintTree method. It is vital to seeing if your methods are working correctly. For example, the following is a basic demonstration of the add() method. Binary SearchTree tree = new BinarySearchTree (); tree.add (10, "Buffalo Wings"); tree.add(43, "Cheez Whiz"); tree.add(18, "Root beer"); tree.add (6, "Pringles"); tree.add(50, "Ice Cream"); tree.add (8, "Chocolate"); tree.printTree (); Should produce the following tree: +- 10: Buffalo Wings +6: Pringles +8: Chocolate +43: Cheez Whiz +18: Root beer + 50: Ice Cream Part 3: Testing Once you have finished your code, you need to test it using some good test data. Now you can see why you wrote the PrintTree method. It is vital to seeing if your methods are working correctly. For example, the following is a basic demonstration of the add() method. Binary SearchTree tree = new BinarySearchTree (); tree.add (10, "Buffalo Wings"); tree.add(43, "Cheez Whiz"); tree.add(18, "Root beer"); tree.add (6, "Pringles"); tree.add(50, "Ice Cream"); tree.add (8, "Chocolate"); tree.printTree (); Should produce the following tree: +- 10: Buffalo Wings +6: Pringles +8: Chocolate +43: Cheez Whiz +18: Root beer + 50: Ice Cream
Expert Answer:
Answer rating: 100% (QA)
A Binary Search Tree BST is a tree in which all the nodes follow the belowmentioned properties The value of the key of the left subtree is less than t... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Compute for the fringe benefits tax due in the following scenarios. During 201A, Alpha Corporation gave the following fringe benefits to its employees: Salaries to rank and file employees P1,000,000...
-
Airdrie Services Inc. started business on March 1, 2014. The company produces monthly financial statements and had total sales of $600,000 (of which $570,000 were on account) during the first four...
-
Model and solve by the Laplace transform: In Prob. 38, let m 1 = m 2 = 10 kg, k 1 = k 3 = 20 kg/sec 2 , k 2 = 40 kg/sec 2 . Find the solution satisfying the initial conditions y 1 (0) = y 2 (0) = 0,...
-
Identify the most appropriate approach and then design. Provide a rationale as to why this approach and design would be most appropriate. Develop a hypothetical research scenario that would...
-
Rozema, Inc., produces chemicals for large biotech companies. It has the following data for manufacturing overhead costs during August 2013: Use F for favorable and U forunfavorable: Fixed $18,000...
-
Defining the database management system and identifying the differences between a database and a database management system (DBMS) or relational database management system (RDBMS). Please provide the...
-
Values for the NASDAQ composite index during the 1,500 days preceding March 10, 2006, can be downloaded from the authors web site. Calculate the one-day 99% VaR and the one-day 99% ES on March 10,...
-
Department of economics econ: multinational corporations reading response task i within one and a half day( 36hours) answer the question based on the reading materials and don't plagiarize! read the...
-
Describe 5 components of Human Capital discussed in the class and give some examples from your own life and readings. How would you use this model if tasked to hire new shift managers at a restaurant?
-
Discuss risks and financial factors associated with exchange and interest rates to assess how they inform Pfizer's financial management approaches. Provide resources to support your response.
-
What type of analysis is the discounted payback period method? Sensitivity analysis Risk analysis Supplementary analysis Break-even analysis
-
Identify and assess the current competitive generic strategy of Marriott International and theconditions that created that strategy (internal and/or external). Identify any changes in strategy that...
-
3. Large companies, such as General Motors Corporation, usually have a multidivisional organizational structure. Describe two distinct advantages which this type organizational structure has over the...
-
Brands include social media lingo in ads designed for traditional media The practice is known as borrowed interest For example, an ad using this tactic might leverage the word like and a thumbs-up...
-
The figure shows a bolted lap joint that uses SAE grade 8 bolts. The members are made of cold-drawn AISI 1040 steel. Find the safe tensile shear load F that can be applied to this connection if the...
-
Show that the call to PIVOT in line 12 of SIMPLEX never decreases the value of .
-
Give three feasible solutions to the linear program in (29.24)(29.28). What is the objective value of each one?
-
Show how to use property 2 of Lemma 16.12 to determine in time O(|A|) whether or not a given set A of tasks is independent. Lemma 16.12 For any set of tasks A, the following statements are...
-
How many board members did Ramalinga Raju implicate in the fraud?
-
How much money did the Rajus earn from sales of Satyam stock in the eight years preceding the fraud revelation?
-
When the fraud was revealed who took control of the company?
Study smarter with the SolutionInn App