Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I will keep reposting this for 350 points! I need as much of the code as possibl

ID: 3621753 • Letter: I

Question

I will keep reposting this for 350 points! I need as much of the code as possible

Objective:
Write a C program that takes as input a list of integers (in random order), stores the integers in a binary tree, and uses a recursive algorithm to search the tree for a particular value.

Background:
Although binary trees can be represented with arrays, we would still have to deal with some memory issues ( ie: we might need enough contiguous memory for an extremely large tree, what if the tree is dynamic? what if some nodes only have 1 child?).

A more efficient way of handling binary trees could be with the use of dynamically allocated structures for the nodes, which are linked together with pointers. For example, we could have the following:


typedef struct node Node;
typedef Node* NodePtr;
typedef Node* Tree;
struct node
{
int value;
NodePtr Left;
NodePtr Right;
};


The above structure also works nicely for recursive functions, especially when adding data to a tree, searching for data in a tree, and (most important) freeing a tree.


Specifications: PLEASE USE THE SAME FUNCTION NAMES AS BELOW TO SAVE ME CONFUSION :)
1. The program will read data from an input file "Data.txt" and store the data in a tree. (Note: Build a complete tree.)
2. The program will prompt the user for a value to search for in the tree.
3. The program will use a pre-order traversal to search for the given value. (Which sort function is best for a tree?)
4. If the value is found, the program will announce that to the user and will tell the user the value of the left and right child of the given number. If the value is not found, program will politely report the situation.
5. The program will have an abstract data type (ADT) for the tree, and will include (as a minimum) the following functions for the ADT:
a. void InsertInTree(int value, Tree T)
b. NodePtr SearchTree(int value, Tree T)
c. void DeleteTree(Tree T)
7. The program will follow good programming practices.
a. It will check for failures opening the files and will provide a graceful exit if a failure exists.
b. The program will have appropriate programmer-defined functions so that the main function
provides a highlight of the program.

Explanation / Answer

Here is the code I came up with. Minor errors do exist...the file read won't work for some reason I can't figure out, but the basics are there. Also, since there is no promise that they will only search for good numbers, you should put in a check to make sure the number is there. Here is the main.cpp: #include #include #include #include #include #include #include "tree.h" using namespace std; /* @author: Kayleigh Duncan @instuctor: Sarah Gothard @date: 6/6/09 */ void printAll( binaryTree& t ) { //cout data > x){ searchTree(seeker->left, x); } else if(seeker->data right, x); } else if(seeker->data == x){ return seeker; } } // insert // // Purpose: insert a node containing x into the search tree, if it does not already exists // // Precondition: properly initialized search tree exist and is referenced by the internal pointer root // Postcondition: if the value x is not in the tree, a node containing x is insert as a new leaf of the search tree // void binaryTree::insert( int x ) { InsertInTree(root, x); } /** insert inserts a new treeNode. If there are no other nodes, the node inserted is the parent. If the value being inserted is less than the parent's value, then the value is inserted to the left. Otherwise, it is inserted to the right. */ void binaryTree::InsertInTree(treeNode*& parent, const int &d){ if (parent == NULL){ parent = new treeNode(d); } else if (d data){ InsertInTree(parent->left, d); } else InsertInTree(parent->right, d); } // preorder // // Purpose: to display the tree nodes inorder // e.g. Tree Contents (preorder): 20 10 30 // e.g. Tree Contents (preorder): empty // // Precondition: the tree is properly initialized // Postcondition: none // // void binaryTree:: preorder() const { preorder(root); cout