Write a C program that reads the words the user types at the
Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Tree. When a duplicate is encountered do not store the letter again and instead keep track of the count in the tree. Once the Binary Search tree has been created print out the tree both inorder and reverse order. Also print the highest and lowest alphabetically letter in the tree if any.
Solution
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void preorder(node *);
findMinimum(struct node* root)
findMaximum(struct node* root)
void reverseLevelOrder(struct node* root)
void main(int argc,char argv) {
char argv = \'N\';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();
printf(\"\ Program For Binary Search Tree \");
do {
printf(\"\ 1.Create\");
printf(\"\ 2.Search\");
printf(\"\ 3.Recursive Traversals\");
printf(\"\ 4.Exit\");
printf(\"\ Enter your choice :\");
scanf(\"%d\", &argc);
switch (argc) {
case 1:
do {
new_node = get_node();
printf(\"\ Enter The Element \");
scanf(\"%d\", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf(\"\ Want To enter More Elements?(y/n)\");
argv= getch();
} while (argv == \'y\');
break;
case 2:
if (root == NULL)
printf(\"Tree Is Not Created\");
else {
printf(\"\ The Preorder display : \");
preorder(root);
}
break;
}
} while (argv != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
return newNode(key);
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
return newNode(key);
else
insert(root->rchild, new_node);
}
}
if (key == node->key)
{
(node->count)++;
return node;
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf(\"%d\", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
// Returns maximum value in a given Binary Tree
int findMaximum(struct node* root)
{
// Base case
if (root == NULL)
return INT_MAXIMUM;
// Return maximum of 3 values:
// 1) Root\'s data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMaximum (root->lchild);
int rres = findMaximum (root->rchild);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Returns minimum value in a given Binary Tree
int findMinimum(struct node* root)
{
// Base case
if (root == NULL)
return INT_MINIMUM;
// Return minimum of 3 values:
// 1) Root\'s data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMinimum(root->lchild);
int rres = findMinimum(root->rchild);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
void reverseLevelOrder(struct node* root)
{
int h = height(root);
int i;
for (i=h; i>=1; i--) //THE ONLY LINE DIFFERENT FROM NORMAL LEVEL ORDER
printGivenLevel(root, i);
}
![Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search](/WebImages/9/write-a-c-program-that-reads-the-words-the-user-types-at-the-1000850-1761515545-0.webp)
![Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search](/WebImages/9/write-a-c-program-that-reads-the-words-the-user-types-at-the-1000850-1761515545-1.webp)
![Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search](/WebImages/9/write-a-c-program-that-reads-the-words-the-user-types-at-the-1000850-1761515545-2.webp)
![Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search](/WebImages/9/write-a-c-program-that-reads-the-words-the-user-types-at-the-1000850-1761515545-3.webp)
![Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search Write a C program that reads the words the user types at the command prompt (using the \'int argc, char * argv[] and store each unique letter in a Binary Search](/WebImages/9/write-a-c-program-that-reads-the-words-the-user-types-at-the-1000850-1761515545-4.webp)