Question

Code in C++ Instructions Write a program which... Prompt the user to enter the number of...

Code in C++

Instructions

Write a program which...


Prompt the user to enter the number of values that they would like to insert into a binary search tree.


Prompt the user to insert, one-at-a-time, a value into the binary search tree.


Print the preorder, postorder, and inorder traversal on the tree.


Binary Search Tree

The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see the examples for the correct format of this output).

R in preorderR, postorderR, and inorderR stands for Recursive. So, you need to write recursive functions for these operations.

Hint: The copy and the deleteTree methods are also tree traversals. The copy is a preorder traversal and you'll want to insert (method) a new value into a based on an existing node. The deleteTree method will delete (keyword) a node based on a postorder traversal. You'll be writing 5 traversals (2 preorder, 2 postorder, and 1 in order) traversal for this assignment.

Sample Output

Here's the output with 7 values (5, 3, 7, 2, 4, 6, 8). This is a perfectly balanced tree.

How many values do you wish to insert? 7
1: 5
2: 3
3: 7
4: 2
5: 4
6: 6
7: 8

Preorder : 5 3 2 4 7 6 8
Postorder : 2 4 3 6 8 7 5
Inorder   : 2 3 4 5 6 7 8

Here is an example of the values 1 to 5 in order.

How many values do you wish to insert? 5
1: 1
2: 2
3: 3
4: 4
5: 5

Preorder : 1 2 3 4 5
Postorder : 5 4 3 2 1
Inorder   : 1 2 3 4 5

Here's Tommy Tutone's "Jenny". This is your typical state with a variety of levels to the tree.

How many values do you wish to insert? 7
1: 8
2: 6
3: 7
4: 5
5: 3
6: 0
7: 9

Preorder : 8 6 5 3 0 7 9
Postorder : 0 3 5 7 6 9 8
Inorder   : 0 3 5 6 7 8 9

Make sure your algorithm works with a tree with no values. Everything should come up blank.

How many values do you wish to insert? 0

Preorder :
Postorder :
Inorder   :

Files

Here is a listing of all files that I created for this assignment. These files should be included in your final submission.

BSTree.hpp, main.cpp

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Code in C++ Instructions Write a program which... Prompt the user to enter the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • There are generally considered to be four distinct binary tree traversals: preorder, inorder, postorder and level-order....

    There are generally considered to be four distinct binary tree traversals: preorder, inorder, postorder and level-order. Consider the following questions about these different kinds of traversals. Answer one of them that has not already been answered. What is the result of the various tree traversals when performed on an arithmetic expression tree? Which of the traversals are depth-first? Which are breadth-first? Which kind of traversal of a binary search tree produces the values in sorted order? Which of the traversals...

  • 1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only)....

    1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only). 2- Apply your proposed algorithm in the previous point to construct the binary tree with the following traversals (java code only):      In order traversal: 9 8 6 1 2 5 4      Postorder traversal: 9 6 1 8 5 4 2

  • QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem...

    QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem 3). However, we do have the following information: performing an inorder and a postorder traversals of the tree (containing nine integer nodes with integer values from 1 through 9) yield the following outputs: a) Inorder (LNR): 123456789 b) Postorder (LRN): 1354 2 8 796 Based on the above traversal information, determine the shape of the binary tree. Note that there is no need to...

  • Question 25 3 pts Add the following values (in the order provided) to a binary search...

    Question 25 3 pts Add the following values (in the order provided) to a binary search tree and provide the values in the order you would get if you did an inorder traversal of the tree. Values: 6, 3, 7,4,9,1,0, 8, 5, 2 Question 26 3 pts Add the following values (in the order provided) to a binary search tree and provide the values in the order you would get if you did an postorder traversal of your tree. Values:...

  • a) Create a binary search tree with the 4 2 2 0 1 4 5 9...

    a) Create a binary search tree with the 4 2 2 0 1 4 5 9 7 1 5 3 6 number. b) Provide the Preorder, Inorder and Postorder traversal of tree obtained in part (a). Show all the steps.

  • Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10,...

    Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10, 11 into an empty binary search tree in the given order. Show the resulting BST after every insertion. (b) 5 points) What are the preorder, inorder, and postorder traversals of the BST you have after (a)? (c) 5 points Delete 2, 7, 5, 6, 11 from the BST you have after (a) in the given order Show the resulting BST after every deletion.

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48,...

    Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48, 30 and write its preorder, inorder and postorder traversal. (Needs to be in C#)

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT