SlideShare a Scribd company logo
1 of 16
Download to read offline
Data Structures
Please I need help completing this c++ program. it was giving to complete it with no other
discriptions.
#include
using namespace std;
template
struct node
{
datatype info;
node *left, *right;
};
template
class tree
{
public:
tree();
~tree();
bool isempty();
void inserttree(datatype data);
void print();
private:
node *root;
};
template
tree::tree()
{
root = NULL;
}
template
void tree ::inserttree(datatype data)
{
node *temp;
temp = new node ;
temp->left = NULL;
temp->right = NULL;
temp->info = data;
if (root == NULL)
root = temp;
else
before = NULL;
after = root;
while (after != NULL)
{
b = a;
root
...;
else
}
if(temp->info< before->info)
before->left = temp;
else
before->right = temp;
//before = after;
//if (temp->info < after->info)
//after = after->left;
//else
//after = after->right;
//if(temp->info< before->info)
//before->left = temp;
//else
//before->right = temp;
}
template
void tree ::print(tree *temp)
{
if (temp != NULL)
{
print(temp->left);
cout << temp->info << endl;
print(temp->right);
}
p();
print(root);
}
int main()
{
tree t;
//put this in a loop
t.inserttree(90);
t.inserttree(80);
t.inserttree(108);
t.p();
}
Solution
// HEADER FILE FOR BINARY TREE//
// Define a structure to be used as the tree node
struct TreeNode
{
int Key;
float fValue;
int iValue;
char cArray[7];
TreeNode *left;
TreeNode *right;
};
class Code202_Tree
{
private:
TreeNode *root;
public:
Code202_Tree();
~Code202_Tree();
bool isEmpty();
TreeNode *SearchTree(int Key);
bool Insert(TreeNode *newNode);
bool Insert(int Key, float f, int i, char *cA);
bool Delete(int Key);
void PrintOne(TreeNode *T);
void PrintTree();
private:
void ClearTree(TreeNode *T);
TreeNode *DupNode(TreeNode * T);
void PrintAll(TreeNode *T);
};
#endif
//IMPLEMENTATION .CPP FILE FOR BINARY TREE//
#include
#include
#include "Code202_Tree.h"
using namespace std;
//--------------------------------------------
// Function: Code202_Tree()
// Purpose: Class constructor.
//--------------------------------------------
Code202_Tree::Code202_Tree()
{
root = NULL;
return;
}
//--------------------------------------------
// Function: Code202_Tree()
// Purpose: Class destructor.
//--------------------------------------------
Code202_Tree::~Code202_Tree()
{
ClearTree(root);
return;
}
//--------------------------------------------
// Function: ClearTree()
// Purpose: Perform a recursive traversal of
// a tree destroying all nodes.
//--------------------------------------------
void Code202_Tree::ClearTree(TreeNode *T)
{
if(T==NULL) return; // Nothing to clear
if(T->left != NULL) ClearTree(T->left); // Clear left sub-tree
if(T->right != NULL) ClearTree(T->right); // Clear right sub-tree
delete T; // Destroy this node
return;
}
//--------------------------------------------
// Function: isEmpty()
// Purpose: Return TRUE if tree is empty.
//--------------------------------------------
bool Code202_Tree::isEmpty()
{
return(root==NULL);
}
//--------------------------------------------
// Function: DupNode()
// Purpose: Duplicate a node in the tree. This
// is used to allow returning a complete
// structure from the tree without giving
// access into the tree through the pointers.
// Preconditions: None
// Returns: Pointer to a duplicate of the node arg
//--------------------------------------------
TreeNode *Code202_Tree::DupNode(TreeNode * T)
{
TreeNode *dupNode;
dupNode = new TreeNode();
*dupNode = *T; // Copy the data structure
dupNode->left = NULL; // Set the pointers to NULL
dupNode->right = NULL;
return dupNode;
}
//--------------------------------------------
// Function: SearchTree()
// Purpose: Perform an iterative search of the tree and
// return a pointer to a treenode containing the
// search key or NULL if not found.
// Preconditions: None
// Returns: Pointer to a duplicate of the node found
//--------------------------------------------
TreeNode *Code202_Tree::SearchTree(int Key)
{
int ValueInTree = false;
TreeNode *temp;
temp = root;
while((temp != NULL) && (temp->Key != Key))
{
if(Key < temp->Key)
temp = temp->left; // Search key comes before this node.
else
temp = temp->right; // Search key comes after this node
}
if(temp == NULL) return temp; // Search key not found
else
return(DupNode(temp)); // Found it so return a duplicate
}
//--------------------------------------------
// Function: Insert()
// Insert a new node into the tree.
// Preconditions: None
// Returns: int (TRUE if successful, FALSE otherwise)
//--------------------------------------------
bool Code202_Tree::Insert(TreeNode *newNode)
{
TreeNode *temp;
TreeNode *back;
temp = root;
back = NULL;
while(temp != NULL) // Loop till temp falls out of the tree
{
back = temp;
if(newNode->Key < temp->Key)
temp = temp->left;
else
temp = temp->right;
}
// Now attach the new node to the node that back points to
if(back == NULL) // Attach as root node in a new tree
root = newNode;
else
{
if(newNode->Key < back->Key)
back->left = newNode;
else
back->right = newNode;
}
return true ;
}
//--------------------------------------------
// Function: Insert()
// Insert a new node into the tree.
// Preconditions: None
// Returns: int (TRUE if successful, FALSE otherwise)
//--------------------------------------------
bool Code202_Tree::Insert(int Key, float f, int i, char *cA)
{
TreeNode *newNode;
// Create the new node and copy data into it
newNode = new TreeNode();
newNode->Key = Key;
newNode->fValue = f;
newNode->iValue = i;
strcpy(newNode->cArray, cA);
newNode->left = newNode->right = NULL;
// Call other Insert() to do the actual insertion
return(Insert(newNode));
}
//--------------------------------------------
// Function: Delete()
// Purpose: Delete a node from the tree.
// Preconditions: Tree contains the node to delete
// Returns: int (TRUE if successful, FALSE otherwise)
//--------------------------------------------
bool Code202_Tree::Delete(int Key)
{
TreeNode *back;
TreeNode *temp;
TreeNode *delParent; // Parent of node to delete
TreeNode *delNode; // Node to delete
temp = root;
back = NULL;
// Find the node to delete
while((temp != NULL) && (Key != temp->Key))
{
back = temp;
if(Key < temp->Key)
temp = temp->left;
else
temp = temp->right;
}
if(temp == NULL) // Didn't find the one to delete
{
return false;
}
else
{
if(temp == root) // Deleting the root
{
delNode = root;
delParent = NULL;
}
else
{
delNode = temp;
delParent = back;
}
}
// Case 1: Deleting node with no children or one child
if(delNode->right == NULL)
{
if(delParent == NULL) // If deleting the root
{
root = delNode->left;
delete delNode;
return true;
}
else
{
if(delParent->left == delNode)
delParent->left = delNode->left;
else
delParent->right = delNode->left;
delete delNode;
return true;
}
}
else // There is at least one child
{
if(delNode->left == NULL) // Only 1 child and it is on the right
{
if(delParent == NULL) // If deleting the root
{
root = delNode->right;
delete delNode;
return true;
}
else
{
if(delParent->left == delNode)
delParent->left = delNode->right;
else
delParent->right = delNode->right;
delete delNode;
return true;
}
}
else // Case 2: Deleting node with two children
{
// Find the replacement value. Locate the node
// containing the largest value smaller than the
// key of the node being deleted.
temp = delNode->left;
back = delNode;
while(temp->right != NULL)
{
back = temp;
temp = temp->right;
}
// Copy the replacement values into the node to be deleted
delNode->Key = temp->Key;
delNode->fValue = temp->fValue;
delNode->iValue = temp->iValue;
strcpy(delNode->cArray, temp->cArray);
// Remove the replacement node from the tree
if(back == delNode)
back->left = temp->left;
else
back->right = temp->left;
delete temp;
return true;
}
}
}
//--------------------------------------------
// Function: PrintOne()
// Purpose: Print data in one node of a tree.
// Preconditions: None
// Returns: void
//--------------------------------------------
void Code202_Tree::PrintOne(TreeNode *T)
{
cout << T->Key << "tt" << T->fValue << "tt" << T->iValue << "tt"
<< T->cArray << " ";
}
//--------------------------------------------
// Function: PrintAll()
// Purpose: Print the tree using a recursive
// traversal
// Preconditions: None
// Returns: void
//--------------------------------------------
void Code202_Tree::PrintAll(TreeNode *T)
{
if(T != NULL)
{
PrintAll(T->left);
PrintOne(T);
PrintAll(T->right);
}
}
//--------------------------------------------
// Function: PrintTree()
// Purpose: Print the tree using a recursive
// traversal. This gives the user access
// to PrintAll() without giving access to
// the root of the tree.
// Preconditions: None
// Returns: void
//--------------------------------------------
void Code202_Tree::PrintTree()
{
PrintAll(root);
}
Main file used to test the tree
#include
#include
#include "Code202_Tree.h"
using namespace std;
int main(void)
{
Code202_Tree *theTree;
TreeNode *newNode;
// Do initialization stuff
theTree = new Code202_Tree();
cout <<"Building tree... ";
// Do simple insert of 15 nodes into tree.
// Insert with keys in the order.
// 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15
// First 5 nodes are inserted using Insert1(). Remainder using Insert2()
// Node 1
newNode = new TreeNode();
newNode->Key = 8;
newNode->iValue = 2;
newNode->fValue = 2.3f;
strcpy(newNode->cArray, "Node1");
newNode->left = newNode->right = NULL;
theTree->Insert(newNode);
// Node 2
// Note: Each time a new node is allocated we reuse the same pointer
// Access to the previous node is not lost because it is not in the tree.
newNode = new TreeNode();
newNode->Key = 4;
newNode->iValue = 4;
newNode->fValue = 3.4f;
strcpy(newNode->cArray, "Node2");
newNode->left = newNode->right = NULL;
theTree->Insert(newNode);
// Node 3
newNode = new TreeNode();
newNode->Key = 12;
newNode->iValue = 8;
newNode->fValue = 4.5f;
strcpy(newNode->cArray, "Node3");
newNode->left = newNode->right = NULL;
theTree->Insert(newNode);
// Node 4
newNode = new TreeNode();
newNode->Key = 2;
newNode->iValue = 16;
newNode->fValue = 5.6f;
strcpy(newNode->cArray, "Node4");
newNode->left = newNode->right = NULL;
theTree->Insert(newNode);
// Node 5
newNode = new TreeNode();
newNode->Key = 6;
newNode->iValue = 32;
newNode->fValue = 6.7f;
strcpy(newNode->cArray, "Node5");
newNode->left = newNode->right = NULL;
theTree->Insert(newNode);
// Node 6
// Remainder of the nodes are inserted using Insert2()
theTree->Insert(10, 7.8f, 64, "Node6");
// Node 7
theTree->Insert(14, 8.9f, 128, "Node7");
// Node 8
theTree->Insert(1, 9.0f, 256, "Node8");
// Node 9
theTree->Insert(3, 0.9f, 512, "Node9");
// Node 10
theTree->Insert(5, 9.8f, 1024, "Node10");
// Node 11
theTree->Insert(7, 8.7f, 2048, "Node11");
// Node 12
theTree->Insert(9, 7.6f, 4096, "Node12");
// Node 13
theTree->Insert(11, 6.5f, 8192, "Node13");
// Node 14
theTree->Insert(13, 5.4f, 16384, "Node14");
// Node 15
theTree->Insert(15, 4.3f, 32768, "Node15");
cout <<"All nodes inserted ";
// Print the tree
cout <<"----------------------------------------------------- ";
theTree->PrintTree();
cout <<"Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
// Find some nodes and print them
cout <<"----------------------------------------------------- ";
cout <<"Testing the search function ";
newNode = theTree->SearchTree(13);
if(newNode != NULL)
{
theTree->PrintOne(newNode);
delete newNode; // Remember this is a duplicate node of the one in
// in the tree and main() is responsible for deleting it.
}
else
cout <<"Search key not found. ";
newNode = theTree->SearchTree(6);
if(newNode != NULL)
{
theTree->PrintOne(newNode);
delete newNode;
}
else
cout <<"Search key not found. ";
newNode = theTree->SearchTree(1);
if(newNode != NULL)
{
theTree->PrintOne(newNode);
delete newNode;
}
else
cout <<"Search key not found. ";
newNode = theTree->SearchTree(25); // Note: there is no Key=25 in the tree
if(newNode != NULL)
{
theTree->PrintOne(newNode);
delete newNode;
}
else
cout <<"Search key not found. ";
// Delete some nodes
cout <<"----------------------------------------------------- ";
cout <<"Testing delete function ";
cout <<"----------------------------------------------------- ";
cout <<"Testing deleting a leaf... ";
theTree->Delete(7); // Delete a known leaf
theTree->PrintTree();
cout <<"Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
cout <<"----------------------------------------------------- ";
cout <<"Testing deleting a node with 2 children... ";
theTree->Delete(12); // Delete a node known to have 2 children
theTree->PrintTree();
cout <<"Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
cout <<"----------------------------------------------------- ";
cout <<"Testing deleting a node with 1 child... ";
theTree->Delete(6); // Delete a node known to have only 1 child
theTree->PrintTree();
cout <<"Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
cout <<"----------------------------------------------------- ";
cout <<"Testing trying to delete a node that is not in the tree... ";
theTree->Delete(7); // Delete a node that is not there
theTree->PrintTree();
cout <<"Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
cout <<"----------------------------------------------------- ";
cout <<"Testing deleting the root... ";
theTree->Delete(8); // Delete the root
theTree->PrintTree();
cout <<"Done. Press Enter to continue...";
cin.get();
cout <<"----------------------------------------------------- ";
return 0;
}

