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

Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
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...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message: Subsequence <XXX> found at index <i>. Where i is the starting index of the subsequence in the array. Otherwise, print Subsequence <XXX> not found. The array of characters and the subsequence will be given through standard input. Read them and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT