Free Essay

Submitted By kloiz

Words 1523

Pages 7

Words 1523

Pages 7

Number Of Nodes:

1. /* 2. * C Program to Find the Number of Nodes in a Binary Tree 3. */ 4. #include <stdio.h> 5. #include <stdlib.h> 6. 7. /* 8. * Structure of node 9. */ 10. struct btnode 11. { 12. int value; 13. struct btnode *l; 14. struct btnode *r; 15. }; 16. 17. void createbinary(); 18. void preorder(node *); 19. int count(node*); 20. node* add(int); 21. 22. typedef struct btnode node; 23. node *ptr, *root = NULL; 24. 25. int main() 26. { 27. int c; 28. 29. createbinary(); 30. preorder(root); 31. c = count(root); 32. printf("\nNumber of nodes in binary tree are:%d\n", c); 33. } 34. /* 35. * constructing the following binary tree 36. * 50 37. * / \ 38. * 20 30 39. * / \ 40. * 70 80 41. * / \ \ 42. *10 40 60 43. */ 44. void createbinary() 45. { 46. root = add(50); 47. root->l = add(20); 48. root->r = add(30); 49. root->l->l = add(70); 50. root->l->r = add(80); 51. root->l->l->l = add(10); 52. root->l->l->r = add(40); 53. root->l->r->r = add(60); 54. } 55. 56. /* 57. * Add the node to binary tree 58. */ 59. node* add(int val) 60. { 61. ptr = (node*)malloc(sizeof(node)); 62. if (ptr == NULL) 63. { 64. printf("Memory was not allocated"); 65. return; 66. } 67. ptr->value = val; 68. ptr->l = NULL; 69. ptr->r = NULL; 70. return ptr; 71. } 72. 73. /* 74. * counting the number of nodes in a tree 75. */ 76. int count(node *n) 77. { 78. int c = 1; 79. 80. if (n == NULL) 81. return 0; 82. else 83. { 84. c += count(n->l); 85. c += count(n->r); 86. return c; 87. } 88. } 89. 90. /* 91. * Displaying the nodes of tree in preorder 92. */ 93. void preorder(node *t) 94. { 95. if (t != NULL) 96. { 97. printf("%d->", t->value); 98. preorder(t->l); 99. preorder(t->r); 100. } 101. }

No. Of Leaf Nodes:

#include <stdio.h>

#include <stdlib.h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node

{

int data; struct node* left; struct node* right;

};

/* Function to get the count of leaf nodes in a binary tree*/ unsigned int getLeafCount(struct node* node)

{

if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+ getLeafCount(node->right);

}

/* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data)

{

struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node);

}

/*Driver program to test above functions*/ int main()

{

/*create a tree*/ struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); /*get leaf count of the above created tree*/ printf("Leaf count of the tree is %d", getLeafCount(root)); getchar(); return 0;

}

Depth Of The Tree:

#include<stdio.h>

#include<stdlib.h>

/* A binary tree node has data, pointer to left child and a pointer to right child */ struct node

{

int data; struct node* left; struct node* right;

};

/* Compute the "maxDepth" of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node.*/ int maxDepth(struct node* node)

{

if (node==NULL) return 0; else { /* compute the depth of each subtree */ int lDepth = maxDepth(node->left); int rDepth = maxDepth(node->right); /* use the larger one */ if (lDepth > rDepth) return(lDepth+1); else return(rDepth+1); }

}

/* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data)

{

struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node);

}

int main()

{

struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printf("Hight of tree is %d", maxDepth(root)); getchar(); return 0;

}

Number of internal Nodes = no of nodes- no of leaf nodes

No. of internal nodes =

1. /*

2. * C Program to Find the Number of Nodes in a Binary Tree

3. */

4. #include <stdio.h>

5. #include <stdlib.h>

6.

7. /*

8. * Structure of node

9. */

10. struct btnode

11. {

12. int value;

13. struct btnode *l;

14. struct btnode *r;

15. };

16.

17. void createbinary();

18. void preorder(node *);

19. int count(node*);

20. node* add(int);

21.

22. typedef struct btnode node;

23. node *ptr, *root = NULL; unsigned int getLeafCount(struct node* node)

{

if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+ getLeafCount(node->right);

}

25. int main()

26. {

27. int c;

28.

29. createbinary();

30. preorder(root);

31. c = count(root); int g = getLeafCount(root); int internal = c- g;

32. printf("\nNumber of internal nodes in binary tree are:%d\n", internal);

33. }

34. /*

35. * constructing the following binary tree

36. * 50

37. * / \

38. * 20 30

39. * / \

40. * 70 80

41. * / \ \

42. *10 40 60

43. */

44. void createbinary()

45. {

46. root = add(50);

47. root->l = add(20);

48. root->r = add(30);

49. root->l->l = add(70);

50. root->l->r = add(80);

51. root->l->l->l = add(10);

52. root->l->l->r = add(40);

53. root->l->r->r = add(60);

54. }

55.

56. /*

57. * Add the node to binary tree

58. */

59. node* add(int val)

60. {

61. ptr = (node*)malloc(sizeof(node));

62. if (ptr == NULL)

63. {

64. printf("Memory was not allocated");

65. return;

66. }

67. ptr->value = val;

68. ptr->l = NULL;

69. ptr->r = NULL;

70. return ptr;

71. }

72.

73. /*

74. * counting the number of nodes in a tree

75. */

76. int count(node *n)

77. {

78. int c = 1;

79.

80. if (n == NULL)

81. return 0;

82. else

83. {

84. c += count(n->l);

85. c += count(n->r);

86. return c;

87. }

88. }

89.

90. /*

91. * Displaying the nodes of tree in preorder

92. */

93. void preorder(node *t)

94. {

95. if (t != NULL)

96. {

97. printf("%d->", t->value);

98. preorder(t->l);

99. preorder(t->r);

100. }

101. }

For 1 through 6, the most important concept is of the Big-Oh notation f(n) = O(g(n)) means there are positive constants c and k, such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k. The values of c and k must be fixed for the function f and must not depend on n.

So, for the questions

Since We have to prove that f(n) < cg(n), only the terms with the HIGHEST COEFFECIENT matters, or the term which has the MAXIMUM VALUE

1. The maximum of those two will be used for the Big-Oh definition. This is just the definition of Big-Oh.

Since O(f(n)+g(n)) is effectively the O() of whichever one if bigger, that is exactly what’s written on the Left hand side.

2. Similarly, since n^5 is the fastest growing term, this is the only term which will matter in finding the Big-Oh, since big Oh needs to prove that f(n) can be SUPPRESSED by g(n) in every case

3. 2^n+1 can be written as 2*2^n, which is of the form c*2^n. Therefore, 2^n+1 = O(2^n)

4. Sum of i^2, i=0 to n is equal to n*(n+1)*(2n+1)/6 which in turn has n^3 as the highest degree term, i.e. the fastest growing term. Hence, O(n^3) is the answer to this.

5. Use this as a practice exercise for yourself. It’s really easy and all you have to do is arrange them in their order of growth, i.e. loglogn grows slower than log^2 (n) , etc

2^100, loglogn, log^2n, sqrt(logn), 1/n, n ^0.01 ,[3sqrt(n), Sqrt(n)], 5n, [nlog4n,6nlogn], 2nlog^2n,4*n^3/2, n^2logn

2^logn, 4^logn, 2^n, [4^n, 2^2^n]

6. Wrong Question. T(n) will be equal to 2^(n+1) – 3

7. Root- if(p->parent == NULL)

Parent- if(p->leftChild !=NULL || p->rightChild !=NULL)

Left Child if(p->parent->left == p)

Right Child id(p->parent->right == p)

isInternal if(p->leftChild !=NULL &&p->rightChild !=NULL && p->parent!=NULL)

Premium Essay

...Random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random......

Words: 291 - Pages: 2

Premium Essay

...random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random random rand......

Words: 273 - Pages: 2

Free Essay

...Introduction to Randomness and Random Numbers. Randomness and random numbers have traditionally been used for a variety of purposes, for example games such as dice games. With the advent of computers, people recognized the need for a means for a means of introducing randomness into a computer program. Surprising as it may seem, however, it is difficult to get a computer to do something by chance. A computer running a program follows its instructions blindly and is therefore completely predictable. Computer engineers chose to introduce randomness into computers in the form of pseudo-random number generators. As the name suggest, pseudo-numbers are not truly random. Rather, they are computed from a mathematical formula or simply taken from a pre-calculated list. A lot of research has gone into pseudo-random number theory and modern algorithms for random numbers have the characteristic that they are predictable, meaning they can be predicted if you know where in the sequence the first number is taken from. For some purposes, predictability, is a good characteristic, for others it is not., Random numbers are used for computer games but they are also used on a more serious scale for the generation of cryptographic keys are for some classes of scientific experiments. For scientific experiments, it is convenient that a series of random numbers can be replayed for use in several experiments, and pseudo-random numbers are well suited for this purpose. For cryptographic use,......

Words: 262 - Pages: 2

Free Essay

...contributions to music in America by directing composers' groups, organized concerts of American music, lectured around the country, and wrote books and magazine articles. 87. One of Ginastera’s early works, Estancia Suite, is nationalistic and uses Argentinean folk material, including popular dances. 88. Composers began to shift from tonality to the twelve-tone system because they discovered it was a compositional technique rather than a special musical style. 89. Twelve-tone compositional techniques used to organize rhythm, dynamics, tone color, and other dimensions of music to produce totally controlled and organized music are called Serialism. 90. In chance, or aleatory music the composer Chooses pitches, tone colors, and rhythms by random methods. 91.An example of aleatoric music is John Cage 92.Minimalist music is characterized by A steady pulse, clear tonality, and insistent repetition of short melodic patterns. 93. Intervals smaller than the half step are called Microtones. 94.Around 1940, John Cage invented the prepared piano, a(n) grand piano whose sound is altered by objects such as bolts, screws, rubber bands, pieces of felt, paper, and plastic inserted between the strings of some of the keys. 95.Edgard Varèse's Poème électronique All answers are correct. designed for the 1958 Brussels World Fair; one of the earliest masterpieces of electronic music created in a tape studio; composed in collaboration with the famous architect Le Corbusier. 96.Ellen......

Words: 1777 - Pages: 8

Premium Essay

...environment at hand and lastly the cost of establishment and implementation of a HR department. Descriptive study has been undertaken to address this paper due to the absence of HR department in the university. Through this study feasibility of an HR department is inquired. The methods used for data collection and research is entirely based on a survey which consists of a combination of questionnaires and interviews which have enabled us to collect primary data for this study as no secondary data can be found due to the absence of the HR department in the university. Multi-stage sampling was carried out where sampling is conducted by using 2 techniques. Here they used random sampling and systematic sampling. Faculty members and employees from the administrative staff were selected as sample. Although selection was random but was based on certain set of rules i.e. systematic sampling. Faculty members were selected from all departments but on the basis of specific seniority where faculty members defined as assistant professors and senior lecturers with at least 1 year experience. In case of administrative staff their seniority and experience in the campus was highly considered when drawing a sample. The sample size for research consisted of 30% of the total number of faculty members, therefore 25 faculty members and about 5 staff members from the administration department. Q2. Why did the author select these methods for his research paper? Ans2. We will describe the......

Words: 3905 - Pages: 16

Premium Essay

...audits Trace Analysis: credit card records computer cookie records garbology - looking for traces of purchase patterns in garbage detecting store traffic patterns by observing the wear in the floor (long term) or the dirt on the floor (short term) exposure to advertisement. Thus it can be seen that as technology advances, observational menthods are becoming cheap as well as more useful. ans 8d EVPI- · In decision theory, the expected value of perfect information (EVPI) is the price that one would be willing to pay in order to gain access to perfect information. · The problem is modeled with a payoff matrix Rij in which the row index i describes a choice that must be made by the payer, while the column index j describes a random variable that the payer does not yet have knowledge of, that has probability pj of being in state j. If the payer is to choose i without knowing the value of j, the best choice is the one that maximizes the expected monetary value: where is the expected payoff for action i i.e. the expectation value, and is choosing the maximum of these expectations for all available actions. On the other hand, with perfect knowledge of j, the player may choose a value of i that optimizes the expectation for that specific j. Therefore, the expected value given perfect information is where is the probability that the system is in state j, and is the pay-off if one follows action i while the system is in state j. Here indicates the best choice......

Words: 3465 - Pages: 14

Free Essay

...Türkçe * Українська * 中文 * More than 50,000 articles: * Bosanski * Български * Dansk * Eesti * Ελληνικά * English (simple) * Esperanto * Euskara * Galego * עברית * Hrvatski * Latviešu * Lietuvių * Norsk nynorsk * Slovenčina * Slovenščina * ไทย Complete list of Wikipedias ------------------------------------------------- Navigation menu * Create account * Log in * Main Page * Talk * Read * View source * View history ------------------------------------------------- Top of Form Bottom of Form * Main page * Contents * Featured content * Current events * Random article * Donate to Wikipedia * Wikipedia store Interaction * Help * About Wikipedia * Community portal * Recent changes * Contact page Tools * What links here * Related changes * Upload file * Special pages * Permanent link * Page information * Wikidata item Print/export * Create a book * Download as PDF * Printable version Languages * Simple English * العربية * Bahasa Indonesia * Bahasa Melayu * Bosanski * Български * Català * Čeština * Dansk * Deutsch * Eesti * Ελληνικά * Español * Esperanto * Euskara * فارسی * Français * Galego * 한국어 * עברית * Hrvatski * Italiano *......

Words: 1160 - Pages: 5

Free Essay

...When is Random Sampling not the best approach to sample selection? Provide an example A random sample is one in which every member of the population has an equal chance of being selected to be part of the sample. For example, you could obtain a random sample by having everyone in a population roll a die and choosing those people who roll a 6. In contrast, the sample would not be random if you chose everyone taller than 6 feet, because not everyone would have an equal chance of being selected ( Bennett, Briggs, Triola, 2009). Example: Telephone Book Sampling If you want to conduct an opinion poll in which the population is all the residents in a town. Could you choose a random sample from selecting names from the local telephone book? A sample drawn from a telephone book is not a random sample of the town population because phone books are missing a lot of names, and anyone whose name is missing has no chance of being selected. The phone book will be missing names and if two or more people share the same phone number the listing could be under one name. The people who choose to have an unlisted phone number or who only use a cell phone do not have the same chance of being polled as the one whom are listed ( Bennett, Briggs, Triola, 2009) Bennett, J. O., Briggs, W. L., Triola, M. F., (2009). Statistical Reasoning for everyday life. (3rd ed.). Retrieved from University of Phoenix eBook. Angela Wilson...

Words: 255 - Pages: 2

Free Essay

...random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay random essay......

Words: 280 - Pages: 2

Free Essay

...is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random. Life is Beautiful. Random Life Life is Random.......

Words: 416 - Pages: 2

Free Essay

...Topic # 3: Random Variables & Processes & Noise T1. B.P. Lathi, Modern Digital and Analog Communication Systems, 3rd Edition, Oxford University Press, 1998: OR 4th Edition 2010 Chapter 8, 9 & 12 T2. Simon Haykin & Michael Moher: Communication Systems; John Wiely, 4th Edition OR 5th Edition, 2010, 5/e. : Chapter 5 R1.DIGITAL COMMUNICATIONS Fundamentals and Applications: ERNARD SKLAR and Pabitra Kumar Ray; Pearson Education 2009, 2/e. : ( Section 5.5) August 11- 18, 2014 1 What is Noise ? Desired Signal : The one that is needed. Effect of Noise : Since the noise adds to the signal, it lives with it. Neither amplification nor the filtering can alleviate the effect of noise on the desired signal. Undesired Signal : The one that gets added to the desired signal when the desired signal is passing through the medium, amplifiers, mixers, filters and other parts of the communication channel between the source and the destination. Noise : The undesired signal that adds to the desired signal and reaches the destination. The only way to keep away from the effects of noise is to see that less amount of noise, relative to the desired signal, is present at the destination Interference: Intentional or unintentional un desired signals that interfere with communication process. 2 ELECTRICAL ELECTRONICS COMMUNICATION INSTRUMENTATION Noise Sources Externally Generated Internally Generated Thermal noise : Random Motion of electrons due to temperature in...

Words: 1408 - Pages: 6

Free Essay

...Pseudo-Random Number Generators (PRNGs) As the word ‘pseudo’ suggests, pseudo-random numbers are not random in the way you might expect, at least not if you're used to dice rolls or lottery tickets. Essentially, PRNGs are algorithms that use mathematical formulae or simply precalculated tables to produce sequences of numbers that appear random. A good example of a PRNG is the linear congruential method. A good deal of research has gone into pseudo-random number theory, and modern algorithms for generating pseudo-random numbers are so good that the numbers look exactly like they were really random. The basic difference between PRNGs and TRNGs is easy to understand if you compare computer-generated random numbers to rolls of a die. Because PRNGs generate random numbers by using mathematical formulae or precalculated lists, using one corresponds to someone rolling a die many times and writing down the results. Whenever you ask for a die roll, you get the next on the list. Effectively, the numbers appear random, but they are really predetermined. TRNGs work by getting a computer to actually roll the die — or, more commonly, use some other physical phenomenon that is easier to connect to a computer than a die is. PRNGs are efficient, meaning they can produce many numbers in a short time, and deterministic, meaning that a given sequence of numbers can be reproduced at a later date if the starting point in the sequence is known. Efficiency is a nice characteristic if your......

Words: 1013 - Pages: 5

Free Essay

...Random Drug Testing: Waste of Time Her grades fell. She was always tired. She never seemed to be able to focus at school. Classes she used to be interested in became utterly mundane. Friends she used to care about became replaceable. She stopped spending time with her family. She sat on the bench at every soccer game instead of becoming the star player her coaches thought she could. This is what addiction to drugs can do to a young person’s life. Addiction can take away everything that once made that young person happy. The only thing that matters anymore is the drug, getting high, and getting higher. It is a horrible and tragic thing that destroys so many young lives. Some people think that in order to prevent these situations, the best solution is random drug testing. But this is not a reasonable solution whatsoever. Many more students are using and selling drugs as they roam around the campus, but will never be “caught” with such a fickle and illusive process. Random student drug testing is not a plausible solution for the drug problem in public schools; it is unreliable and it infringes on the lives of those students involved. Those who support random drug testing argue that the growing trend of drug testing a small population of students in a school is effective at attacking the drug abuse problem, because fewer students will use when there is an obvious consequence (Drug Testing in School Activities 2). They b... ... middle of paper ... ...o Hear......

Words: 322 - Pages: 2

Free Essay

...essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think about it…. This essay is very random. Think......

Words: 289 - Pages: 2

Free Essay

...Random numbers in C++ and The Pythagorean Theorem Name Course Date Random numbers in C++ and The Pythagorean Theorem Introduction Computer programs in light of the technological advances that have been made, arguably make up for the most important concepts in such developments. A set of instructions designed to assist a computer to prefer a given task is referred to as a computer program. There are numerous languages used to create/design computer for instance Java Script, Java, C++, SQL and Sage (Laine, 2013). Computer programming is defined as a process of developing a working set of computer instructions meant to aid the computer in the performance of a given task. Computer programming starts with the formulation of a valid computer problem. This process is then followed by the development of an executable computer program, for instance Firefox Web Brower (Laine 2013). It is worth noting that there are other programs in the same realm. Computer programming is a diverse field that is of utmost importance in the modern world, especially with the continuous expansion of the internet. Perhaps the relevance of this can be underlined by the fact that computer programming has carved out as a course on itself. Computer programming is offered under several courses studied in colleges and universities (Laine, 2013). Computer programming is not only for computer students but for all who use computers on a day to day basis. This is by extension everyone since the...

Words: 9330 - Pages: 38