More Related Content

Similar to Data StructuresPlease I need help completing this c++ program..pdf

Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docx
budbarber38650
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
funkybabyindia
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
contact32
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
rock73
 
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
erremmfab
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
anton291
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
noreendchesterton753
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
rohit219406
 
Using C++I keep getting messagehead does not name a type.pdf
Using C++I keep getting messagehead does not name a type.pdfUsing C++I keep getting messagehead does not name a type.pdf
Using C++I keep getting messagehead does not name a type.pdf
alokkesh1
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
arjuncp10
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
adityastores21
 

Similar to Data StructuresPlease I need help completing this c++ program..pdf (20)

Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docx
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
 
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
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
 
Using C++I keep getting messagehead does not name a type.pdf
Using C++I keep getting messagehead does not name a type.pdfUsing C++I keep getting messagehead does not name a type.pdf
Using C++I keep getting messagehead does not name a type.pdf
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
 

More from arkleatheray

Determine whether the following statements about the accumulated eang.pdf
Determine whether the following statements about the accumulated eang.pdfDetermine whether the following statements about the accumulated eang.pdf
Determine whether the following statements about the accumulated eang.pdf
arkleatheray
 
Create Student Project Database with insert, select, update, and del.pdf
Create Student Project Database with insert, select, update, and del.pdfCreate Student Project Database with insert, select, update, and del.pdf
Create Student Project Database with insert, select, update, and del.pdf
arkleatheray
 
Before 1980, most cell control and management software was written b.pdf
Before 1980, most cell control and management software was written b.pdfBefore 1980, most cell control and management software was written b.pdf
Before 1980, most cell control and management software was written b.pdf
arkleatheray
 
^^^Q3. I am trying to implement double linked list but I was faile.pdf
^^^Q3. I am trying to implement double linked list but I was faile.pdf^^^Q3. I am trying to implement double linked list but I was faile.pdf
^^^Q3. I am trying to implement double linked list but I was faile.pdf
arkleatheray
 
What is the evolutionary basis for sexual reproductionSolution.pdf
What is the evolutionary basis for sexual reproductionSolution.pdfWhat is the evolutionary basis for sexual reproductionSolution.pdf
What is the evolutionary basis for sexual reproductionSolution.pdf
arkleatheray
 
Why is it that the Grams stain is not appropriate for acid fast ba.pdf
Why is it that the Grams stain is not appropriate for acid fast ba.pdfWhy is it that the Grams stain is not appropriate for acid fast ba.pdf
Why is it that the Grams stain is not appropriate for acid fast ba.pdf
arkleatheray
 
Why does this version of the swap function fail to work Is there a f.pdf
Why does this version of the swap function fail to work Is there a f.pdfWhy does this version of the swap function fail to work Is there a f.pdf
Why does this version of the swap function fail to work Is there a f.pdf
arkleatheray
 
