Write a program in C++ to create a Binary Search tree (BST) of string data. The...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a program in C++ to create a Binary Search tree (BST) of string data. The program will perform these operations: Insert name(s), Traverse Preorder, Search name, Delete name, Leaf Count, sibling of a person and Quit. Use this header file: class BT { private: struct node { string data; node* left; node* right; node* root; BT(); }; public: bool isEmpty() const { return root == NULL; } void insert(string); void print preorder(); void preorderTrav(node*); void searchBST (string); void deleteNode(string); int count(); int leafCount(node*); void nodeSibling(string); }; //Constructor //Check for empty //Insert item in BST //Preorder traversing driver //Preorder traversing //Searches BST for a specific node //Delete item in BST //Count driver //Counts number of leaves in BST //Finds sibling of a node Use the following menu in your program. Must place your name in the menu heading. MENU (YOUR NAME)- 1. Insert name(s) 2. Traverse Preorder 3. Search name 4. Delete name 5. Leaf Count 6. Sibling of a person 7. Quit Enter your choice: Use the following names in the given order to form the BST tree: KIM PAM DON TOM RON BEN AMY Option 1: Inserts name(s) in a BST. Option 2: Option 3: Enter Your Choice <1 - 7> 1 Enter number of items to insert: 4 Enter node: KIM Inserted. Enter node: PAM Inserted. Enter node: DON Inserted. Enter node: TOM Inserted. Traverse the tree in Preorder and display the node info and its left child info and right child info. If a node does not have a child then display NIL. Enter Your Choice <1 - 7> 2 Sample output: Traversing Preorder Node Info Left Child Info Right Child Info KIM DON PAM DON NIL NIL PAM NIL TOM NIL TOM NIL Search for the name in the BST. It will prompt: Enter name you want to search for: KIM If the name is found, then display the name and the left child information and right child information. If the item is not found, then it will display - " is not found in the BST" Node Info Left Child Info Right Child Info KIM DON PAM Option 4: Option 5: Option 6: Deletes a name from the BST. It will prompt: Enter name you want to delete: TOM If the name is found then delete the name from the BST and displays the message - " is deleted." If the name is not in the BST, then it will display - "_ is not found in the BST" TOM is deleted Counts the number of leaves in the BST and displays - "There are number of leaves in the BST." Enter a name and it will display the sibling of that person. It will prompt: Enter the name you want to find the sibling of: The sibling of is If the person has no sibling, then it displays: has no sibling. Ex: Enter the item: The sibling of PAM is DON. Option 7: Quit the program. PAM Write a program in C++ to create a Binary Search tree (BST) of string data. The program will perform these operations: Insert name(s), Traverse Preorder, Search name, Delete name, Leaf Count, sibling of a person and Quit. Use this header file: class BT { private: struct node { string data; node* left; node* right; node* root; BT(); }; public: bool isEmpty() const { return root == NULL; } void insert(string); void print preorder(); void preorderTrav(node*); void searchBST (string); void deleteNode(string); int count(); int leafCount(node*); void nodeSibling(string); }; //Constructor //Check for empty //Insert item in BST //Preorder traversing driver //Preorder traversing //Searches BST for a specific node //Delete item in BST //Count driver //Counts number of leaves in BST //Finds sibling of a node Use the following menu in your program. Must place your name in the menu heading. MENU (YOUR NAME)- 1. Insert name(s) 2. Traverse Preorder 3. Search name 4. Delete name 5. Leaf Count 6. Sibling of a person 7. Quit Enter your choice: Use the following names in the given order to form the BST tree: KIM PAM DON TOM RON BEN AMY Option 1: Inserts name(s) in a BST. Option 2: Option 3: Enter Your Choice <1 - 7> 1 Enter number of items to insert: 4 Enter node: KIM Inserted. Enter node: PAM Inserted. Enter node: DON Inserted. Enter node: TOM Inserted. Traverse the tree in Preorder and display the node info and its left child info and right child info. If a node does not have a child then display NIL. Enter Your Choice <1 - 7> 2 Sample output: Traversing Preorder Node Info Left Child Info Right Child Info KIM DON PAM DON NIL NIL PAM NIL TOM NIL TOM NIL Search for the name in the BST. It will prompt: Enter name you want to search for: KIM If the name is found, then display the name and the left child information and right child information. If the item is not found, then it will display - " is not found in the BST" Node Info Left Child Info Right Child Info KIM DON PAM Option 4: Option 5: Option 6: Deletes a name from the BST. It will prompt: Enter name you want to delete: TOM If the name is found then delete the name from the BST and displays the message - " is deleted." If the name is not in the BST, then it will display - "_ is not found in the BST" TOM is deleted Counts the number of leaves in the BST and displays - "There are number of leaves in the BST." Enter a name and it will display the sibling of that person. It will prompt: Enter the name you want to find the sibling of: The sibling of is If the person has no sibling, then it displays: has no sibling. Ex: Enter the item: The sibling of PAM is DON. Option 7: Quit the program. PAM
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
What particular strengths of the relational model have led to the pre-eminent position that it holds today as a vehicle for database management? [8 marks] Identify any weaknesses in the model,...
-
answer the question clearly You are building a flight-control system for which a convincing safety case must be made. Would you assign the tasks of safety requirements engineering, test case...
-
The functions in Exercises 1128 are all one-to-one. For each function, a. Find an equation for f -1 (x), the inverse function. b. Verify that your equation is correct by showing that f( f -1 (x)) = x...
-
Of two-car families in a small city, 88% remain two-car families in the following year and 12% become one-car families in the following year. Of one-car families, 72% remain one-car families and 28%...
-
The court found that the addressing of audit reports to the shareholders, while not conclusive, is a strong indication that P&T intended the shareholders to rely upon them. Do you agree, in general,...
-
A neutron is shot straight up with an initial speed of \(100 \mathrm{~m} / \mathrm{s}\). As it rises, does its de Broglie wavelength increase, decrease, or not change? Explain.
-
Walter receives cash of $18,000 and land with a fair market value of $75,000 (adjusted basis of $50,000) in a current distribution. His basis in his partnership is $16,000. a. What amount of gain...
-
Lama and Joan sell electronic equipment and supplies through their partnership. They wish to expand their computer lines and decide to admit Marah to the partnership. Lama's capital is $200,000,...
-
What are the advantages to foreign companies in setting up production in India?
-
What means of punishment for criminal offenses exist in your state? Is capital punishment available for persons convicted of first-degree murder? Which punishments, if any, do you think are most...
-
If you were CEO of a company, would you spend $15 million per year to put your companys name on a stadium?
-
Go to http://www.findlaw.com/casecode/supreme.html. Use this page to locate the Supreme Courts decision in Gonzales v. Raich (2005). Read the decision. Identify the key issue and the Courts holding....
-
In a bag of assorted candies, there are 13 Almond Joys and 7 Twizzlers left. You sample 5 candies at random. What is the probability that you get 2 or fewer Almond Joys?
-
How does your answer change if city residents spend 60 cents of every additional dollar on goods made in other cities or countries?
-
1. What is the difference between an accounts payable and a notes payable? Explain 2. How does goodwill arise? How is it accounted for and reported on the financial statements? 3. The following...
-
True & False The basis of an asset must be reduced by the depreciation allowable, 2. Adjusted gross income (AGI) is the basis for a number of phase-outs of deductions. 3. A change to adjusted gross...
-
The Captain of an interstellar photon-rocket spaceship wishes to maintain a constant acceleration \(a\) in the instantaneous rest-frame of the ship, since that would provide a constant effective...
-
Prove from Fermat's Principle that the angles of incidence and reflection are equal for light bouncing off a mirror. Use neither algebra nor calculus in your proof! (Hint: The result was proven by...
-
An ideal converging lens focusses light from a point object onto a point image. Consider only rays that are straight lines except when crossing an air-glass boundary, such as those shown in the...
Study smarter with the SolutionInn App