1. The programming language is C++ 2. Write a function that inserts the nodes of
ID: 3859384 • Letter: 1
Question
1. The programming language is C++
2. Write a function that inserts the nodes of a binary tree into an ordered linked list. Also write a program to test your function.
a. Do not change anything in the supplied Ch19_Ex9.cpp except to add documentation and your name.
b. Please use the file names listed below since your file will have the following components:
Ch19_Ex9.cpp shown below
//Data
//68 43 10 56 77 82 61 82 33 56 72 66 99 88 12 6 7 21 -999
#include <iostream>
#include "binarySearchTree.h"
#include "orderedLinkedList.h"
using namespace std;
int main()
{
bSearchTreeType<int> treeRoot;
orderedLinkedList<int> newList;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
while (num != -999)
{
treeRoot.insert(num);
cin >> num;
}
cout << endl << "Tree nodes in inorder: ";
treeRoot.inorderTraversal();
cout << endl;
cout << "Tree Height: " << treeRoot.treeHeight()
<< endl;
treeRoot.createList(newList);
cout << "newList: ";
newList.print();
cout << endl;
system("pause");
return 0;
}
binarySearchTree.h
binaryTree.h
linkedList.h
orderedLinkedList.h
Please lable which code goes in which file
Explanation / Answer
Answer:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void insert(int,int );
void remove(int);
void print(int);
int find(int);
int find1(int,int);
int input[40],t=1,s,x,i;
main()
{
int ch,y;
for(i=1;i<40;i++)
input[i]=-1;
while(1)
{
cout <<"1.INSERT 2.DELETE 3.print 4.find 5.EXIT Enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=find(1);
if(y!=-1) remove(y);
else cout<<"no such element in input";
break;
case 3:
print(1);
cout<<" ";
for(int i=0;i<=32;i++)
cout <<i;
cout <<" ";
break;
case 4:
cout <<"enter the element to find:";
cin >> x;
y=find(1);
if(y == -1) cout <<"no such element in input";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}
void insert(int s,int ch )
{
int x;
if(t==1)
{
input[t++]=ch;
return;
}
x=find1(s,ch);
if(input[x]>ch)
input[2*x]=ch;
else
input[2*x+1]=ch;
t++;
}
void remove(int x)
{
if( input[2*x]==-1 && input[2*x+1]==-1)
input[x]=-1;
else if(input[2*x]==-1)
{ input[x]=input[2*x+1];
input[2*x+1]=-1;
}
else if(input[2*x+1]==-1)
{ input[x]=input[2*x];
input[2*x]=-1;
}
else
{
input[x]=input[2*x];
remove(2*x);
}
t--;
}
int find(int s)
{
if(t==1)
{
cout <<"no element in input";
return -1;
}
if(input[s]==-1)
return input[s];
if(input[s]>x)
find(2*s);
else if(input[s]<x)
find(2*s+1);
else
return s;
}
void print(int s)
{
if(t==1)
{cout <<"no element in input:";
return;}
for(int i=1;i<40;i++)
if(input[i]==-1)
cout <<" ";
else cout <<input[i];
return ;
}
int find1(int s,int ch)
{
if(t==1)
{
cout <<"no element in input";
return -1;
}
if(input[s]==-1)
return s/2;
if(input[s] > ch)
find1(2*s,ch);
else find1(2*s+1,ch);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.