Starting from:

$30

COP3502C-Assignment 2 Binary Search Tree

Introduction: For this  you have to write a c program that will utilize the Binary Search Tree (BST).

Problem
In this, you have to read a text file (in.txt) that contains a set of words. The first line of the file contains 3 numbers (N, S, D). These numbers represent the sequence of input words in your file.

N:  represents the number of words to read to build binary search tree. You have to write a recursive insert code to create and insert these words into the binary search tree. After inserting all the items, you should show the words in Pre order, In order, and Post order. So, you need to create three functions for this purpose.

S: represents the number of the words to search from the tree. These S words are placed after the first N words in the input file.You need to implement a search function that will be able to search these words in your BST. 

Additionally, the search words will also be used to look in the binary search tree and count number of words in the tree that come before the search word, alphabetically. You will need to create a recursive function with the following prototype CountBefore(treeNode* root, char searchKey[]), where treeNode is the node structure of your tree.

D: represents the number of words to be deleted from the BST. This list of words are placed after N+S words in the input file. Write a recursive delete function for your task. After deleting all the items, also show the tree in only in-order traversal.

Other required functions: In addition to the general functions for the above tasks, your code should also contain the following functions.

totalCharacters(treeNode* root): This function takes the root of the tree in parameter and returns the total number of characters in the whole Binary Search Tree. You need to call this function after creating the tree and also at the end of deleting the requested nodes from the tree.

isBalanced(treeNode* root): We call the tree as imbalanced if the differences between the height of left subtree and height of right subtree is more than one. This function takes the root of the tree in parameter and returns 1 if the tree is balanced, otherwise it returns 0. In this process,  you might consider implementing a height function to get the height of a subtree. The isBalanced function should be called after creating the tree and also at the end of deleting the requested nodes from the tree.

 

Example:

The figure below shows an example of BST with strings.

 

 

All the output must be written to the out.txt file according the sample output shown bellow.  You may output in the console

 

 

 

 

Example ample Input file in.txt: 

  9 3 4 judy   mary bill   alice tom   fred jane
           joe

dave
 

jane   jones judy    alice mary

  judy fred
 

Sample Output file out.txt: 

Pre Order: judy bill alice fred dave jane joe mary tom

In Order: alice bill dave fred jane joe judy mary tom

Post Order: alice dave joe jane fred bill tom mary judy

Total characters: 35

Height left= 3, height right = 1. The tree is imbalanced!

Search Phase: jane: Found, Items before: 4  jones: Not found, Items before: 0 judy: Found, Items before: 6  Delete Phase: alice: deleted mary: deleted judy: deleted fred: deleted

In Order: bill dave jane joe tom
Requirement:

You must use file IO, Binary Search Tree and relevant traversal for your solution. All the information must be extracted from the tree.

Your program must have to compile in EUSTIS to get credit.

More products