The groin is also known as the _____ region. crural inguinal perin.pdf
The groin is also known as the _____ region.  crural  inguinal  perin.pdfThe groin is also known as the _____ region.  crural  inguinal  perin.pdf
The groin is also known as the _____ region. crural inguinal perin.pdf
arkleatheray
 
Security Challenges Please respond to the followingDistributed .pdf
Security Challenges Please respond to the followingDistributed .pdfSecurity Challenges Please respond to the followingDistributed .pdf
Security Challenges Please respond to the followingDistributed .pdf
arkleatheray
 

More from arkleatheray (20)

briefly write about the Aridity of Subtropics Causes, locations, etc.pdf
briefly write about the Aridity of Subtropics Causes, locations, etc.pdfbriefly write about the Aridity of Subtropics Causes, locations, etc.pdf
briefly write about the Aridity of Subtropics Causes, locations, etc.pdf
 
Compare and contrast the Mann-Whitney (aka Wilcoxon Rank Sum) Test w.pdf
Compare and contrast the Mann-Whitney (aka Wilcoxon Rank Sum) Test w.pdfCompare and contrast the Mann-Whitney (aka Wilcoxon Rank Sum) Test w.pdf
Compare and contrast the Mann-Whitney (aka Wilcoxon Rank Sum) Test w.pdf
 
