SlideShare a Scribd company logo
1 of 13
Download to read offline
A)
B)
// C++ program to create a Complete Binary tree from its Linked List
// Representation
#include
#include
#include
using namespace std;
// Linked list node
struct ListNode
{
int data;
ListNode* next;
};
// Binary tree node structure
struct BinaryTreeNode
{
int data;
BinaryTreeNode *left, *right;
};
// Function to insert a node at the beginning of the Linked List
void push(struct ListNode** head_ref, int new_data)
{
// allocate node and assign data
struct ListNode* new_node = new ListNode;
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// method to create a new binary tree node from the given data
BinaryTreeNode* newBinaryTreeNode(int data)
{
BinaryTreeNode *temp = new BinaryTreeNode;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// converts a given linked list representing a complete binary tree into the
// linked representation of binary tree.
void convertList2Binary(ListNode *head, BinaryTreeNode* &root)
{
// queue to store the parent nodes
queue q;
// Base Case
if (head == NULL)
{
root = NULL; // Note that root is passed by reference
return;
}
// 1.) The first node is always the root node, and add it to the queue
root = newBinaryTreeNode(head->data);
q.push(root);
// advance the pointer to the next node
head = head->next;
// until the end of linked list is reached, do the following steps
while (head)
{
// 2.a) take the parent node from the q and remove it from q
BinaryTreeNode* parent = q.front();
q.pop();
// 2.c) take next two nodes from the linked list. We will add
// them as children of the current parent node in step 2.b. Push them
// into the queue so that they will be parents to the future nodes
BinaryTreeNode *leftChild = NULL, *rightChild = NULL;
leftChild = newBinaryTreeNode(head->data);
q.push(leftChild);
head = head->next;
if (head)
{
rightChild = newBinaryTreeNode(head->data);
q.push(rightChild);
head = head->next;
}
// 2.b) assign the left and right children of parent
parent->left = leftChild;
parent->right = rightChild;
}
}
// Utility function to traverse the binary tree after conversion
void inorderTraversal(BinaryTreeNode* root)
{
if (root)
{
inorderTraversal( root->left );
cout << root->data << " ";
inorderTraversal( root->right );
}
}
// Driver program to test above functions
int main()
{
// create a linked list shown in above diagram
struct ListNode* head = NULL;
push(&head, 36); /* Last node of Linked List */
push(&head, 30);
push(&head, 25);
push(&head, 15);
push(&head, 12);
push(&head, 10); /* First node of Linked List */
BinaryTreeNode *root;
convertList2Binary(head, root);
cout << "Inorder Traversal of the constructed Binary Tree is:  ";
inorderTraversal(root);
return 0;
}
c)
#include
#include
using namespace std;
template
class bintree
{
bintree *left;
T data;
bintree *right;
public :
bintree()
{
left=right=NULL;
}
void create();
void preorder();
void inorder();
void postorder();
};
template
void bintree :: create()
{
char opt;
cout << " Enter the element : ";
cin >> data;
cout << " Is there left child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
left = new bintree;
left->create();
}
cout << " Is there right child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
right = new bintree;
right->create();
}
}
template
void bintree :: preorder()
{
if (this!=NULL)
{
cout << data << " ";
left->preorder();
right->preorder();
}
}
template
void bintree :: inorder()
{
if (this!=NULL)
{
left->inorder();
cout << data << " ";
right->inorder();
}
}
template
void bintree :: postorder()
{
if (this!=NULL)
{
left->postorder();
right->postorder();
cout << data << " ";
}
}
int main()
{
bintree *x;
x=NULL;
int ch;
do
{
cout << "Binary Tree Operation Menu ";
cout << "1. Create ";
cout << "2. Preorder Traversal ";
cout << "3. Inorder Traversal ";
cout << "4. Postorder Traversal ";
cout << "5. Exit  ";
cout << "Enter Your Choice [1..5] : ";
cin >> ch;
switch(ch)
{
case 1 :
x = new bintree ;
x->create();
break;
case 2 :
x->preorder();
break;
case 3 :
x->inorder();
break;
case 4 :
x->postorder();
break;
case 5 :
break;
default :
cout << "Invalid Choice!";
}
}while (ch!=5);
system("pause");
}
Solution
A)
B)
// C++ program to create a Complete Binary tree from its Linked List
// Representation
#include
#include
#include
using namespace std;
// Linked list node
struct ListNode
{
int data;
ListNode* next;
};
// Binary tree node structure
struct BinaryTreeNode
{
int data;
BinaryTreeNode *left, *right;
};
// Function to insert a node at the beginning of the Linked List
void push(struct ListNode** head_ref, int new_data)
{
// allocate node and assign data
struct ListNode* new_node = new ListNode;
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// method to create a new binary tree node from the given data
BinaryTreeNode* newBinaryTreeNode(int data)
{
BinaryTreeNode *temp = new BinaryTreeNode;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// converts a given linked list representing a complete binary tree into the
// linked representation of binary tree.
void convertList2Binary(ListNode *head, BinaryTreeNode* &root)
{
// queue to store the parent nodes
queue q;
// Base Case
if (head == NULL)
{
root = NULL; // Note that root is passed by reference
return;
}
// 1.) The first node is always the root node, and add it to the queue
root = newBinaryTreeNode(head->data);
q.push(root);
// advance the pointer to the next node
head = head->next;
// until the end of linked list is reached, do the following steps
while (head)
{
// 2.a) take the parent node from the q and remove it from q
BinaryTreeNode* parent = q.front();
q.pop();
// 2.c) take next two nodes from the linked list. We will add
// them as children of the current parent node in step 2.b. Push them
// into the queue so that they will be parents to the future nodes
BinaryTreeNode *leftChild = NULL, *rightChild = NULL;
leftChild = newBinaryTreeNode(head->data);
q.push(leftChild);
head = head->next;
if (head)
{
rightChild = newBinaryTreeNode(head->data);
q.push(rightChild);
head = head->next;
}
// 2.b) assign the left and right children of parent
parent->left = leftChild;
parent->right = rightChild;
}
}
// Utility function to traverse the binary tree after conversion
void inorderTraversal(BinaryTreeNode* root)
{
if (root)
{
inorderTraversal( root->left );
cout << root->data << " ";
inorderTraversal( root->right );
}
}
// Driver program to test above functions
int main()
{
// create a linked list shown in above diagram
struct ListNode* head = NULL;
push(&head, 36); /* Last node of Linked List */
push(&head, 30);
push(&head, 25);
push(&head, 15);
push(&head, 12);
push(&head, 10); /* First node of Linked List */
BinaryTreeNode *root;
convertList2Binary(head, root);
cout << "Inorder Traversal of the constructed Binary Tree is:  ";
inorderTraversal(root);
return 0;
}
c)
#include
#include
using namespace std;
template
class bintree
{
bintree *left;
T data;
bintree *right;
public :
bintree()
{
left=right=NULL;
}
void create();
void preorder();
void inorder();
void postorder();
};
template
void bintree :: create()
{
char opt;
cout << " Enter the element : ";
cin >> data;
cout << " Is there left child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
left = new bintree;
left->create();
}
cout << " Is there right child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
right = new bintree;
right->create();
}
}
template
void bintree :: preorder()
{
if (this!=NULL)
{
cout << data << " ";
left->preorder();
right->preorder();
}
}
template
void bintree :: inorder()
{
if (this!=NULL)
{
left->inorder();
cout << data << " ";
right->inorder();
}
}
template
void bintree :: postorder()
{
if (this!=NULL)
{
left->postorder();
right->postorder();
cout << data << " ";
}
}
int main()
{
bintree *x;
x=NULL;
int ch;
do
{
cout << "Binary Tree Operation Menu ";
cout << "1. Create ";
cout << "2. Preorder Traversal ";
cout << "3. Inorder Traversal ";
cout << "4. Postorder Traversal ";
cout << "5. Exit  ";
cout << "Enter Your Choice [1..5] : ";
cin >> ch;
switch(ch)
{
case 1 :
x = new bintree ;
x->create();
break;
case 2 :
x->preorder();
break;
case 3 :
x->inorder();
break;
case 4 :
x->postorder();
break;
case 5 :
break;
default :
cout << "Invalid Choice!";
}
}while (ch!=5);
system("pause");
}

