Question

In: Computer Science

How to read and print all contents in a binary file using a Binary Search Tree...

How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information.

An example would be a sports record binary file that consist of name of athlete, height , weight, championships won.

Athlete: Michael Jordan

Height: 6’6”

Weight : 205 lbs

Championships won: 6

===================

Athlete: LeBron James

Height: 6’8”

Weight: 250 lbs

Championships won: 4

Solutions

Expert Solution

To traverse a binary search tree in C with the following node structure Tnode and Athelete structure

C Code

#include<stdio.h>
#include<string.h>
struct Athlete
{
   char name[30];
   float weight;
   int ft,inch,won;  
};
struct Tnode
{
   struct Tnode *left;
   struct Tnode *right;
   struct Athlete data;
};

//inorder traversal of a BST
void inOrder(struct Tnode * root)
{
   //base case when root is NULL
   if(root==NULL)
       return;
   //GO LEFT
   inOrder(root->left);
   //print the athlete information
   printf("Athlete: %s\n",root->data.name);
   printf("Height: %d'' %d''''\n",root->data.ft,root->data.inch);
   printf("Weight: %.2f lbs\n",root->data.weight);
   printf("Championships won: %d \n",root->data.won);
   //go right
   inOrder(root->right);
}

//search an element in BST using athlete name
void search(struct Tnode * root,char name[30])
{
   //base case when root is NULL
   if(root==NULL)
       return;
   //GO LEFT
   inOrder(root->left);
   //print the athlete information if name matches
   if(strcmp(name,root->data.name)==0)
   {
   printf("Athlete: %s\n",root->data.name);
   printf("Height: %d'' %d''''\n",root->data.ft,root->data.inch);
   printf("Weight: %.2f lbs\n",root->data.weight);
   printf("Championships won: %d \n",root->data.won);
}
   //go right
   inOrder(root->right);
}



Related Solutions

1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree...
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree is a data structure that consists of JavaScript objects called "nodes". A tree always has a root node which holds its own integer value property and can have up to two child nodes (or leaf nodes), a left and right property. A leaf node holds a value attribute and, likewise, a left and right attribute each potentially pointing to another node in the binary...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
Using Node.js, show how you would read a file and and write out its contents to...
Using Node.js, show how you would read a file and and write out its contents to a new file in reverse order. As an example, if the source file had: Roses are red, Violets are blue. If the Trump administration continues to lose cabinet members, I will not be sad. Boo hoo hoo. The destination file would have: I will not be sad. Boo hoo hoo. If the Trump administration continues to lose cabinet members, Violets are blue. Roses are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT