Question

In: Computer Science

C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...

C++(screenshot output please) Part 2 - Tree programs

Program 1 Implement a tree using an array

Program 2 Implement a tree using linked list - pointer Binary Tree

Program 3 - Convert program 1 to a template

Solutions

Expert Solution

Answer-

Program 1 Implement a tree using an array-

// C++ implementation of tree using array
// numbering starting from 0 to n-1.
#include<bits/stdc++.h>
using namespace std;
char tree[10];
int root(char key)
{
   if(tree[0] != '\0')
       cout << "Tree already had root";
   else
       tree[0] = key;
   return 0;
}

int set_left(char key, int parent)
{
   if(tree[parent] == '\0')
       cout << "\nCan't set child at"
           << (parent * 2) + 1
           << " , no parent found";
   else
       tree[(parent * 2) + 1] = key;
   return 0;
}

int set_right(char key, int parent)
{
   if(tree[parent] == '\0')
       cout << "\nCan't set child at"
           << (parent * 2) + 2
           << " , no parent found";
   else
       tree[(parent * 2) + 2] = key;
   return 0;
}

int print_tree()
{
   cout << "\n";
   for(int i = 0; i < 10; i++)
   {
       if(tree[i] != '\0')
           cout << tree[i];
       else
           cout << "-";
   }
   return 0;
}

int main()
{
   root('A');
   //insert_left('B',0);
   set_right('C', 0);
   set_left('D', 1);
   set_right('E', 1);
   set_right('F', 2);
   print_tree();
   return 0;
}

Program 2 Implement a tree using linked list - pointer Binary Tree-

#include <iostream>
using namespace std;

struct nod {
nod *l, *r;
int d;
}*r = NULL, *p = NULL, *np = NULL, *q;

void create() {
int v,c = 0;
while (c < 6) {
if (r == NULL) {
r = new nod;
cout<<"enter value of root node\n";
cin>>r->d;
r->r = NULL;
r->l = NULL;
} else {
p = r;
cout<<"enter value of node\n";
cin>>v;
while(true) {
if (v< p->d) {
if (p->l == NULL) {
p->l = new nod;
p = p->l;
p->d = v;
p->l = NULL;
p->r = NULL;
cout<<"value entered in left\n";
break;
} else if (p->l != NULL) {
p = p->l;
}
} else if (v >p->d) {
if (p->r == NULL) {
p->r = new nod;
p = p->r;
p->d = v;
p->l = NULL;
p->r = NULL;
cout<<"value entered in right\n";
break;
} else if (p->r != NULL) {
p = p->r;
}
}
}
}
c++;
}
}

void inorder(nod *p) {
if (p != NULL) {
inorder(p->l);
cout<<p->d<<endl;
inorder(p->r);
}
}

void preorder(nod *p) {
if (p != NULL) {
cout<<p->d<<endl;
preorder(p->l);
preorder(p->r);
}
}

void postorder(nod *p) {
if (p != NULL) {
postorder(p->l);
postorder(p->r);
cout<<p->d<<endl;
}
}

int main() {
create();
cout<<" traversal in inorder\n";
inorder(r);
cout<<" traversal in preorder\n";
preorder(r);
cout<<" traversal in postorder\n";
postorder(r);
}

Program 3 - Convert program 1 to a template-

// C++ implementation of tree using array
// numbering starting from 0 to n-1.
#include<bits/stdc++.h>
using namespace std;
template<typename To, typename From>
To convert_to(From from) { return To::unimplemented_conversion; }
char tree[10];

int root(char key)
{
   if(tree[0] != '\0')
       cout << "Tree already had root";
   else
       tree[0] = key;
   return 0;
}

int set_left(char key, int parent)
{
   if(tree[parent] == '\0')
       cout << "\nCan't set child at"
           << (parent * 2) + 1
           << " , no parent found";
   else
       tree[(parent * 2) + 1] = key;
   return 0;
}

int set_right(char key, int parent)
{
   if(tree[parent] == '\0')
       cout << "\nCan't set child at"
           << (parent * 2) + 2
           << " , no parent found";
   else
       tree[(parent * 2) + 2] = key;
   return 0;
}

int print_tree()
{
   cout << "\n";
   for(int i = 0; i < 10; i++)
   {
       if(tree[i] != '\0')
           cout << tree[i];
       else
           cout << "-";
   }
   return 0;
}

int main()
{
   root('A');
   //insert_left('B',0);
   set_right('C', 0);
   set_left('D', 1);
   set_right('E', 1);
   set_right('F', 2);
   print_tree();
   return 0;
}


Related Solutions

Part 1 (Objective C++ and please have output screenshot) The purpose of this part of the...
Part 1 (Objective C++ and please have output screenshot) The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following...
This is C++, please insert screenshot of output please. Part1: Palindrome detector Write a program that...
This is C++, please insert screenshot of output please. Part1: Palindrome detector Write a program that will test if some string is a palindrome Ask the user to enter a string Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome. Main displays the result ( if the string is a...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
**** Using C Sharp **** ***If possible please include screenshot of output *** **Also, if possible,...
**** Using C Sharp **** ***If possible please include screenshot of output *** **Also, if possible, please provide info from .txt files created** You are to write a program which is going to use inheritance. It should start off with the base classes Account: contains the string name, int accountnumber, double balance. Savings: Derived from Account class, it should contain double interest rate. Checkings: Derived from Account class, it should contain double overdraftlimit. CreditCard: Derived from Checkings class, it should...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that...
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that a text file named FIRST.TXT contains some text written into it, write a class and a method named vowelwords(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry umbrella and overcoat...
Please code the program showing the output below. using C language 1. Using 'if' or 'while'...
Please code the program showing the output below. using C language 1. Using 'if' or 'while' or 'for' and 'break' statement / only using <stdio.h> A bC dEf GhIj KlMnO 2. a program that identifies the largest number that can be expressed in short. Using only loop (ex.for,if,while) and break statement only using <stdio.h>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT