Question

In: Computer Science

• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a...

• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a preorder and a Breadth First Search (BFS) way.

• The program gets a parameter ? from the command line and stores ? randomly generated integers in a dynamic array. To generate a random integer between 1 and Max-Rand (a system constant):

▪ Use srand() to seed the rand() function. Then, call rand() again and again (? times) to generate ? random integers. Next, printout the values stored in the dynamic array.

• Next, the program stores the content of the array into a BST.

• Next, the program performs a preorder exploration of the BST and prints out the contents of the tree according to the pre-order exploration during visit.

• Finally, the program performs BFS on the BST and prints out the content of the tree according to the BFS visit order.

▪ You can use queue for the BFS.

Solutions

Expert Solution

Answer:

I have written the code based on your requirements.

The code is error-free and it is working perfectly.

I have also added the output-screenshot that I got by running the below program.

Output:

Enter an Integer : 10


Preorder :1458127707 687154677 433243266 218170988 810241110 797489018 1751937168 1480755986 1595744206 1533785652   

Breadth First Search :
1458127707 687154677 1751937168 433243266 810241110 1480755986 218170988 797489018 1595744206 1533785652

Code:

#include<bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left,*right;
};

Node *newnode(int data)
{
Node *temp=new Node();
temp->data=data;
temp->left=NULL;
temp->right=NULL;
  
return temp;
}

Node *insert(Node *root,int data)
{
if(root==NULL)
{
return newnode(data);
}
else
{
if(data < root->data)
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,data);
}
  
return root;
}
}


void preorder(Node *root)
{
if(root!=NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}

void bfs(Node *root)
{
if(root==NULL)
return;
  
cout<<"\n\nBreadth First Search : \n";
  
queue<Node*> q;
  
q.push(root);
  
while(!q.empty())
{
  

  
Node * top=q.front();
q.pop();
  
cout<<top->data<<" ";
  
if(top->left)
{
q.push(top->left);
}
if(top->right)
{
q.push(top->right);
}
  
  
  
  
}
  
}
int main()
{
Node *root=NULL;
  
int n;
  
cout<<"Enter an Integer : ";
cin>>n;
  
int arr[n];
  
srand(time(0));
  
for(int i=0;i<n;i++)
{
arr[i]=rand()%RAND_MAX+1;
}
  
for(int i=0;i<n;i++)
{
root=insert(root,arr[i]);
}
  
  
cout<<"\n\nPreorder :";
preorder(root);
  
bfs(root);
  
}


Related Solutions

What is a binary search tree or BST? Discuss its characteristics. What are other types of...
What is a binary search tree or BST? Discuss its characteristics. What are other types of binary trees as well?
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports an efficient procedure which, on input (x, k) where x is the root of a BST and k an integer, output the k-th smallest number store in the BST. Let n denote the total number of elements stored in the BST, what is the running time of your efficient procedure? How does your modification of the BST data structure affect the performance of other...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
In this assignment you will create a Binary Search Tree to storeand retrieve objects of...
In this assignment you will create a Binary Search Tree to store and retrieve objects of type ItemType. The purpose of this assignment is for you to become familiar with basic tree operations, and understand the efficiency of trees compared to previously studied data structures. Binary Tree nodes have only two children, left and right. Nodes are compared based on their Key instance variable, which in this assignment is of type ItemType. All elements in the left subtree of a...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT