When I run this Code to get the Length of a Binary tree I get a Segmentation Fault: 11 Error.
I've tried correcting it and the only way I can get it to run is by calling the size function just for the left or right nodes. When I run it this way (which according to me is correct) I get the error.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
struct node {
int data;
struct node* left;
struct node* right;
};
typedef struct node node;
node* newNode( int data ){
node* node = malloc( sizeof(node) );
assert(node);
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
node* insert( node* node, int data ) {
if( node == NULL){
return newNode(data);
}
else{
if( data <= node->data ){
node->left = insert(node->left, data);
}
else{
node->right = insert(node->right,data);
}
}
return node;
}
node* buildOneTwoThree() {
node* root = newNode(2);
root->left = newNode(1);
root->right = newNode(5);
return root;
}
int size( node* tree ) {
int n = 0;
if( tree == NULL ){
return 0;
} else {
return size(tree->left) + 1 + size(tree->right);
}
}
int main(){
node* tree = NULL;
tree = buildOneTwoThree();
printf("size = %i \n", size(tree)+size(tree->right) );
return 0;
}
change
node* node = malloc( sizeof(node) );//<<-sizeof(node) : It has been interpreted as a variable name, not the type.
to
node* node = malloc( sizeof(struct node) );
Related
My program is not giving the desired output. Pls check. What I am doing is, I am first building the queue and then printing every node in the order mentioned in the queue.
The desired output should be F D J B E G K A C I H. The output I am getting is F D J B A C.
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node *left;
struct node *right;
};
struct queue
{
struct node *root;
struct queue *next;
};
struct queue *head;
struct node* newnode(char c)
{
struct node *n = (struct node*)malloc(sizeof(struct node));
n->data = c;
n->left = NULL;
n->right = NULL;
return n;
}
void enqueue(struct node *tree)
{
if(head == NULL)
{
struct queue *n = (struct queue*)malloc(sizeof(struct queue));
n->root = tree;
n->next = NULL;
head = n;
}
else
{
struct queue *p = head;
while(p->next != NULL)
{
p = p->next;
}
struct queue *n = (struct queue*)malloc(sizeof(struct queue));
n->root = tree;
n->next = NULL;
p->next = n;
}
}
void traverse(struct node *tree)
{
if(tree != NULL)
{
enqueue(tree->left);
enqueue(tree->right);
traverse(tree->left);
traverse(tree->right);
}
}
void display()
{
struct queue *p = head;
while(p->next != NULL)
{
printf("%c ",p->root->data);
p = p->next;
}
printf("%c \n",p->root->data);
}
int main()
{
struct node *root = newnode('F');
root->left = newnode('D');
root->right = newnode('J');
root->left->left = newnode('B');
root->left->right = newnode('E');
root->right->left = newnode('G');
root->right->right = newnode('K');
root->left->left->left = newnode('A');
root->left->left->right = newnode('C');
root->right->left->left = newnode('I');
root->right->left->left->right = newnode('H');
enqueue(root);
traverse(root);
display();
return 0;
}
Several issues in the traverse function:
enqueue(tree->left) will eventually add NULL to the queue, and this will cause problems in the display function, where it is expected that all p have a non-NULL p->root member.
It doesn't perform a level by level traversal. It is more like a depth-first traversal (after traversing direct siblings). In a level order traversal, there is no recursion. You should iterate the queue (lagging behind as it grows), to fetch values for tree.
Here is a correction for traverse:
void traverse(struct node *tree)
{
struct queue *current = head;
while (current != NULL)
{
tree = current->root;
if (tree->left != NULL) enqueue(tree->left);
if (tree->right != NULL) enqueue(tree->right);
current = current->next; // Walk along the queue while it is being built
}
}
#include <stdio.h>
#include <stdlib.h>
// A Tree node
typedef struct leaf
{
char ch;
int freq;
struct leaf *left, *right;
}leaf;
typedef struct node{
int data;
leaf* leaf_data;
struct node *ptr;
} node;
//----- LINKED LIST -----
struct LinkedList {
node* head;
//node* tail;
};
typedef struct LinkedList LinkedList_s;
leaf* createLeaf(char ch, int freq)
{
leaf* node = malloc(sizeof(leaf));
node->ch = ch;
node->freq = freq;
node->left = NULL;
node->right= NULL;
return node;
}
//----- CREATE LINKED LIST -----
LinkedList_s createSortedList()
{
LinkedList_s list = {NULL};
return list;
}
node* createStackNode(leaf* l){
node* temp = (node*) malloc(sizeof(node)) ;
temp->data = (int) l->freq;
temp->leaf_data = l;
return temp;
}
void insert(LinkedList_s* head, node* l) {
if(head->head == NULL){
head->head = l;
}
else if (head->head->data > l->data){
l->ptr = head->head;
head->head = l;
}
else{
node *prev = (node*)malloc(sizeof(node));
node *next = (node*)malloc(sizeof(node));
int i = 0;
prev = NULL;
next = head->head;
while(next != NULL && i == 0){
if (next->data >= l->data){
prev->ptr = l;
l->ptr = next;
i = 1;
}
prev = next;
next = next->ptr;
}
if (next == NULL){
prev->ptr = l;
l->ptr = next;
}
}
}
node* dequeue(LinkedList_s *list){
node* temp = (node*)malloc(sizeof(node));
temp = list->head;
list->head = list->head->ptr;
return temp;
}
void insertLeaf(LinkedList_s *list, leaf* l){
node* n = createStackNode(l);
//printf("%d", n->data);
insert(list, n);
}
basically I'm building a huffman encoder for a class project and I'm trying to create a sorted list/priority queue to store BTS leaf node in order of frequency. I'm having problems with createStackNode. If I change the node struct int data to int* data I don't get a seg. fault, but this way it randomly seg. faults and won't sort. Any ideas would be much appreciated!
So I call insertLeft from a separate file
#include <stdio.h>
#include <stdlib.h>
#include "Sorted_List.h"
#include "linked_counter.h"
int main(void){
//Make a Frequency List
LinkedList_t* x = malloc(sizeof(LinkedList_t));
x = makeList();
//Create a Sorted List/Priority Queue
struct LinkedList_s* list = malloc(sizeof(LinkedList_s));
*list = createSortedList();
leaf* l = malloc(sizeof(leaf));
//Current node for traversal
Node_t* current = malloc(sizeof(Node_t));
current= x->head;
while(current != NULL){
l = createLeaf(current->data, current->count);
printf("%c %d ->",l->ch, l->freq);
insertLeaf(list, l);
current= current->next;
}
//for(int i = 0; i < 5; i++)
//printf("%c~~\n", list->head->leaf_data->ch);
}
at insert leaf it gives the fault
I am getting a segmentation fault when trying to print the nodes in my binary tree. It looks to be an issue with the third node. I have searched google and stack overflow for hours but I can not understand what the problem is. I am trying to teach myself data structures in C and am very much a novice so I may be doing something in a frowned upon way.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} Node;
typedef struct
{
Node *root;
} BinarySearchTree;
void printInOrder(Node *);
void addNode(Node *, Node *);
int main (void)
{
BinarySearchTree tree;
BinarySearchTree *tree_ptr = &tree;
Node n1, n2, n3;
n1.data = 1;
n2.data = 2;
n3.data = 3;
Node *n1_ptr = &n1;
Node *n2_ptr = &n2;
Node *n3_ptr = &n3;
tree_ptr->root = n1_ptr;
addNode(tree_ptr->root, n2_ptr);
addNode(tree_ptr->root, n3_ptr);
printInOrder(tree_ptr->root);
}
void printInOrder(Node *root)
{
if (root == NULL)
{
return;
} else
{
printInOrder(root->left);
printf("%i\n", root->data);
printInOrder(root->right);
}
}
void addNode(Node *root, Node *node)
{
if (node->data < root->data)
{
if (root->left == NULL)
{
root->left = node;
} else
{
addNode(root->left, node);
}
}
else if (node->data > root->data)
{
if (root->right == NULL)
{
root->right = node;
} else
{
addNode(root->right, node);
}
}
}
output:
1
2
Segmentation fault: 11
There doesn't seem to be an issue with any but the third node. If I comment out the line that adds the second node I get the same error (with only 1 being printed, obviously).
Your initialization is incomplete
n1.data = 1;
n2.data = 2;
n3.data = 3;
should also set the pointers
n1.data = 1;
n1.left = NULL;
n1.right = NULL;
n2.data = 2;
n2.left = NULL;
n2.right = NULL;
n3.data = 3;
n3.left = NULL;
n3.right = NULL;
Problem is occurring because you are not initializing all the member of structure Node type variable.
I would suggest, you should write a function to initialize the Node type variable, like this:
void init_node(Node * nodeptr, int data)
{
nodeptr->data = data;
nodeptr->left = NULL;
nodeptr->right = NULL;
}
and in your main() (or from where ever you want to initialize) you can simply do:
init_node(&n1, 1);
init_node(&n2, 2);
init_node(&n3, 3);
With this, you will never miss assigning NULL to left and right pointer during initialization of Node type variable and chances of the error occurring because of it will be reduced to a greater extent.
I found this on Internet to reverse a list using recursion and applied it in codeblocks but the output only reverse prints last two Insert call from main function. It skips the first three Insert calls. Why? I did search for this problem here but I failed to understand them as I'm a beginner. Kindly help
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
struct Node* Insert (struct Node* head, int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
struct Node* head = NULL;
head = Insert(head,2);
head = Insert(head,7);
head = Insert(head,3);
head = Insert(head,1);
head = Insert(head,4);
reversePrint(head);
return 0;
}
O/P : 4 1
NOTES:
Don't cast the return of value of malloc
You declared two *head and confused yourself
No need to pass pointer to function and return pointer when you have head declared as global. Which is not a good idea but I followed your code.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
void Insert (int data)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
Insert(2);
Insert(7);
Insert(3);
Insert(1);
Insert(4);
reversePrint(head);
return 0;
}
OUTPUT:
4 1 3 7 2
In this code, I need create a function which will sum even numbers in my BST. I don't know how to pass all nodes of my tree. Here is the code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, int key) {
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
void main(void)
{
struct node *root = NULL;
int data[]={3,1,2,6,4,7,8}, n, i;
n=sizeof(data)/sizeof(data[0]);
for(i=0;i<n;i++)
root=insert(root,data[i]);
}
Try something like this. Sorry, have no C-compiler at hand.
int sum(struct node *root) {
if (root == NULL) {
return 0;
}
int value = 0;
if (root->key % 2 == 0) {
value = root->key;
}
return value + sum(root->left) + sum(root->right);
}
You need to do one of the different search, BFS or DFS, for search in every node of the tree. Also in every moment you should save the actual sum of even numbers. The comparison should be like
if(node->key % 2 == 0){
final += node->key;
}
Hope this help.