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.
Related
I am trying to implement the insert operation of a binary search tree using C. Why does the following code show a Segmentation fault when trying to print the value of the left and right nodes of the root?
Please explain what caused this error exactly.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node *root, *temp = NULL;
void insert(int data) {
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
if (root == NULL){
// if tree is empty insert the node as root
root = newNode;
}else {
// if the tree is not empty
temp = root;
while(temp != NULL) {
if(data <= root->data) {
temp = temp->left;
}
if(data > root->data) {
temp = temp->right;
}
}
temp = newNode;
}
}
int main() {
insert(7);
insert(4);
insert(8);
printf("\n\n------%d------", root->left->data);
printf("\n\n------%d------", root->right->data);
return 0;
}
The assignment temp = newNode only stores a pointer in the temp variable, not somewhere in the tree. So your tree's root node will never get any child. By consequence the main program is dereferencing a root->left pointer that is NULL, and this explains the error you get.
In order to really attach the new node at the right place in the tree, you need to modify a left or right member of some node. You can do this in several ways. One is to make temp a pointer-pointer, so that it will have the address of a left or right member. Then the assignment to *temp, will be an assignment to a node's left or right member, effectively extending the tree with that new node.
Here is the updated part of the code:
struct node **temp = &root;
while(*temp != NULL) {
if(data <= root->data) {
temp = &(*temp)->left;
}
if(data > root->data) {
temp = &(*temp)->right;
}
}
*temp = newNode;
okay you have a few issues here,
first of all, temp will point back to newNode, as you have not copied newNode's values to where temp is pointing now.
second and no less important, newNode is created within the scope of insert() - therefore, root will always remain null, as root points after execution of insert() to data wich no longer exists.
I have improved your code, and this definitely works as expected
hope it has helped.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* left;
struct node* right;
}node;
node *root=NULL;
void insert(int data) {
node *temp = NULL;
if (root == NULL){
/* if tree is empty insert the node as root*/
root = malloc(sizeof(struct node));
root->data=data;
}else {
/*if the tree is not empty*/
temp = root;
while(temp->left!= NULL||temp->right!=NULL) {
if(data <= temp->data){
if(temp->left==NULL)
break;
else
temp = temp->left;
}
else if(data > temp->data){
if(temp->right==NULL)
break;
else
temp = temp->right;
}
}
if(data<= temp->data){
temp->left=malloc(sizeof(struct node));
(temp->left)->data=data;
}
else {
temp->right=malloc(sizeof(struct node));
(temp->right)->data=data;
}
/*temp=newNode;*/
}
}
int main() {
insert(7);
insert(4);
insert(8);
printf("\n\n------%d------", root->left->data);
printf("\n\n------%d------", root->right->data);
return 0;
}
I was trying to create a BST in C. I was just adding bare basic functionalities. However, I have seemed to run into a problem either adding a node or with the in-order traversal - somehow created an infinite loop. Please offer feedback as I am trying to improve. Thanks!
#include <stdio.h>
#include <stdlib.h>
//node structure
struct node{
int data;
struct node* left;
struct node* right;
}typedef node;
//create node
node * createLeaf(int x){
node * temp = (node*)malloc(sizeof(node));
temp->data = x;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//insert node
node *insert(node *root,int x){
if(root == NULL){
root = createLeaf(x);
return root;
}
else{
if(x > root->data){
root->right = insert(root->right,x);
}
else if(x < root->data){
root->left = insert(root->left,x);
}
}
return root;
}
//in-order traversal
void inorder(node * root){
while(root!=NULL){
inorder(root->left);
printf("%d\n",root->data);
inorder(root->right);
}
}
int main()
{
node * root = NULL;
root = insert(root,5);
insert(root,8);
insert(root,1);
inorder(root);
printf("Hello World");
return 0;
}
What #Eugene suggested replacing while with if will fix the problem
Or you can add a break at the end of while loop and it should also solve your problem.
But I will suggest to only use either recursion or iteration.
I have previously posted about this same topic. I am self-learning data structures using MIT Open Courseware. I'm doing the 6.S096-Introduction to C/C++ course and attempting the fourth assignment.
It is based on binary search trees and I gave it a try. I wanted to print the values for debugging but kept getting different executions each time.
One time, the cycle doesn't complete and the other time, it goes on to infinity. The debugging block also relates to the other function(find_node_data) I have to complete. So if I can figure what's wrong here, I can easily finish the find_node_data. I have commented a few things to see if it affects anything. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int node_id;
int data;
struct node* left;
struct node* right;
}node;
///*** DO NOT CHANGE ANY FUNCTION DEFINITIONS ***///
// Declare the tree modification functions below...
node* newNode(int data,int node_id){
node* new_node = (node*) malloc(sizeof(node));
new_node->data = data;
new_node->node_id= node_id;
new_node->right= new_node->left=NULL;
return new_node;
}
node* insert_node(node* root, int node_id, int data) {
if(root==NULL)
return newNode(data,node_id);
else{
node* cur;
if(node_id<root->node_id){
cur=insert_node(root->left,data,node_id);
root->left=cur;
}
else if(node_id>root->node_id){
cur=insert_node(root->right,data,node_id);
root->right=cur;
}
}
return root;
}
// Find the node with node_id, and return its data
/*int find_node_data(node* root, int node_id) {
node* current;
for( current = root->; current->next!=NULL;
current= current->next){
if(current->data == data) return current;
}
return NULL;
}
*/
int main() {
/*
Insert your test code here. Try inserting nodes then searching for them.
When we grade, we will overwrite your main function with our own sequence of
insertions and deletions to test your implementation. If you change the
argument or return types of the binary tree functions, our grading code
won't work!
*/
int T,data,node_id;
printf("Print yo cases");
scanf("%d", &T);
node* root = NULL;
while(T-->0){
printf("Type yo numnums no. %d:",T);
scanf("%d %d",&data,&node_id);
root=insert_node(root,data,node_id);
}
node *lol;
node *king;
for(lol=root;lol->left!=NULL;lol=lol->left){
//for(king=root;king->right!=NULL;king=king->right){
printf("executed!\n");
printf("%d ",lol->node_id);//,king->node_id);
//}
}
return 0;
}
To find the node_data you can use recursion to find the node.
node* find_node_data(node *root, int node_id) {
if (root == NULL)
return NULL;
else if (root->node_id == node_id)
return root;
else {
node *left = find_node_data(root->left, node_id);
return left? left: find_node_data(root->right, node_id);
}
}
And then get the data for the node e.g. get the data for node with node_id 42:
printf("node data %d", find_node_data(root, 42)->data);
Full program below (I can't guarantee its correctness but maybe you can?)
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int node_id;
int data;
struct node *left;
struct node *right;
} node;
///*** DO NOT CHANGE ANY FUNCTION DEFINITIONS ***///
// Declare the tree modification functions below...
node *newNode(int data, int node_id) {
node *new_node = (node *) malloc(sizeof(node));
new_node->data = data;
new_node->node_id = node_id;
new_node->right = new_node->left = NULL;
return new_node;
}
node *insert_node(node *root, int data, int node_id) {
if (root == NULL)
return newNode(data, node_id);
else {
node *cur;
if (node_id < root->node_id) {
cur = insert_node(root->left, data, node_id);
root->left = cur;
}
else if (node_id > root->node_id) {
cur = insert_node(root->right, data, node_id);
root->right = cur;
}
}
return root;
}
// Find the node with node_id, and return its data
/*
int find_node_data_old(node *root, int node_id) {
node *current;
for (current = root->; current->next != NULL;
current = current->next) {
if (current->data == data) return current;
}
return NULL;
}*/
node* find_node_data(node *root, int node_id) {
if (root == NULL)
return NULL;
else if (root->node_id == node_id)
return root;
else {
node *left = find_node_data(root->left, node_id);
return left? left: find_node_data(root->right, node_id);
}
}
void print(node *np) {
if (np) {
print(np->left);
printf("(%d, %d)", np->node_id, np->data);
print(np->right);
}
}
int main() {
/*
Insert your test code here. Try inserting nodes then searching for them.
When we grade, we will overwrite your main function with our own sequence of
insertions and deletions to test your implementation. If you change the
argument or return types of the binary tree functions, our grading code
won't work!
*/
int T, data, node_id;
printf("Print yo cases");
scanf("%d", &T);
node *root = NULL;
while (T-- > 0) {
printf("Type yo numnums no. %d:", T);
scanf("%d %d", &data, &node_id);
root = insert_node(root, data, node_id);
}
node *lol;
node *king;
for (lol = root; lol->left != NULL; lol = lol->left) {
//for(king=root;king->right!=NULL;king=king->right){
printf("executed!\n");
printf("%d ", lol->node_id);//,king->node_id);
//}
}
print(root);
printf("\n");
printf("node data %d", find_node_data(root, 42)->data);
return 0;
}
Test
Print yo cases3
Type yo numnums no. 2:22 42
Type yo numnums no. 1:21 41
Type yo numnums no. 0:20 40
executed!
42 executed!
41 (40, 20)(41, 21)(42, 22)
node data 22
You may also use Jonathan Leffler's improved recursion to find the node:
node *find_node_data2(node *root, int node_id) {
if (root == NULL)
return NULL;
else if (root->node_id == node_id)
return root;
else if (root->node_id > node_id)
return find_node_data(root->left, node_id);
else
return find_node_data(root->right, node_id);
}
Both functions return the correct values as seen in the second test.
int main() {
/*
Insert your test code here. Try inserting nodes then searching for them.
When we grade, we will overwrite your main function with our own sequence of
insertions and deletions to test your implementation. If you change the
argument or return types of the binary tree functions, our grading code
won't work!
*/
int T, data, node_id;
printf("Print yo cases");
scanf("%d", &T);
node *root = NULL;
while (T-- > 0) {
printf("Type yo numnums no. %d:", T);
scanf("%d %d", &data, &node_id);
root = insert_node(root, data, node_id);
}
node *lol;
node *king;
for (lol = root; lol->left != NULL; lol = lol->left) {
//for(king=root;king->right!=NULL;king=king->right){
printf("executed!\n");
printf("%d ", lol->node_id);//,king->node_id);
//}
}
print(root);
printf("\n");
printf("node data %d\n", find_node_data(root, 42)->data);
printf("node data find_node_data2 %d", find_node_data2(root, 42)->data);
return 0;
}
Test 2
Print yo cases3
Type yo numnums no. 2:11 12
Type yo numnums no. 1:13 14
Type yo numnums no. 0:20 42
(12, 11)(14, 13)(42, 20)
node data 20
node data find_node_data2 20
I am trying to make a BST from a given array and then traverse it In-Order. There are no compilation errors but at run time the error is Segmentation fault (core dumped). I have been unable to figure out the cause of the error(tried to browse stackoverflow for similar cases). The code is as follows:
#include<stdio.h>
#include<stdlib.h>
struct TreeNode{
int data;
struct TreeNode *right;
struct TreeNode *left;
};
struct TreeNode *newTreeNode(){
struct TreeNode *node = malloc(sizeof(struct TreeNode));
if(node){
node->data = 0;
node->left = NULL;
node->right = NULL;
return node;
}
else{
printf("Memory Error");
}
};
void InorderTraversalRecursive(struct TreeNode *node){
InorderTraversalRecursive(node->left);
printf("%d",node->data);
InorderTraversalRecursive(node->right);
}
struct TreeNode *InsertIntoTree(struct TreeNode *node, int data){
if(node == NULL){
node = newTreeNode();
node->data = data;
node->left = node->right = NULL;
}
else{
if(data > node->data)
InsertIntoTree(node->right, data);
else if(data < node->data)
InsertIntoTree(node->left, data);
}
return node;
}
struct TreeNode *MakeTreeFromData(int *arr){
int size = sizeof(arr)/sizeof(arr[0]);
struct TreeNode *root;
root = newTreeNode();
int i;
for(i=0;i<size;i++){
InsertIntoTree(root,arr[i]);
}
return root;
}
void main(){
int data[] = {4,2,6,1,7,3,5,8};
struct TreeNode *root;
root = MakeTreeFromData(data);
InorderTraversalRecursive(root);
}
One more error is in function InorderTraversalRecursive. The code should be:
void InorderTraversalRecursive(struct TreeNode *node)
{
if(!node)
return;
InorderTraversalRecursive(node->left);
printf("%d",node->data);
InorderTraversalRecursive(node->right);
}
This would eliminate the segmentation fault.
You should pass the size of array in function MakeTreeFromData().
So the declaration becomes
struct TreeNode *MakeTreeFromData(int *arr,int size)
where size is calculated by the same formula but inside main and then passed to the function.
Moreover, the function InsertIntoTree() should be defined as
struct TreeNode *InsertIntoTree(struct TreeNode *node, int data)
{
if(node==NULL)
{
node = newTreeNode();
node->data = data;
node->left = node->right = NULL;
}
else
{
if(data >= node->data)
node->right = InsertIntoTree(node->right, data);
else
node->left = InsertIntoTree(node->left, data);
}
return node;
}
And voila you get the correct output!!!
One obvious error is in this line
int size = sizeof(arr)/sizeof(arr[0]);
where arr is not an array but a pointer. This doesn't do at all what you think it is doing. You'd have to pass the size of your array as a parameter to your function.
If you have one powerful tool to know about when coding in C is GDB.
Read some tutorials and you won't need to ask this questions.
In my personal experience, I loved the gdb experience with emacs.
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) );