handle head of binary search tree in nested structure - c

i have to create binary search tree. so i have used nested structure. i am not able to maintain head of the tree. when i give inputs then it is creating new node of every input. Help me with how to handle Head.
tree.h
#ifndef __TREE__H__
#define __TREE__H__
typedef struct _BSTNode
{
int data;
struct _BSTNode *left,*right;
}BSTNode;
typedef struct _BST
{
BSTNode *tHead;
}BST;
int insertBST(BST ,int);
#endif
mainTree.c
#include"tree.h"
#include<stdio.h>
int main()
{
FILE *fp;
BST bst;
bst.tHead=NULL;
int element;
fp = fopen("input","r");
while(fscanf(fp,"%d",&element)!=EOF)
{
insertBST(bst,element);
}
return 0;
}
insertBST.c
#include"tree.h"
#include<stdio.h>
int insertBST(BST bst,int element)
{
return _insertBST(bst.tHead,NULL,element);
}
int _insertBST(BSTNode *p,BSTNode *parent,int e)
{
if(p==NULL)
return createBST(p,e);
if(p->data == e)
return 0;
if(p->data < e)
return _insertBST(p->right,p,e);
if(p->data > e)
return _insertBST(p->left,p,e);
return -1;
}
createBST.c
#include"tree.h"
#include<stdlib.h>
#include<stdio.h>
int createBST(BSTNode *p,int element)
{
BSTNode *node = (BSTNode *)malloc(sizeof(BSTNode));
node->data = element;
node->left = NULL;
node->right = NULL;
if(p== NULL)
{
p=node;
fprintf(stdout,"Im here 1");
return 0;
}
else
{
if(p->data < node ->data)
(p->left) = node;
fprintf(stdout,"Im here 2");
return 0;
if(p->data > node ->data)
(p->right) = node;
fprintf(stdout,"Im here 3");
return 0;
}
}
output:-
#:~/Ds/tree$gcc mainTree.c insertBST.c createBST.c
#:~/Ds/tree$ ./a.out
Im here 1Im here 1Im here 1Im here 1Im here 1
input :- 40 10 30 20

Related

I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function

Below is the program
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node* reverse_ll(struct node* hnode)
{
if(hnode == 0)
{
return 0;
}
if(hnode->next == 0)
{
head=hnode;
return hnode;
}
struct node* ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next=0;
//return hnode;
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==0)
{
printf("empty");
}
else
{
while(ptr!=0)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("null");
}
}
int main()
{
struct node* h;
lastinsert(1);
lastinsert(2);
lastinsert(3);
lastinsert(4);
lastinsert(5);
display();
h=reverse_ll(head);
display();
return 0;
}
In function reverse_ll() even if I comment "return hnode" I am getting the right output How is it possible where does ptr receives its address from when I comment "return hnode"?
output: 1->2->3->4->5->null
5->4->3->2->1->null
reverse_ll() must return a struct node * in the recursive case:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void lastinsert(int data) {
struct node **c = &head;
for(; *c; c = &(*c)->next);
*c = malloc(sizeof(*head));
if (!(*c)) {
printf("malloc failed\n");
return;
}
(*c)->data = data;
(*c)->next = NULL;
}
struct node *reverse_ll(struct node* hnode) {
if(!hnode)
return NULL;
if(!hnode->next) {
head = hnode;
return hnode;
}
struct node *ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next = NULL;
return hnode;
}
void display() {
if(!head) {
printf("empty");
return;
}
for(struct node *ptr = head; ptr; ptr = ptr->next) {
printf("%d->",ptr->data);
}
printf("null\n");
}
int main() {
for(int i = 1; i <= 5; i++) {
lastinsert(i);
}
display();
reverse_ll(head);
display();
// It's good practice to implement a function that frees you list
// which you would call here.
return 0;
}
and example run:
$ ./a.out
1->2->3->4->5->null
5->4->3->2->1->null

How can I get a non-empty binary tree and print it?

