Generate a new element one at a time

Assignment Help Data Structure & Algorithms
Reference no: EM13948841

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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#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);
}

Reference no: EM13948841

Questions Cloud

Returns for various states of the economy : Stock A has the following returns for various states of the economy:
Compute the number of pans that must be sold for ingo : Compute the number of pans that must be sold for Ingo to break even. How many pans must be sold for Ingo to earn a before-tax profit of $12,600?
What is the most that you should pay for the annuity : You just inherited some money and a broker offers to sell you an annuity that pays $9,000 at the end of each year for 20 years, with the first payment coming 5 YEARS FROM TODA. You could earn 5% on your money in other investments with equal risk. Wha..
Difference between science fiction and myth : What is the difference between Science Fiction and Myth, According to Suvin
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.
Financing outstanding-debt : The Saunders Investment Bank has the following financing outstanding. Debt: 60,000 bonds with a coupon rate of 6 percent and a current price quote of 109.5; the bonds have 20 years to maturity. 230,000 zero coupon bonds with a price quote of 17.5 and..
Dividends tend to fluctuate in direct relation to changes : Based on the dividend growth model, the price of a stock will remain constant if the dividend is cut, provided that the: Dividends tend to fluctuate in direct relation to changes in annual earnings. Managers are less concerned with the change in the ..
Person to become a successful entrepreneur : What qualifies a person to become a successful entrepreneur? Why are you interested in an entrepreurship focused exchange program?
Calculate the break-even units rounded to the nearest unit : Calculate the break-even units, rounded to the nearest unit. Calculate the units needed to earn $18,000, rounded to the nearest unit.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Methods of generated data experiments

Overview of the different methods of generated data experiments - Some of visualization techniques are provided in this research to show how well the predictive modelling is performing and show an interesting method in the data related to the proje..

  Describe the need for complex data structures

Describe the need for complex data structures and how they are used. Describe the design and application of arrays and how the array simplifies program development.

  What are the characteristics of a good algorithm

What is an algorithm? What are the characteristics of a good algorithm and what do you mean by complexity of an algorithm? Explain the meaning of worst case analysis and best case analysis with an example.

  Discuss the ways in which yfile can optimize to reduce size

Discuss the ways in which you can optimize a file in order to reduce file size and maintain quality.

  Explain advantages of eager decision tree algorithm

Explain advantages and disadvantages of new algorithm compared with eager decision tree algorithm, and advantages and disadvantages of new algorithm compared with lazy kNN algorithm.

  Creating a database design in visio

Designing Databases with Visio Professional: A Tutorial," to help you complete Section 1: Visio Database Design. (Note: This tutorial focuses on the use of Microsoft Visio.

  Create algorithm which generates access control matrix

Create an algorithm which generates the access control matrix A for any given history matrix H of the Chinese Wall model.

  How many students need to be entered

Write a program that would allow a user to enter student names and Final grades (e.g. A,B,C,D,F) from their courses. You do not know how many students need to be entered

  Graph algorithm.

Graph algorithm. a. Draw a depth-first search tree based on a given graph. Assume that adjacent vertices are visited in alphabetical order. Then compute the Num and Low values for each vertex, and find out the articulation points i

  Diferentiate losles and losy algorithms

Write briefly about he Computer System Components. Diferentiate losles and losy algorithms

  Refresh address counter

A microcomputer memory is built from 64K X 1 DRAM, with DRAM cell array organized into 256 rows. Each row requires being refreshed at least once every four ms, strictly on a periodic basis.

  Describe a computation of the timer-based protocol

Describe a computation of the timer-based protocol in which the receiver opens a connection upon receipt of a packet with a sequence number greater than zero.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd