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...
-
Suppose Prudential Bache (PB) buys $1,000,000 of CitiCorp bonds at a price of 101. The CitiCorp bonds pay cash interest at the annual rate of 7% and mature at the end of five years. 1. How much did...
-
1. How does a catapult work? 2. What type of simple machine is it? (incline plane, lever, pulley, screw, wedge, or wheel and axle) How do you know? http://www.mikids.com/Smachines.htm 3. How far were...
-
A recent study examined the effects of carbon monoxide exposure on a group of construction workers. The following table presents the numbers of workers who reported various symptoms, along with the...
-
The Daniels Tool & Die Corporation has been in existence for a little over three years. The company's sales have been increasing each year as it builds a reputation. The company manufactures dies to...
-
A city in South Florida is tracking traffic accident data during its peak season for tourism. View the chart below to see the data. Traffic Week Accidents 1 19 2 24 3 4 27 27 20 20 LO 5 29 29 6 31 7...
-
Brentwood Bay Inc.s comparative balance sheet at September 30, 2020, and its 2020 income statement are shown below: Other information for the year ended September 30, 2020: a. Acquired equipment by...
-
In the production of a biotherapeutic drug, a suspension containing media A and RO water are added to another suspension of media B in a bioreactor B-01 as shown in the schematic below. I Media...
-
The grain-size distribution of two soils is provided below. The liquid and plastic limits of No. 40 sieve fraction of the soil are as follows: Soil A: Liquid Limit = 30, Plastic Limit = 22 Soil B:...
-
Milo receives a commission of 2% on all sales. If his commission on a sale was $31.00 , find the cost of the item he sold.
-
How does International Benefits work? what issues need further research with international benefits? example of a international company benefit.
-
Lillibridge & Friends, Incorporated provides you with the following data for its single product: Sales price per unit Fixed costs (per quarter): Selling, general, and administrative (SG&A)...
-
How many minutes would you have to walk in November so that your mean for the 11 months would be 2 percentage points more than your mean for the 10 months? 178 + 182 + 188 +198 + 205 +219 +230 + 245...
-
Evaluate the integral. 3 1- x + x + 1 (x + 1)(x + 2) dx
-
Integration is a vital concept when applied in one?s life. Integrating your life means making ideal choices. Perfect choices on the other go in line with quality decisions. Quality decisions lead to...
-
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...
-
The gross profit ratio is equal to: (a) profit divided by net sales. (b) cost of sales divided by net sales. (c) net sales minus cost of sales, divided by net sales. (d) net sales minus cost of...
-
What inventory cost flow method does Dominos Pizza Enterprises Ltd use for its inventories?
-
Which inventory cost flow method produces the highest profit in a period of rising prices?
Study smarter with the SolutionInn App