I built three files, which includes MainFunc.c, AllFun.h, and OtheFunc.c.
The program happens runtime error, which print nothing. However, I need it output a tree as previous order.
I guess the problem may be that I built an empty tree, but I can't solve it.
MainFunc.c
#include "AllFun.h"
int main(int argc, char* argv[])
{
int nums[MaxSize] = { 0 };
printf("Enter value of root node: ");
for (int i = 0; i < MaxSize; ++i) {
scanf_s("%d", &nums[i]);
}
TreeNode* root = create(nums, MaxSize);
preorder(root);
return 0;
}
AllFun.h
#include <stddef.h> // NULL
#include <stdlib.h>
#include <stdio.h>
#define MaxSize 10
typedef struct node {
int data;
struct node* lchild, * rchild;
} TreeNode;
TreeNode *create(int nums[], int n);
void preorder(TreeNode *root);
OtheFunc.c
#include "AllFun.h"
TreeNode* newNode(int v) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
if (node) {
node->data = v;
node->lchild = node->rchild = NULL;
return node;
}
}
void insert(TreeNode* root, int x)
{
if (root == NULL) {
root = newNode(x);
return;
}
if (root->data < x) {
insert(root->lchild, x);
}
else {
insert(root->rchild, x);
}
}
TreeNode *create(int nums[], int n)
{
TreeNode* root = NULL; // Build a empty root node
for (int i = 0; i < n; i++) {
insert(root, nums[i]);
}
return root;
}
void preorder(TreeNode* root)
{
if (root == NULL) {
return;
}
printf("%d ", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
You have to pass the node as a pointer to pointer, then the function can change the pointer:
void insert(TreeNode** root, int x)
{
if (*root == NULL) {
*root = newNode(x);
return;
}
if ((*root)->data < x) {
insert(&(*root)->lchild, x);
}
else {
insert(&(*root)->rchild, x);
}
}
Call with insert(&root, nums[i]);:
https://ideone.com/nV5q1g

cs50 pset5 unload problem - memory leakage

I am trying to solve the cs50 pset5. I am using a tries method.
I can insert word into the tries successfully. However, when i unload the memory, it fails. I am so stuck at the unload problem for a few days.
When I try the unload function, it stated that Erorr Message: double free or corruption (!prev): 0x000000000205d010 ***
It is probably due to my unload function, but i have drawn out the logic. It seems fine to me. Anyone has any idea where to amend?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define LENGTH 45
#define SIZE_OF_CHILD 27
typedef struct Node {
bool is_word;
struct Node* child[SIZE_OF_CHILD];
} Node;
Node* root;
Node* temp;
Node* create_node(Node* node);
void test();
int cal_key(char c);
Node* insert(const char* word);
void unload(Node* node);
int main() {
root = malloc(sizeof(Node));
if (root == NULL) {
exit(0);
}
root = create_node(root);
temp = malloc(sizeof(Node));
if (temp == NULL) {
exit(0);
}
test();
}
// **************** function to create a node *******************
Node* create_node(Node* node) {
node->is_word = false;
for (int i = 0; i < SIZE_OF_CHILD; i++) {
node->child[i] = NULL;
}
return node;
}
//************* calculate the key value ************
// Assume that the input is in lower case
int cal_key(char c) {
if (isalpha(c)) {
if (islower(c)) {
return c - 'a';
}
else {
return c + 32 -'a';
}
}
else {
return SIZE_OF_CHILD - 1;
}
}
//*************** function to insert an item in the the node ***********
Node* insert(const char* word) {
int str_len = strlen(word);
temp = root;
int key;
for (int i = 0; i < str_len; i++) {
key = cal_key(word[i]);
if (temp->child[key] == NULL) {
Node* node = malloc(sizeof(Node));
if (node == NULL) {
fprintf(stderr, "Error in creating node\n");
exit(0);
}
node = create_node(node);
temp->child[key] = node;
}
temp = temp->child[key];
}
temp->is_word = true;
return root;
}
//***************** function to unload a function ********************
void unload(Node* node) {
// This is to find the last node
for (int i = 0; i < SIZE_OF_CHILD; i++) {
if (node->child[i] != NULL) {
unload(node->child[i]);
}
free(node);
}
}
void test() {
root = insert("Peter");
unload(root);
}

why my C structure must add a weird int and it affect the pointer?

this is a binary tree queue problem
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 10
typedef struct _node
{
int value;
struct _node *left;
struct _node *right;
}TNode,*Tree;
add a *next in q_node is my purpose
other wise , we need to add in the Tree node struct
So, for the sake of doesn't modify the struct of tree
I design a q_node struct to include it
we can use define command to make it as a template.
typedef struct _q_node
{
TNode *t_node;
int length;
struct _q_node *next;
}QNode;
typedef struct _Queue
{
QNode *head;
QNode *tail;
}Queue;
Queue* init_queue()
{
Queue *queue=(Queue*)malloc(sizeof(Queue));
queue->head = queue->tail = NULL;
return queue;
}
int enQueue(Queue *pQueue,TNode *pTNode)
{
QNode *pQNode = (QNode *)malloc(sizeof(QNode));
pQNode->t_node = pTNode;
if(pQueue->head == NULL)
{//when it's empty
pQueue->head = pQNode;
pQueue->tail = pQNode;
}
else
{
pQueue->tail->next = pQNode;
pQueue->tail = pQNode;
}
}
QNode* deQueue(Queue *pQueue)
{
if(pQueue->head == NULL)
{
return NULL;
}
QNode *deNode= pQueue->head;
pQueue->head = pQueue->head->next;
return deNode;
}
TNode* init_TNode(int value)
{
TNode *new_node = (TNode*)malloc(sizeof(TNode));
new_node->value=value;
new_node->left = new_node->right = NULL;
return new_node;
}
//0:empty
int ifEmpty(Queue *pQueue)
{
if(pQueue->head == NULL)
{
//printf("empty tree\n");
return 0;
}
//printf("queue is not empty\n");
return 1;
}
int insert_tree(Tree pTree,int pValue)
{
//found NULL sub tree, then add to his father->left
if(!pTree)
{
return 0;
}
TNode *tNode = init_TNode(pValue);
if(tNode==NULL)
{
printf("create TNode error!\n");
return 0;
}
if(pValue < pTree->value)
if(insert_tree(pTree->left,pValue)==0)
{
//no left child any more,set a new left child to pTree
pTree->left = tNode;
printf("insert :%d\n",pValue);
}
if(pValue > pTree->value)
if(insert_tree(pTree->right,pValue)==0)
{
pTree->right = tNode;
printf("insert :%d\n",pValue);
}
}
Tree creatTree()
{
srand(time(NULL));
Tree root = init_TNode(rand()%100);
printf("root is %d\n",root->value);
int i ;
for(i=1;i<NUM;i++)
{
insert_tree(root,rand()%100);
}
printf("creat tree succuess!Tree heigh is:%d\n",get_tree_height(root));
return root ;
}
int get_tree_height(Tree pRoot)
{
if(!pRoot)
{
return 0;
}
int lh=0,rh=0;
lh = get_tree_height(pRoot->left);
rh = get_tree_height(pRoot->right);
return (lh<rh)?(rh+1):(lh+1);
}
int breath_travel(Tree pRoot,Queue *pQueue)
{
if(!pRoot)
{
return 0;
}
enQueue(pQueue,pRoot);
printf("_______________________\n");
printf("breath begin,enter root:\n");
while(ifEmpty(pQueue)!=0)
{
QNode *qNode = deQueue(pQueue);
//make suer every enQueue Node is not NULL
if(qNode->t_node->left!=NULL)
{enQueue(pQueue,qNode->t_node->left);}
if(qNode->t_node->right!=NULL)
{
enQueue(pQueue,qNode->t_node->right);
}
//print the tree node value
printf("%d ",qNode->t_node->value);
}
printf("\n-----------\nbreath end!\n-----------\n");
return 1;
}
int main()
{
Queue *queue=init_queue();
int i;
ifEmpty(queue);
printf("insert node to queue\n");
Tree root = creatTree();
if(!root)
{
printf("create Tree failed!\n");
return 0;
}
breath_travel(root,queue);
// free(queue);
return 0;
}
if this version can function well in my computer i have to add a unused int
"int length" in the beginning " _q_node" structure , if i don't add it the ifEmpty function cannot find the right position like "pQueue->head == NULL"
why this happen?
Your program has a bug in the insert_tree function. I have added a few comments to your code:
int insert_tree(Tree pTree,int pValue)
{
//found NULL sub tree, then add to his father->left
if(!pTree)
{
return 0;
}
TNode *tNode = init_TNode(pValue);
if(tNode==NULL)
{
printf("create TNode error!\n");
return 0;
}
if(pValue < pTree->value)
if(insert_tree(pTree->left,pValue)==0) // Here the return value is used
{
//no left child any more,set a new left child to pTree
pTree->left = tNode;
printf("insert :%d\n",pValue);
}
if(pValue > pTree->value)
if(insert_tree(pTree->right,pValue)==0) // Here the return value is used
{
pTree->right = tNode;
printf("insert :%d\n",pValue);
}
// No return value here !!
}
As you can see from my comments you miss a return value at the end of the function. Since you use that return value in other places, you program uses some uninitialized return value. That can make your program fail.
BTW: enQueue miss a return value as well.
An advice: Always compile your code with a high warning level and consider all warnings to be errors. In other words - if there are warnings they shall be fixed before running the code.
If you compile using gcc use -Wall to get all warnings
Besides that I think there is a problem with the logic in this function. It uses recursion to find where to insert the new value. In each recursive call you create a new node using TNode *tNode = init_TNode(pValue); but you only use it at the end of the recursion. In other words it seems you have memory leaks.
Further it's unclear how/where you handle the case where pValue is equal to pTree->value
BTW: pValue is a real bad name for an integer as the p makes you think it's a pointer.

I can't inset a node to bin search tree

I want to write a BST,but the insert function doesn't work. Debugging it , I found that it was not inserting.
/* Binary Search Tree (BST).demo */
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode{
int data;
struct treeNode* lChild;
struct treeNode* rChild;
} treeNode;
treeNode* createNode(){
treeNode *nNode;
nNode=(struct treeNode*)malloc(sizeof(treeNode));
nNode->data=0;
nNode->lChild=NULL;
nNode->rChild=NULL;
return nNode;
}
void insert(treeNode* rt,int idata)
{
if(rt==NULL){
treeNode* nNode;
nNode=createNode();
nNode->data=idata;
rt=nNode;
rt->lChild=NULL;
rt->rChild=NULL;
}else{
if(idata < rt->data)
insert(rt->lChild,idata);
else insert(rt->rChild,idata);
}
}
int main()
{
treeNode *root;
root=(treeNode*)malloc(sizeof(treeNode));
root->data=15;
root->lChild=NULL;
root->rChild=NULL;
insert(root,13);
if(root->lChild==NULL)
printf("root no l child\n");
// printf("root lchild data :%d",root->lChild->data);
return 0;
}
use this as the insert function :
void insert(treeNode** rt,int idata)
{
if((*rt)==NULL)
{
treeNode* nNode;
nNode=createNode();
nNode->data=idata;
*rt=nNode;
(*rt)->lChild=NULL;
(*rt)->rChild=NULL;
}
else
{
if(idata < (*rt)->data)
insert(&((*rt)->lChild),idata);
else
insert(&((*rt)->rChild),idata);
}
}
and the insert function call in main() as:
insert(&root,13);
I usually implement it like this:
void insert(treeNode* rt, int idata) {
// assert( rt != NULL );
if(idata < rt->data){
if( rt->lChild != NULL)
insert(rt->lChild, idata);
else {
rt->lChild = createNode();
rt->lChild->data = idata;
}
} else {
if( rt->rChild != NULL)
insert(rt->rChild, idata);
else {
rt->rChild = createNode();
rt->rChild->data = idata;
}
}
}
Moreover, I prefer give an argument to createNode()
treeNode* createNode(int idata){
// ...
nNode->data = idata;
// ...
}
So the insert will look like:
void insert(treeNode* rt, int idata) {
// ...
rt->lChild = createNode(idata);
// ...
rt->rChild = createNode(idata);
// ...
}

Resources