Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Preorder Binary Search Tree You will read numbers from a ?le to construct a binary search tree with them. Then you will output a representation
Preorder Binary Search Tree You will read numbers from a ?le to construct a binary search tree with them. Then you will output a representation of this BST to an output ?le using preorder traversal. The input ?le will consist of a single line of numbers separated by spaces. You may assume that there will always be less than 100 numbers and they will all be unique. Your C++ program must read these numbers in order and add them to a binary search tree with the ?rst number becoming the root node. You must implement a node and/or a binary tree class yourself. Implement these however you like but do not use advanced standard library features such as maps. Remember that there is no need to implement node deletion functionality you only need to implement add and traversal functions. For the output, the program will create a ?le with one line per node in preorder including the nodes location in the tree. For example, if the input ?le is: 3 1 2 4 Your program will:
- Read 3, making it the root node
- Read 1, go to the left child of 3, since the left child is empty insert 1 as left child of 3
- Read 2, go to the left child of 3, go to the right child of 1, since the right child is empty insert 2 as right child of 1
- Read 4, go to the right child of 3, since the right child is empty insert 4 as right child of 3
- is at [xl] so you have to go root->left to access it
- is at [xlr] so you have to go root->left->right to access it 4 is at [xr] so you have to go root->right to access it input1.txt:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started