(in C) Binry Srch tree problem:
1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements)
2. strcmp() can be used to compare data and decide were to insert a new node.
3. The output should include:
-print the tree as in-order traversal.
-print the character count in the tree.
-Ask user input for a word to search, print an output if either found or not. (typing exit will exit program)
-print height of tree
Sample in.txt:
13
lion
koala
coyote
chicken
rat
roach
moose
shark
puma
fox
wolf
duck
horse
//Please use following struct and function//
struct tree_node {
int data;
struct tree_node* lft;
struct tree_node* rght;
};
//even nodes//
int prntEvn(struct tree_node *current_ptr) {
if (currnt_ptr != NULL) {
if (currnt_ptr->data % 2 == 0)
printf(“%d “, current_ptr->data);
prntEvn(current_ptr->lft);
prntEvn(current_ptr->rght);
}
}
//C program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct tree_node {
char data[10];
struct tree_node* lft;
struct tree_node* rght;
}node;
node* createnode(char data[]){
node* newnode=(node*) malloc(sizeof(node));
strcpy(newnode->data , data);
newnode->lft=NULL;
newnode->rght=NULL;
return newnode;
}
node* insertnode(node*tree,char data[]){
if(!tree)return createnode(data);
if(strcmp(tree->data,data) < 0){
tree->rght= insertnode(tree->rght,data);
}
else if(strcmp(tree->data,data) > 0){
tree->lft=insertnode(tree->lft,data);
}
return tree;
}
void inorder(node*tree){
if(!tree)return;
inorder(tree->lft);
printf("%s\n",tree->data);
inorder(tree->rght);
}
int countCharacters(node *tree){
if(tree == NULL)return 0;
return strlen(tree->data) +
countCharacters(tree->lft) +
countCharacters(tree->rght);
}
int height(node * tree){
if(tree == NULL)return 0;
int lheight = height(tree->lft);
int rheight = height(tree->rght);
if(lheight > rheight) return lheight + 1;
return rheight+1;
}
int search(node*tree , char name[]){
if(tree == NULL) return 0;
if(strcmp(tree->data , name) == 0)return 1;
if(strcmp(tree->data , name) < 0) return
search(tree->rght , name);
return search(tree->lft , name);
}
int main(){
FILE* input;
input = fopen("in.txt","r");
char name[10];
node*bst = NULL;
int n ,i;
fscanf(input , "%d" ,&n);
for(i=0;i<n;i++){
fscanf(input , "%s" ,name);
bst = insertnode(bst , name);
}
printf("In-order traversal\n");
inorder(bst);
printf("\nThe character count in the tree :
%d\n",countCharacters(bst));
printf("Enter name to search : ");
scanf("%s",name);
if(search(bst , name)){
printf("%s found in
tree\n",name);
}
else{
printf("%s not found in
tree\n",name);
}
printf("Height of tree : %d\n",height(bst));
fclose(input);
return 0;
}
//sample output
(in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...
I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...
Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...
Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{ int day; int month; int year; }; /** This structure is to store the size of the box and it...