More Related Content

Similar to A)B) C++ program to create a Complete Binary tree from its Lin.pdf

THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdfsudhinjv
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfadisainternational
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxnoreendchesterton753
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfajayadinathcomputers
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfferoz544
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfshalins6
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docxajoy21
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfangelfragranc
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdferremmfab
 

Similar to A)B) C++ program to create a Complete Binary tree from its Lin.pdf (20)

THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docx
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdf
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 

More from anton291

There are many ways to do the Lewis symbol for yo.pdf
                     There are many ways to do the Lewis symbol for yo.pdf                     There are many ways to do the Lewis symbol for yo.pdf
There are many ways to do the Lewis symbol for yo.pdfanton291
 
the compound is heptane note the molecular ion.pdf
                     the compound is heptane  note the molecular ion.pdf                     the compound is heptane  note the molecular ion.pdf
the compound is heptane note the molecular ion.pdfanton291
 
Succinic acid is oxidized C goes from +34 to +1 .pdf
                     Succinic acid is oxidized C goes from +34 to +1 .pdf                     Succinic acid is oxidized C goes from +34 to +1 .pdf
Succinic acid is oxidized C goes from +34 to +1 .pdfanton291
 
pH = pK +log ([salt][acid]) = pH = -log(1.8 x .pdf
                     pH = pK +log ([salt][acid])  = pH = -log(1.8 x .pdf                     pH = pK +log ([salt][acid])  = pH = -log(1.8 x .pdf
pH = pK +log ([salt][acid]) = pH = -log(1.8 x .pdfanton291
 
Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf
                     Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf                     Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf
Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdfanton291
 
Moseley used X-ray diffraction to show that each .pdf
                     Moseley used X-ray diffraction to show that each .pdf                     Moseley used X-ray diffraction to show that each .pdf
Moseley used X-ray diffraction to show that each .pdfanton291
 
Welding is a permanent joining of two similar metals by melting the .pdf
Welding is a permanent joining of two similar metals by melting the .pdfWelding is a permanent joining of two similar metals by melting the .pdf
Welding is a permanent joining of two similar metals by melting the .pdfanton291
 
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdf
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdfUse of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdf
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdfanton291
 
the solute potential of the plant cell is Greater than the pure wate.pdf
the solute potential of the plant cell is Greater than the pure wate.pdfthe solute potential of the plant cell is Greater than the pure wate.pdf
the solute potential of the plant cell is Greater than the pure wate.pdfanton291
 
The Human Genome Project (HGP) determines the sequence of nucleotide.pdf
The Human Genome Project (HGP) determines the sequence of nucleotide.pdfThe Human Genome Project (HGP) determines the sequence of nucleotide.pdf
The Human Genome Project (HGP) determines the sequence of nucleotide.pdfanton291
 
solutionlist intersect(list L1, list L2){list result;positi.pdf
solutionlist intersect(list L1, list L2){list result;positi.pdfsolutionlist intersect(list L1, list L2){list result;positi.pdf
solutionlist intersect(list L1, list L2){list result;positi.pdfanton291
 
Sorry for not seeing const temp, The answers are-The reaction abso.pdf
Sorry for not seeing const temp, The answers are-The reaction abso.pdfSorry for not seeing const temp, The answers are-The reaction abso.pdf
Sorry for not seeing const temp, The answers are-The reaction abso.pdfanton291
 
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdf
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdfSO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdf
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdfanton291
 
selection_sort..c#include stdio.hheader file input output func.pdf
selection_sort..c#include stdio.hheader file input output func.pdfselection_sort..c#include stdio.hheader file input output func.pdf
selection_sort..c#include stdio.hheader file input output func.pdfanton291
 
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdf
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdfQ2.AnswerThe Toll -like receptor encodes a protein which plays .pdf
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdfanton291
 
Nucleus is the dense central part of an atom. .pdf
                     Nucleus  is the dense central part of an atom.   .pdf                     Nucleus  is the dense central part of an atom.   .pdf
Nucleus is the dense central part of an atom. .pdfanton291
 
Its demorgans theorem. Draw truth table to verify.pdf
                     Its demorgans theorem. Draw truth table to verify.pdf                     Its demorgans theorem. Draw truth table to verify.pdf
Its demorgans theorem. Draw truth table to verify.pdfanton291
 
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdf
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdfIonic Bonds (between ionic molecules) - Medium strong bond - In an.pdf
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdfanton291
 
I will help .SolutionI will help ..pdf
I will help .SolutionI will help ..pdfI will help .SolutionI will help ..pdf
I will help .SolutionI will help ..pdfanton291
 
HDL!SolutionHDL!.pdf
HDL!SolutionHDL!.pdfHDL!SolutionHDL!.pdf
HDL!SolutionHDL!.pdfanton291
 

More from anton291 (20)

There are many ways to do the Lewis symbol for yo.pdf
                     There are many ways to do the Lewis symbol for yo.pdf                     There are many ways to do the Lewis symbol for yo.pdf
There are many ways to do the Lewis symbol for yo.pdf
 
the compound is heptane note the molecular ion.pdf
                     the compound is heptane  note the molecular ion.pdf                     the compound is heptane  note the molecular ion.pdf
the compound is heptane note the molecular ion.pdf
 
Succinic acid is oxidized C goes from +34 to +1 .pdf
                     Succinic acid is oxidized C goes from +34 to +1 .pdf                     Succinic acid is oxidized C goes from +34 to +1 .pdf
Succinic acid is oxidized C goes from +34 to +1 .pdf
 
pH = pK +log ([salt][acid]) = pH = -log(1.8 x .pdf
                     pH = pK +log ([salt][acid])  = pH = -log(1.8 x .pdf                     pH = pK +log ([salt][acid])  = pH = -log(1.8 x .pdf
pH = pK +log ([salt][acid]) = pH = -log(1.8 x .pdf
 
Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf
                     Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf                     Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf
Step1 25 ml of1M HCl w ill neutralise 25 ml 1 M N.pdf
 
Moseley used X-ray diffraction to show that each .pdf
                     Moseley used X-ray diffraction to show that each .pdf                     Moseley used X-ray diffraction to show that each .pdf
Moseley used X-ray diffraction to show that each .pdf
 
Welding is a permanent joining of two similar metals by melting the .pdf
Welding is a permanent joining of two similar metals by melting the .pdfWelding is a permanent joining of two similar metals by melting the .pdf
Welding is a permanent joining of two similar metals by melting the .pdf
 
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdf
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdfUse of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdf
Use of oxygen for ATP synthesis is called as Aerobic respiration. Th.pdf
 
the solute potential of the plant cell is Greater than the pure wate.pdf
the solute potential of the plant cell is Greater than the pure wate.pdfthe solute potential of the plant cell is Greater than the pure wate.pdf
the solute potential of the plant cell is Greater than the pure wate.pdf
 
The Human Genome Project (HGP) determines the sequence of nucleotide.pdf
The Human Genome Project (HGP) determines the sequence of nucleotide.pdfThe Human Genome Project (HGP) determines the sequence of nucleotide.pdf
The Human Genome Project (HGP) determines the sequence of nucleotide.pdf
 
solutionlist intersect(list L1, list L2){list result;positi.pdf
solutionlist intersect(list L1, list L2){list result;positi.pdfsolutionlist intersect(list L1, list L2){list result;positi.pdf
solutionlist intersect(list L1, list L2){list result;positi.pdf
 
Sorry for not seeing const temp, The answers are-The reaction abso.pdf
Sorry for not seeing const temp, The answers are-The reaction abso.pdfSorry for not seeing const temp, The answers are-The reaction abso.pdf
Sorry for not seeing const temp, The answers are-The reaction abso.pdf
 
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdf
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdfSO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdf
SO2 = 18IF4+ = 34I3- = 22The easiest way to think about this i.pdf
 
selection_sort..c#include stdio.hheader file input output func.pdf
selection_sort..c#include stdio.hheader file input output func.pdfselection_sort..c#include stdio.hheader file input output func.pdf
selection_sort..c#include stdio.hheader file input output func.pdf
 
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdf
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdfQ2.AnswerThe Toll -like receptor encodes a protein which plays .pdf
Q2.AnswerThe Toll -like receptor encodes a protein which plays .pdf
 
Nucleus is the dense central part of an atom. .pdf
                     Nucleus  is the dense central part of an atom.   .pdf                     Nucleus  is the dense central part of an atom.   .pdf
Nucleus is the dense central part of an atom. .pdf
 
Its demorgans theorem. Draw truth table to verify.pdf
                     Its demorgans theorem. Draw truth table to verify.pdf                     Its demorgans theorem. Draw truth table to verify.pdf
Its demorgans theorem. Draw truth table to verify.pdf
 
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdf
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdfIonic Bonds (between ionic molecules) - Medium strong bond - In an.pdf
Ionic Bonds (between ionic molecules) - Medium strong bond - In an.pdf
 
I will help .SolutionI will help ..pdf
I will help .SolutionI will help ..pdfI will help .SolutionI will help ..pdf
I will help .SolutionI will help ..pdf
 
HDL!SolutionHDL!.pdf
HDL!SolutionHDL!.pdfHDL!SolutionHDL!.pdf
HDL!SolutionHDL!.pdf
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

A)B) C++ program to create a Complete Binary tree from its Lin.pdf

  • 1. A) B) // C++ program to create a Complete Binary tree from its Linked List // Representation #include #include #include using namespace std; // Linked list node struct ListNode { int data; ListNode* next; }; // Binary tree node structure struct BinaryTreeNode { int data; BinaryTreeNode *left, *right; }; // Function to insert a node at the beginning of the Linked List void push(struct ListNode** head_ref, int new_data) { // allocate node and assign data struct ListNode* new_node = new ListNode; new_node->data = new_data; // link the old list off the new node new_node->next = (*head_ref); // move the head to point to the new node (*head_ref) = new_node; } // method to create a new binary tree node from the given data BinaryTreeNode* newBinaryTreeNode(int data) { BinaryTreeNode *temp = new BinaryTreeNode;
  • 2. temp->data = data; temp->left = temp->right = NULL; return temp; } // converts a given linked list representing a complete binary tree into the // linked representation of binary tree. void convertList2Binary(ListNode *head, BinaryTreeNode* &root) { // queue to store the parent nodes queue q; // Base Case if (head == NULL) { root = NULL; // Note that root is passed by reference return; } // 1.) The first node is always the root node, and add it to the queue root = newBinaryTreeNode(head->data); q.push(root); // advance the pointer to the next node head = head->next; // until the end of linked list is reached, do the following steps while (head) { // 2.a) take the parent node from the q and remove it from q BinaryTreeNode* parent = q.front(); q.pop(); // 2.c) take next two nodes from the linked list. We will add // them as children of the current parent node in step 2.b. Push them // into the queue so that they will be parents to the future nodes BinaryTreeNode *leftChild = NULL, *rightChild = NULL; leftChild = newBinaryTreeNode(head->data); q.push(leftChild); head = head->next; if (head) {
  • 3. rightChild = newBinaryTreeNode(head->data); q.push(rightChild); head = head->next; } // 2.b) assign the left and right children of parent parent->left = leftChild; parent->right = rightChild; } } // Utility function to traverse the binary tree after conversion void inorderTraversal(BinaryTreeNode* root) { if (root) { inorderTraversal( root->left ); cout << root->data << " "; inorderTraversal( root->right ); } } // Driver program to test above functions int main() { // create a linked list shown in above diagram struct ListNode* head = NULL; push(&head, 36); /* Last node of Linked List */ push(&head, 30); push(&head, 25); push(&head, 15); push(&head, 12); push(&head, 10); /* First node of Linked List */ BinaryTreeNode *root; convertList2Binary(head, root); cout << "Inorder Traversal of the constructed Binary Tree is: "; inorderTraversal(root); return 0; }
  • 4. c) #include #include using namespace std; template class bintree { bintree *left; T data; bintree *right; public : bintree() { left=right=NULL; } void create(); void preorder(); void inorder(); void postorder(); }; template void bintree :: create() { char opt; cout << " Enter the element : "; cin >> data; cout << " Is there left child for " << data << " [y/n] ? : "; cin >> opt; if (opt=='y') { left = new bintree; left->create(); }
  • 5. cout << " Is there right child for " << data << " [y/n] ? : "; cin >> opt; if (opt=='y') { right = new bintree; right->create(); } } template void bintree :: preorder() { if (this!=NULL) { cout << data << " "; left->preorder(); right->preorder(); } } template void bintree :: inorder() { if (this!=NULL) { left->inorder(); cout << data << " "; right->inorder(); } } template void bintree :: postorder()
  • 6. { if (this!=NULL) { left->postorder(); right->postorder(); cout << data << " "; } } int main() { bintree *x; x=NULL; int ch; do { cout << "Binary Tree Operation Menu "; cout << "1. Create "; cout << "2. Preorder Traversal "; cout << "3. Inorder Traversal "; cout << "4. Postorder Traversal "; cout << "5. Exit "; cout << "Enter Your Choice [1..5] : "; cin >> ch; switch(ch) { case 1 : x = new bintree ; x->create(); break; case 2 : x->preorder(); break; case 3 : x->inorder();
  • 7. break; case 4 : x->postorder(); break; case 5 : break; default : cout << "Invalid Choice!"; } }while (ch!=5); system("pause"); } Solution A) B) // C++ program to create a Complete Binary tree from its Linked List // Representation #include #include #include using namespace std; // Linked list node struct ListNode { int data; ListNode* next; }; // Binary tree node structure struct BinaryTreeNode { int data; BinaryTreeNode *left, *right; }; // Function to insert a node at the beginning of the Linked List
  • 8. void push(struct ListNode** head_ref, int new_data) { // allocate node and assign data struct ListNode* new_node = new ListNode; new_node->data = new_data; // link the old list off the new node new_node->next = (*head_ref); // move the head to point to the new node (*head_ref) = new_node; } // method to create a new binary tree node from the given data BinaryTreeNode* newBinaryTreeNode(int data) { BinaryTreeNode *temp = new BinaryTreeNode; temp->data = data; temp->left = temp->right = NULL; return temp; } // converts a given linked list representing a complete binary tree into the // linked representation of binary tree. void convertList2Binary(ListNode *head, BinaryTreeNode* &root) { // queue to store the parent nodes queue q; // Base Case if (head == NULL) { root = NULL; // Note that root is passed by reference return; } // 1.) The first node is always the root node, and add it to the queue root = newBinaryTreeNode(head->data); q.push(root); // advance the pointer to the next node head = head->next; // until the end of linked list is reached, do the following steps
  • 9. while (head) { // 2.a) take the parent node from the q and remove it from q BinaryTreeNode* parent = q.front(); q.pop(); // 2.c) take next two nodes from the linked list. We will add // them as children of the current parent node in step 2.b. Push them // into the queue so that they will be parents to the future nodes BinaryTreeNode *leftChild = NULL, *rightChild = NULL; leftChild = newBinaryTreeNode(head->data); q.push(leftChild); head = head->next; if (head) { rightChild = newBinaryTreeNode(head->data); q.push(rightChild); head = head->next; } // 2.b) assign the left and right children of parent parent->left = leftChild; parent->right = rightChild; } } // Utility function to traverse the binary tree after conversion void inorderTraversal(BinaryTreeNode* root) { if (root) { inorderTraversal( root->left ); cout << root->data << " "; inorderTraversal( root->right ); } } // Driver program to test above functions int main() {
  • 10. // create a linked list shown in above diagram struct ListNode* head = NULL; push(&head, 36); /* Last node of Linked List */ push(&head, 30); push(&head, 25); push(&head, 15); push(&head, 12); push(&head, 10); /* First node of Linked List */ BinaryTreeNode *root; convertList2Binary(head, root); cout << "Inorder Traversal of the constructed Binary Tree is: "; inorderTraversal(root); return 0; } c) #include #include using namespace std; template class bintree { bintree *left; T data; bintree *right; public : bintree() { left=right=NULL; } void create(); void preorder(); void inorder(); void postorder(); };
  • 11. template void bintree :: create() { char opt; cout << " Enter the element : "; cin >> data; cout << " Is there left child for " << data << " [y/n] ? : "; cin >> opt; if (opt=='y') { left = new bintree; left->create(); } cout << " Is there right child for " << data << " [y/n] ? : "; cin >> opt; if (opt=='y') { right = new bintree; right->create(); } } template void bintree :: preorder() { if (this!=NULL) { cout << data << " "; left->preorder(); right->preorder(); } }
  • 12. template void bintree :: inorder() { if (this!=NULL) { left->inorder(); cout << data << " "; right->inorder(); } } template void bintree :: postorder() { if (this!=NULL) { left->postorder(); right->postorder(); cout << data << " "; } } int main() { bintree *x; x=NULL; int ch; do { cout << "Binary Tree Operation Menu "; cout << "1. Create "; cout << "2. Preorder Traversal "; cout << "3. Inorder Traversal "; cout << "4. Postorder Traversal ";
  • 13. cout << "5. Exit "; cout << "Enter Your Choice [1..5] : "; cin >> ch; switch(ch) { case 1 : x = new bintree ; x->create(); break; case 2 : x->preorder(); break; case 3 : x->inorder(); break; case 4 : x->postorder(); break; case 5 : break; default : cout << "Invalid Choice!"; } }while (ch!=5); system("pause"); }