Determine whether the following statements about the accumulated eang.pdf
Determine whether the following statements about the accumulated eang.pdfDetermine whether the following statements about the accumulated eang.pdf
Determine whether the following statements about the accumulated eang.pdf
 
Discuss the changing demographics of Entrepreneurs and give an examp.pdf
Discuss the changing demographics of Entrepreneurs and give an examp.pdfDiscuss the changing demographics of Entrepreneurs and give an examp.pdf
Discuss the changing demographics of Entrepreneurs and give an examp.pdf
 
Create Student Project Database with insert, select, update, and del.pdf
Create Student Project Database with insert, select, update, and del.pdfCreate Student Project Database with insert, select, update, and del.pdf
Create Student Project Database with insert, select, update, and del.pdf
 
Before 1980, most cell control and management software was written b.pdf
Before 1980, most cell control and management software was written b.pdfBefore 1980, most cell control and management software was written b.pdf
Before 1980, most cell control and management software was written b.pdf
 
at age 51 can you take early distributions from traditional IRA for .pdf
at age 51 can you take early distributions from traditional IRA for .pdfat age 51 can you take early distributions from traditional IRA for .pdf
at age 51 can you take early distributions from traditional IRA for .pdf
 
What is a view What is it used for Give examples when a view can b.pdf
What is a view What is it used for Give examples when a view can b.pdfWhat is a view What is it used for Give examples when a view can b.pdf
What is a view What is it used for Give examples when a view can b.pdf
 
^^^Q3. I am trying to implement double linked list but I was faile.pdf
^^^Q3. I am trying to implement double linked list but I was faile.pdf^^^Q3. I am trying to implement double linked list but I was faile.pdf
^^^Q3. I am trying to implement double linked list but I was faile.pdf
 
What is the evolutionary basis for sexual reproductionSolution.pdf
What is the evolutionary basis for sexual reproductionSolution.pdfWhat is the evolutionary basis for sexual reproductionSolution.pdf
What is the evolutionary basis for sexual reproductionSolution.pdf
 
Why is it that the Grams stain is not appropriate for acid fast ba.pdf
Why is it that the Grams stain is not appropriate for acid fast ba.pdfWhy is it that the Grams stain is not appropriate for acid fast ba.pdf
Why is it that the Grams stain is not appropriate for acid fast ba.pdf
 
Why does this version of the swap function fail to work Is there a f.pdf
Why does this version of the swap function fail to work Is there a f.pdfWhy does this version of the swap function fail to work Is there a f.pdf
Why does this version of the swap function fail to work Is there a f.pdf
 
Which of the following is NOT a reason why heat is a bad method to ov.pdf
Which of the following is NOT a reason why heat is a bad method to ov.pdfWhich of the following is NOT a reason why heat is a bad method to ov.pdf
Which of the following is NOT a reason why heat is a bad method to ov.pdf
 
What are the trends in browsersSolution1.chrome Google chrom.pdf
What are the trends in browsersSolution1.chrome Google chrom.pdfWhat are the trends in browsersSolution1.chrome Google chrom.pdf
What are the trends in browsersSolution1.chrome Google chrom.pdf
 
The probability of a persons being left-handed is .12. In the 1992.pdf
The probability of a persons being left-handed is .12. In the 1992.pdfThe probability of a persons being left-handed is .12. In the 1992.pdf
The probability of a persons being left-handed is .12. In the 1992.pdf
 
The mass of a radioactive substance follows a continuous exponential.pdf
The mass of a radioactive substance follows a continuous exponential.pdfThe mass of a radioactive substance follows a continuous exponential.pdf
The mass of a radioactive substance follows a continuous exponential.pdf
 
The groin is also known as the _____ region. crural inguinal perin.pdf
The groin is also known as the _____ region.  crural  inguinal  perin.pdfThe groin is also known as the _____ region.  crural  inguinal  perin.pdf
The groin is also known as the _____ region. crural inguinal perin.pdf
 
short answer List and describe two advances during the Industrial Re.pdf
short answer List and describe two advances during the Industrial Re.pdfshort answer List and describe two advances during the Industrial Re.pdf
short answer List and describe two advances during the Industrial Re.pdf
 
SheilaSalmonella agar is _ because _ breaks down amino acids that co.pdf
SheilaSalmonella agar is _ because _ breaks down amino acids that co.pdfSheilaSalmonella agar is _ because _ breaks down amino acids that co.pdf
SheilaSalmonella agar is _ because _ breaks down amino acids that co.pdf
 
