Question

Binary trees - C programming; Write a program in C, you must have as input a...

Binary trees - C programming;

Write a program in C, you must have as input a .txt file (start.txt), and have as output answering questions about the tree. Edges as pairs (x, y), x and y refer to the names of the nodes
Input (start.txt):
(1,2)
(1,3)
(2,4)
(2,5)
(3,6)
(3,7)
(4,8)

Output(output.txt):


Is the tree complete? NO
What is the weight of the tree? 4
Is it balanced? YES

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

struct node
{
int value;
struct node *right;
struct node *left;
}mynode;

int max(int a, int b)
{
return (a >= b) ? a : b;
}


struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->value = data;
node->left = NULL;
node->right = NULL;

return (node);
}


void putNode(struct node* node, int data1, int data2)
{
if (node == NULL)
return;

if(node->value == data1){
if(node->left!=NULL){
node->right = newNode(data2);
}else{
node->left = newNode(data2);
}
return;
}

putNode(node->left,data1,data2);
putNode(node->right,data1,data2);
}

int weight(struct node* node)
{
   if(node==NULL)
       return 0;

   else
   {
       int leftb=weight(node->left);
       int rightb=weight(node->right);
       return max(leftb,rightb)+1;
   }
}

int isComplete (struct node * node)
{


if (node == NULL) return TRUE;

int ld=weight(node->left);
int rd=weight(node->right);

return(ld==rd && isComplete(node->left) && isComplete(node->right));

}


int isBalanced(struct node* node) {
if (node == NULL) {
return TRUE;
}
int left = weight(node->left);
int right = weight(node->right);
return abs(left - right) <= 1 && isBalanced(node->left) && isBalanced(node->right);
}

int main()
{
FILE *fp;
char fname[50] = "start.txt";


fp = fopen(fname, "r");

if(fp == NULL)
{
printf("Can't open file\n");
}
int arr1[200]={0},arr2[200]={0};
char c = fgetc(fp);
int p1=0,p2=0;
while (c != EOF)
{
c = fgetc(fp);
arr1[p1++] = (int)(c)-48;
c = fgetc(fp);
c = fgetc(fp);
arr2[p2++] = (int)(c)-48;
c =fgetc(fp);
c =fgetc(fp);
c =fgetc(fp);

}
struct node* root = newNode(arr1[0]);
for(int i = 0 ; i < p1; i++){
putNode(root,arr1[i],arr2[i]);
}
FILE *fo = fopen("output.txt", "w");
if (fo == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int complete = isComplete(root);
if(complete){
const char *text = "YES";
fprintf(fo, "Is the tree complete? %s\n", text);
}else{
const char *text = "NO";
fprintf(fo, "Is the tree complete? %s\n", text);
}

int w = weight(root);
fprintf(fo, "What is the weight of the tree? %d\n", w);

int b = isBalanced(root);
if(b){
const char *text = "YES";
fprintf(fo, "Is it balanced? %s\n", text);
}else{
const char *text = "NO";
fprintf(fo, "Is it balanced? %s\n", text);
}

fclose(fp);
fclose(fo);


return 0;
}

Add a comment
Know the answer?
Add Answer to:
Binary trees - C programming; Write a program in C, you must have as input a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Binary trees - C programming; Write a program in C, you must have as input a...

    Binary trees - C programming; Write a program in C, you must have as input a .txt file (start.txt) which represents a binary tree, Edges as pairs (x, y), x and y refer to the names of the nodes , and have as output answering questions about the tree.EX Input (start.txt): (1,2) (1,3) (2,4) (2,5) (3,6) (3,7) (4,8) Output(output.txt): Is the tree complete? YES What is the weight of the tree? 4 Is it balanced? YES

  • IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...

    IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of...

    Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or *. The tree represents an arithmetic expression where the value at a non-leaf...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binar...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are n...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT