Computer Science Homework Solutions
Problem
#19522

Among other things, a binary search tree can be used for sorting data elements. This project is to randomly generate a sequence of integer numbers, insert the first 20 distinct numbers into a binary search tree, and finally produce an in-order listing of the tree.

Among other things, a binary search tree can be used for sorting data elements. This project is to randomly generate a sequence of integer numbers, insert the first 20 distinct numbers into a binary search tree, and finally produce an in-order listing of the tree. You are provided with the declarations and two functions for binary search trees (see the appended). One of the functions is Insert that adds a new element to a binary search tree. And the other is a  lookup which checks if a given element is already in a binary search tree. In this project, you need to define a print function that prints tree elements in in-order as well as a count function that counts and returns the number of tree nodes in a binary search tree. In addition you need to complete the main function. You may use the built-in random number function to generate new elements for inserting as shown below:

srand(time(NULL));     //do it at the beginning of main function
newElem = rand() % 100;
                //generate a new element one at a time
Then you can check for each new element if it is already there using the lookup function and if there are enough elements in the tree using the count function before doing insert. Finally, use the print function to show the result.



see attached file

Attached file(s):
Attachments
Question.doc  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

Question.doc
Among other things, a binary search tree can be used for sorting data
elements. This project is to randomly generate a sequence of integer
numbers, insert the first 20 distinct numbers into a binary search tree,
and finally produce an in-order listing of the tree. You are provided
with the declarations and two functions for binary search trees (see the
appended). One of the functions is Insert that adds a new element to a
binary search tree. And the other is lookup which checks if a given
element is already in a binary search tree. In this project, you need to
define a print function that prints tree elements in in-order as well as
a count function that counts and returns the number of tree nodes in a
binary search tree. In addition you need to complete the main function.
You may use the built-in random number function to generate new elements
for inserting as shown below:

srand(time(NULL)); /* do it at the beginning of main function */

newElem = rand() % 100; /* generate a new element one at a time */

Then you can check for each new element if it is already there using the
lookup function and if there are enough elements in the tree using the
count function before doing insert. Finally, use the print function to
show the result.

/* declarations and function definitions for binary search trees */

#include

#include

#include

#define TRUE 1

#define FALSE 0

typedef int BOOLEAN;

typedef int ETYPE;

typedef struct NODE *TREE;

struct NODE {

ETYPE element;

TREE leftChild, rightChild;

};

TREE insert(ETYPE x, TREE T)

{

if (T == NULL) {

T = (TREE) malloc(sizeof(struct NODE));

T->element = x;

T->leftChild = NULL;

T->rightChild = NULL;

}

else if (x < T->element)

T->leftChild = insert(x, T->leftChild);

else if (x > T->element)

T->rightChild = insert(x, T->rightChild);

return T;

}

BOOLEAN lookup(ETYPE x, TREE T)

{

if (T == NULL)

return FALSE;

else if (x == T->element)

return TRUE;

else if (x < T->element)

return lookup(x, T->leftChild);

else /* x must be > T->element */

return lookup(x, T->rightChild);

}
Solution
What is this?
By OTA - Overall OTA Rating
Yaohua Zhu, MSc - 5/5
Purchase Cost Now
$2.19 CAD
Included in Download
  • Plain text response
  • Attached file(s):
    • solution.cpp
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
  • Data Structure C++ - 1. Show the intermediate and final results of inserting 20, 10, 40, 50, 90, 30, 60, 70, 80, 49, 51, and 52 into an initially empty AVL tree.
  • Declarations in C - ****This is "C" Programming**** I selected "b" but I've always seem to get lost doing Boolean. Assuming the following declarations: int A=1, B=2, C=3, D=4; What is the value of the fol ...
  • Write a program whose main function is merely a collection of variable declarations and function calls. - Please see attached file. Write a program whose main function is merely a collection of variable declarations and function calls. This program reads a text and outputs the letters, together with t ...
  • Data Structures with C++ (STL) - Consider the class declaration class demoClass { public: // assign arguments as initial values for the data members demoClass(int a = 5, int b = 10); // function ...
  • Testcase Creation (Example: Vending Machine) - Derive test cases from the following informal specifications for a simple vending machine: Program CoinBox accepts quarters and distributes coffee. Users may: - insert a quarter - ask for a cu ...
Browse