Security Challenges Please respond to the followingDistributed .pdf
Security Challenges Please respond to the followingDistributed .pdfSecurity Challenges Please respond to the followingDistributed .pdf
Security Challenges Please respond to the followingDistributed .pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Data StructuresPlease I need help completing this c++ program..pdf

  • 1. Data Structures Please I need help completing this c++ program. it was giving to complete it with no other discriptions. #include using namespace std; template struct node { datatype info; node *left, *right; }; template class tree { public: tree(); ~tree(); bool isempty(); void inserttree(datatype data); void print(); private: node *root; }; template tree::tree() { root = NULL; } template void tree ::inserttree(datatype data) {
  • 2. node *temp; temp = new node ; temp->left = NULL; temp->right = NULL; temp->info = data; if (root == NULL) root = temp; else before = NULL; after = root; while (after != NULL) { b = a; root ...; else } if(temp->info< before->info) before->left = temp; else before->right = temp; //before = after; //if (temp->info < after->info) //after = after->left; //else //after = after->right; //if(temp->info< before->info) //before->left = temp; //else //before->right = temp; } template void tree ::print(tree *temp) { if (temp != NULL) {
  • 3. print(temp->left); cout << temp->info << endl; print(temp->right); } p(); print(root); } int main() { tree t; //put this in a loop t.inserttree(90); t.inserttree(80); t.inserttree(108); t.p(); } Solution // HEADER FILE FOR BINARY TREE// // Define a structure to be used as the tree node struct TreeNode { int Key; float fValue; int iValue; char cArray[7]; TreeNode *left; TreeNode *right; }; class Code202_Tree { private: TreeNode *root; public: Code202_Tree();
  • 4. ~Code202_Tree(); bool isEmpty(); TreeNode *SearchTree(int Key); bool Insert(TreeNode *newNode); bool Insert(int Key, float f, int i, char *cA); bool Delete(int Key); void PrintOne(TreeNode *T); void PrintTree(); private: void ClearTree(TreeNode *T); TreeNode *DupNode(TreeNode * T); void PrintAll(TreeNode *T); }; #endif //IMPLEMENTATION .CPP FILE FOR BINARY TREE// #include #include #include "Code202_Tree.h" using namespace std; //-------------------------------------------- // Function: Code202_Tree() // Purpose: Class constructor. //-------------------------------------------- Code202_Tree::Code202_Tree() { root = NULL; return; } //-------------------------------------------- // Function: Code202_Tree() // Purpose: Class destructor. //-------------------------------------------- Code202_Tree::~Code202_Tree() { ClearTree(root); return;
  • 5. } //-------------------------------------------- // Function: ClearTree() // Purpose: Perform a recursive traversal of // a tree destroying all nodes. //-------------------------------------------- void Code202_Tree::ClearTree(TreeNode *T) { if(T==NULL) return; // Nothing to clear if(T->left != NULL) ClearTree(T->left); // Clear left sub-tree if(T->right != NULL) ClearTree(T->right); // Clear right sub-tree delete T; // Destroy this node return; } //-------------------------------------------- // Function: isEmpty() // Purpose: Return TRUE if tree is empty. //-------------------------------------------- bool Code202_Tree::isEmpty() { return(root==NULL); } //-------------------------------------------- // Function: DupNode() // Purpose: Duplicate a node in the tree. This // is used to allow returning a complete // structure from the tree without giving // access into the tree through the pointers. // Preconditions: None // Returns: Pointer to a duplicate of the node arg //-------------------------------------------- TreeNode *Code202_Tree::DupNode(TreeNode * T) { TreeNode *dupNode; dupNode = new TreeNode(); *dupNode = *T; // Copy the data structure
  • 6. dupNode->left = NULL; // Set the pointers to NULL dupNode->right = NULL; return dupNode; } //-------------------------------------------- // Function: SearchTree() // Purpose: Perform an iterative search of the tree and // return a pointer to a treenode containing the // search key or NULL if not found. // Preconditions: None // Returns: Pointer to a duplicate of the node found //-------------------------------------------- TreeNode *Code202_Tree::SearchTree(int Key) { int ValueInTree = false; TreeNode *temp; temp = root; while((temp != NULL) && (temp->Key != Key)) { if(Key < temp->Key) temp = temp->left; // Search key comes before this node. else temp = temp->right; // Search key comes after this node } if(temp == NULL) return temp; // Search key not found else return(DupNode(temp)); // Found it so return a duplicate } //-------------------------------------------- // Function: Insert() // Insert a new node into the tree. // Preconditions: None // Returns: int (TRUE if successful, FALSE otherwise) //-------------------------------------------- bool Code202_Tree::Insert(TreeNode *newNode) {
  • 7. TreeNode *temp; TreeNode *back; temp = root; back = NULL; while(temp != NULL) // Loop till temp falls out of the tree { back = temp; if(newNode->Key < temp->Key) temp = temp->left; else temp = temp->right; } // Now attach the new node to the node that back points to if(back == NULL) // Attach as root node in a new tree root = newNode; else { if(newNode->Key < back->Key) back->left = newNode; else back->right = newNode; } return true ; } //-------------------------------------------- // Function: Insert() // Insert a new node into the tree. // Preconditions: None // Returns: int (TRUE if successful, FALSE otherwise) //-------------------------------------------- bool Code202_Tree::Insert(int Key, float f, int i, char *cA) { TreeNode *newNode; // Create the new node and copy data into it newNode = new TreeNode(); newNode->Key = Key;
  • 8. newNode->fValue = f; newNode->iValue = i; strcpy(newNode->cArray, cA); newNode->left = newNode->right = NULL; // Call other Insert() to do the actual insertion return(Insert(newNode)); } //-------------------------------------------- // Function: Delete() // Purpose: Delete a node from the tree. // Preconditions: Tree contains the node to delete // Returns: int (TRUE if successful, FALSE otherwise) //-------------------------------------------- bool Code202_Tree::Delete(int Key) { TreeNode *back; TreeNode *temp; TreeNode *delParent; // Parent of node to delete TreeNode *delNode; // Node to delete temp = root; back = NULL; // Find the node to delete while((temp != NULL) && (Key != temp->Key)) { back = temp; if(Key < temp->Key) temp = temp->left; else temp = temp->right; } if(temp == NULL) // Didn't find the one to delete { return false; } else {
  • 9. if(temp == root) // Deleting the root { delNode = root; delParent = NULL; } else { delNode = temp; delParent = back; } } // Case 1: Deleting node with no children or one child if(delNode->right == NULL) { if(delParent == NULL) // If deleting the root { root = delNode->left; delete delNode; return true; } else { if(delParent->left == delNode) delParent->left = delNode->left; else delParent->right = delNode->left; delete delNode; return true; } } else // There is at least one child { if(delNode->left == NULL) // Only 1 child and it is on the right { if(delParent == NULL) // If deleting the root {
  • 10. root = delNode->right; delete delNode; return true; } else { if(delParent->left == delNode) delParent->left = delNode->right; else delParent->right = delNode->right; delete delNode; return true; } } else // Case 2: Deleting node with two children { // Find the replacement value. Locate the node // containing the largest value smaller than the // key of the node being deleted. temp = delNode->left; back = delNode; while(temp->right != NULL) { back = temp; temp = temp->right; } // Copy the replacement values into the node to be deleted delNode->Key = temp->Key; delNode->fValue = temp->fValue; delNode->iValue = temp->iValue; strcpy(delNode->cArray, temp->cArray); // Remove the replacement node from the tree if(back == delNode) back->left = temp->left; else back->right = temp->left;
  • 11. delete temp; return true; } } } //-------------------------------------------- // Function: PrintOne() // Purpose: Print data in one node of a tree. // Preconditions: None // Returns: void //-------------------------------------------- void Code202_Tree::PrintOne(TreeNode *T) { cout << T->Key << "tt" << T->fValue << "tt" << T->iValue << "tt" << T->cArray << " "; } //-------------------------------------------- // Function: PrintAll() // Purpose: Print the tree using a recursive // traversal // Preconditions: None // Returns: void //-------------------------------------------- void Code202_Tree::PrintAll(TreeNode *T) { if(T != NULL) { PrintAll(T->left); PrintOne(T); PrintAll(T->right); } } //-------------------------------------------- // Function: PrintTree() // Purpose: Print the tree using a recursive // traversal. This gives the user access
  • 12. // to PrintAll() without giving access to // the root of the tree. // Preconditions: None // Returns: void //-------------------------------------------- void Code202_Tree::PrintTree() { PrintAll(root); } Main file used to test the tree #include #include #include "Code202_Tree.h" using namespace std; int main(void) { Code202_Tree *theTree; TreeNode *newNode; // Do initialization stuff theTree = new Code202_Tree(); cout <<"Building tree... "; // Do simple insert of 15 nodes into tree. // Insert with keys in the order. // 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 // First 5 nodes are inserted using Insert1(). Remainder using Insert2() // Node 1 newNode = new TreeNode(); newNode->Key = 8; newNode->iValue = 2; newNode->fValue = 2.3f; strcpy(newNode->cArray, "Node1"); newNode->left = newNode->right = NULL; theTree->Insert(newNode); // Node 2 // Note: Each time a new node is allocated we reuse the same pointer // Access to the previous node is not lost because it is not in the tree.
  • 13. newNode = new TreeNode(); newNode->Key = 4; newNode->iValue = 4; newNode->fValue = 3.4f; strcpy(newNode->cArray, "Node2"); newNode->left = newNode->right = NULL; theTree->Insert(newNode); // Node 3 newNode = new TreeNode(); newNode->Key = 12; newNode->iValue = 8; newNode->fValue = 4.5f; strcpy(newNode->cArray, "Node3"); newNode->left = newNode->right = NULL; theTree->Insert(newNode); // Node 4 newNode = new TreeNode(); newNode->Key = 2; newNode->iValue = 16; newNode->fValue = 5.6f; strcpy(newNode->cArray, "Node4"); newNode->left = newNode->right = NULL; theTree->Insert(newNode); // Node 5 newNode = new TreeNode(); newNode->Key = 6; newNode->iValue = 32; newNode->fValue = 6.7f; strcpy(newNode->cArray, "Node5"); newNode->left = newNode->right = NULL; theTree->Insert(newNode); // Node 6 // Remainder of the nodes are inserted using Insert2() theTree->Insert(10, 7.8f, 64, "Node6"); // Node 7 theTree->Insert(14, 8.9f, 128, "Node7");
  • 14. // Node 8 theTree->Insert(1, 9.0f, 256, "Node8"); // Node 9 theTree->Insert(3, 0.9f, 512, "Node9"); // Node 10 theTree->Insert(5, 9.8f, 1024, "Node10"); // Node 11 theTree->Insert(7, 8.7f, 2048, "Node11"); // Node 12 theTree->Insert(9, 7.6f, 4096, "Node12"); // Node 13 theTree->Insert(11, 6.5f, 8192, "Node13"); // Node 14 theTree->Insert(13, 5.4f, 16384, "Node14"); // Node 15 theTree->Insert(15, 4.3f, 32768, "Node15"); cout <<"All nodes inserted "; // Print the tree cout <<"----------------------------------------------------- "; theTree->PrintTree(); cout <<"Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; // Find some nodes and print them cout <<"----------------------------------------------------- "; cout <<"Testing the search function "; newNode = theTree->SearchTree(13); if(newNode != NULL) { theTree->PrintOne(newNode); delete newNode; // Remember this is a duplicate node of the one in // in the tree and main() is responsible for deleting it. } else cout <<"Search key not found. "; newNode = theTree->SearchTree(6);
  • 15. if(newNode != NULL) { theTree->PrintOne(newNode); delete newNode; } else cout <<"Search key not found. "; newNode = theTree->SearchTree(1); if(newNode != NULL) { theTree->PrintOne(newNode); delete newNode; } else cout <<"Search key not found. "; newNode = theTree->SearchTree(25); // Note: there is no Key=25 in the tree if(newNode != NULL) { theTree->PrintOne(newNode); delete newNode; } else cout <<"Search key not found. "; // Delete some nodes cout <<"----------------------------------------------------- "; cout <<"Testing delete function "; cout <<"----------------------------------------------------- "; cout <<"Testing deleting a leaf... "; theTree->Delete(7); // Delete a known leaf theTree->PrintTree(); cout <<"Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; cout <<"----------------------------------------------------- "; cout <<"Testing deleting a node with 2 children... "; theTree->Delete(12); // Delete a node known to have 2 children
  • 16. theTree->PrintTree(); cout <<"Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; cout <<"----------------------------------------------------- "; cout <<"Testing deleting a node with 1 child... "; theTree->Delete(6); // Delete a node known to have only 1 child theTree->PrintTree(); cout <<"Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; cout <<"----------------------------------------------------- "; cout <<"Testing trying to delete a node that is not in the tree... "; theTree->Delete(7); // Delete a node that is not there theTree->PrintTree(); cout <<"Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; cout <<"----------------------------------------------------- "; cout <<"Testing deleting the root... "; theTree->Delete(8); // Delete the root theTree->PrintTree(); cout <<"Done. Press Enter to continue..."; cin.get(); cout <<"----------------------------------------------------- "; return 0; }