subject
Engineering, 29.02.2020 01:00 quece233

I need help writing these functions for my code. In the homework this week you started reading and programming binary search trees. The one missing piece was inserting into a binary search tree; we'll take care of that today and write the insert function, as well as a height function. Both functions will be implemented in the "bst. h" header file, and tested using a provided main program in "main. cpp".Step one is to implement the insert function --- go ahead and modify "bst. h", adding the necessary code to (1) allocate a new node, and (b) link it into the tree. Once you have insert implemented, test your work using Codio's interactive terminal window. In this exercise we are building a tree of strings, with "#" as the sentinel. Example:#Size: 6Inorder: apple aunt dad mom pizza uncleHeight: 3You'll know the tree is built properly if the inorder output is in sorted order --- if not, then your insert function is not linking the new nodes into the tree correctly. Ignore the height for now (which is probably displayed as -1).Once insert is working, step two is to implement the height function in "bst. h". Since height needs to recursively traverse the entire tree, you'll want to take a public-private approach where a private helper function does the actual work of computing the height. Here is the main. cpp:#include #include #include "bst. h"using namespace std;int main(){binarysearchtree tree;string key; 1. Inputs values from the keyboard and builds a binary search// tree; reads input until the sentinel ("#") is input. The// resulting binary search tree is returned.//cin >> key;while (key != "#"){tree. insert(key);cin >> key;} 2. Output size and contents (in order)://cout << "Size: " << tree. size() << endl;tree. inorder(); 3. Output height://cout << "Height: " << tree. height() << endl;// done:return 0;}Here is the bst. h:#pragma once#include #include // std::maxusing namespace std;templateclass binarysearchtree{private:struct NODE{TKey Key;NODE* Left;NODE* Right;};NODE* Root; // pointer to root node of tree (nullptr if empty)int Size; // # of nodes in the tree (0 if empty) _inorder does the actual inorder traversal and output// to console. Each key is output to the console followed// by " ", including the last key.//void _inorder(NODE* cur){if (cur == nullptr)return;else{_inorder(cur-&g t;Left);cout << cur->Key << " ";_inorder(cur->Right);}}public: default constructor: Creates an empty tree.//binarysearchtree(){Root = nullptr;Size = 0;} size: Returns the # of nodes in the tree, 0 if empty.//int size(){return Size;} height Computes and returns height of tree; height of an empty tree is// defined as -1.//int height(){ TODO:// return -1;} search: Searches the tree for the given key, returning true if found// and false if not.//bool search(TKey key){NODE* cur = Root;while (cur != nullptr){if (key == cur->Key) // already in treereturn true;if (key < cur->Key) // search left:{cur = cur->Left;}else{cur = cur->Right;}}//while // if get here, not foundreturn false;} insert Inserts the given key into the tree; if the key has already been insert then// the function returns without changing the tree.//void insert(TKey key){NODE* prev = nullptr;NODE* cur = Root; 1. Search to see if tree already contains key://while (cur != nullptr){if (key == cur->Key) // already in treereturn;if (key < cur->Key) // search left:{prev = cur;cur = cur->Left;}else{prev = cur;cur = cur->Right;}}//while 2. if we get here, key is not in tree, so allocate// a new node to insert: TODO: allocate a new node, store key, initialize// pointer fields: 3. link in the new node: NOTE: cur is null, and prev denotes node where// we fell out of the tree. if prev is null, then// the tree is empty and the Root pointer needs// to be updated. TODO: link in the new node, updating Root// pointer as appropriate 4. update size and we're done:// TODO://} inorder: Performs an inorder traversal of the tree, outputting// the keys to the console.//void inorder(){cout << "Inorder: ";_inorder(Root);cout << endl;}};

ansver
Answers: 2

Another question on Engineering

question
Engineering, 04.07.2019 18:10
Afull journal bearing has a journal diameter of 27 mm, with a unilateral tolerance of -0.028 mm. the bushing bore has a diameter of 27.028 mm and a unilateral tolerance of 0.04 mm. the l/d ratio is 0.5. the load is 1.3 kn and the journal runs at 1200 rev/min. if the average viscosity is 50 mpa-s, find the minimum film thickness, the power loss, and the side flow for the minimum clearance assembly.
Answers: 1
question
Engineering, 04.07.2019 18:10
Ajournal bearing has a journal diameter of 3.250 in with a unilateral tolerance of 20.003 in. the bushing bore has a diameter of 3.256 in and a unilateral tolerance of 0.004 in. the bushing is 2.8 in long and supports a 700-lbf load. the journal speed is 900 rev/min. find the minimum oil film thickness and the maximum film pressure for both sae 20 and sae 20w-30 lubricants, for the tightest assembly if the operating film temperature is 160°f. a computer code is appropriate for solving this problem.
Answers: 3
question
Engineering, 04.07.2019 18:10
Carbon dioxide gas expands isotherm a turbine from 1 mpa, 500 k at 200 kpa. assuming the ideal gas model and neglecting the kinetic and potential energies, determine the change in entropy, heat transfer and work for each kilogram of co2.
Answers: 2
question
Engineering, 04.07.2019 18:20
Asolid cylinder is concentric with a straight pipe. the cylinder is 0.5 m long and has an outside diameter of 8 cm. the pipe has an inside diameter of 8.5 cm. the annulus between the cylinder ad the pipe contains stationary oil. the oil has a specific gravity of 0.92 and a kinematic viscosity of 5.57 x 10-4 m2/s. most nearly, what is the force needed to move the cylinder along the pipe at a constant velocity of 1 m/s?
Answers: 3
You know the right answer?
I need help writing these functions for my code. In the homework this week you started reading and p...
Questions
question
Mathematics, 24.03.2020 21:07
question
Mathematics, 24.03.2020 21:08
question
History, 24.03.2020 21:08
question
Mathematics, 24.03.2020 21:08
Questions on the website: 13722363