472,796 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

simple tree data struct implementation (beginner)


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
int data;
struct tree *left,*right;
};

void init(struct tree *node)
{
node = malloc(sizeof node);
if(!node)
{
printf("mem error\n");
exit(EXIT_FAILURE);
}
}

struct tree *assign(int data,struct tree *node)
{
struct tree *temp;
memmove(temp,node,sizeof node);
init(temp);
temp->data = data;
if(data < temp->data)
{
temp = temp->left;
temp->data = data;
}
else if(data > temp->data)
{
temp = temp->right;
temp->data = data;
}
else
temp->right = temp->left = NULL;
memmove(node,temp,sizeof node);
return node;
}

int print_tree(struct tree *node)
{
printf("%d\n",node->data);
while(node != NULL)
{
struct tree *temp;
memmove(temp,node,sizeof node);
temp = temp -> right;
print_tree(temp);
temp = temp-> left;
memmove(node,temp,sizeof node);
}
return 0;
}

int main(void)
{
struct tree *node;
node = assign(1 , node);
node = assign(2 , node);
node = assign(8 , node);
print_tree(node);
return 0;
}

This little, ugly code does not work as expected. I am getting a memory
overflow error. What could be the problem? Help.
--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #1
6 3611
sathyashrayan wrote:
int print_tree(struct tree *node)
{
printf("%d\n",node->data);
while(node != NULL)
{
struct tree *temp;
memmove(temp,node,sizeof node); There is at least one good reason it crashes. You cannot move data into
a "junk" pointer. Anyway, why do you need to move it? I haven't looked
at your code too closely, but I think removing the above line should
solve your problem.
temp = temp -> right;
print_tree(temp);
temp = temp-> left;
memmove(node,temp,sizeof node);
}
return 0;
}

This little, ugly code does not work as expected. I am getting a memory
overflow error. What could be the problem? Help.


--
bjrnove

Nov 14 '05 #2
"sathyashrayan" <sa***********@REMOVETHISgmail.com> wrote:
void init(struct tree *node)
{
node = malloc(sizeof node); struct tree *assign(int data,struct tree *node)
{
struct tree *temp;
memmove(temp,node,sizeof node);
init(temp); int print_tree(struct tree *node)
{
printf("%d\n",node->data);
while(node != NULL)
{
struct tree *temp;
memmove(temp,node,sizeof node);
temp = temp -> right;
print_tree(temp);
temp = temp-> left;
memmove(node,temp,sizeof node);
} This little, ugly code does not work as expected. I am getting a memory
overflow error.


No wonder. You write through your pointer before you even try to
allocate memory for it; and even when you do, you do so in a way that
doesn't work. Read <http://www.eskimo.com/~scs/C-faq/q4.8.html>; and
then allocate memory _before_ you write to it.
(BTW, IMO "init" is not the right name for that function. All it does is
allocate memory; it does not initialise anything. Ditto for "assign",
which creates a whole new node, and doesn't just assign values.)
Also, when you get this to work, you're going to have fun with your
printing function. In particular, you're going to have fun watching it
spin merrily in its hamster-wheel, printing nothing.

Richard
Nov 14 '05 #3
sathyashrayan wrote:
[...]
struct tree *assign(int data,struct tree *node)
{
struct tree *temp;
memmove(temp,node,sizeof node); [...] This little, ugly code does not work as expected. I am getting a memory
overflow error. What could be the problem? Help.


Among other things, temp is a wild pointer and there is no reason to
think that you code won't explode as soon as it hits that memmove.
Nov 14 '05 #4
sathyashrayan wrote:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
int data;
struct tree *left,*right;
};

void init(struct tree *node)
{
node = malloc(sizeof node);
if(!node)
{
printf("mem error\n");
exit(EXIT_FAILURE);
}
}


I read no further than here. The only function of init is to
either abort the program, or to create a memory leak. Think about
it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5


sathyashrayan wrote:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
int data;
struct tree *left,*right;
};

void init(struct tree *node)
{
node = malloc(sizeof node);
if(!node)
{
printf("mem error\n");
exit(EXIT_FAILURE);
}
}

First problem: you're allocating the wrong amount of memory. You're
only allocating enough to hold an object of type struct tree*, not type
struct tree (type of node == struct tree*, type of *node == struct
tree). You need to dereference node in the sizeof operation to get the
right size.

Second problem: the assignment to node won't be reflected in the
calling function. Remember that to write to a parameter, you need to
pass a pointer to that type. If you want to write to a pointer, you
have to pass a pointer to a pointer, like so:

void init(struct tree **node)
{
*node = malloc(sizeof **node);
...
}
....

init(&temp);

Third problem: you aren't initializing the contents of the node after
you allocate it. The left and right members don't point anywhere
meaningful; they're just random bit strings that may or may not resolve
to valid addresses. If you *know* the left and right members will
*never* be read before they are assigned, it's not a problem. However,
it's usually good practice to explicitly initialize pointers to NULL.
That way you have a realiable test to see if they point anywhere
meaningful or not.

void init(struct tree **node)
{
*node = malloc(sizeof **node);
if (!*node)
{
/* handle memory allocation error */
}
(*node)->left = (*node)->right = NULL;
}

My personal preference is to write allocators as functions returning
the type of object being allocated, instead of passing the object to an
initializer function. Instead of void init(struct tree **node), I'd
use

struct tree *new_node(void)
{
struct tree *tmp = malloc(sizeof *tmp);
if (!tmp)
{
/* log memory allocation error */
return NULL;
}
tmp->left = tmp->right = NULL;
tmp->data = 0; // or some other initial value
return tmp;
}

I also write a corresponding deallocator function:

void destroy_node(struct tree **node)
{
if (*node)
{
free(*node);
*node = NULL;
}
}
struct tree *assign(int data,struct tree *node)
{
struct tree *temp;
memmove(temp,node,sizeof node);


Fourth problem (and the cause of your error): temp doesn't yet point
anywhere meaningful; you haven't attached a valid memory location to it
yet. You need to init temp before attempting to copy to it. Move the
init() call before this call.

There are other logic problems, but I have to run, and this should get
you past the initial memory errors.

Nov 14 '05 #6
sathyashrayan wrote:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
int data;
struct tree *left,*right;
};

void init(struct tree *node)
{
node = malloc(sizeof node);
node = malloc(sizeof *node)
if(!node)
{
printf("mem error\n");
exit(EXIT_FAILURE);
}
}
You never use the value of node so you have leaked memory.
Also you never use the value of 'node' that you passed in.
Try this (or the other posted solution):

struct tree *init(void)
{
struct tree *node = malloc(sizeof *node);
if (!node) {...........}
return node;
}
struct tree *assign(int data,struct tree *node)
{
struct tree *temp;
memmove(temp,node,sizeof node);
init(temp);
struct tree *temp;
temp = init();
memmove(temp, node, sizeof *temp);
temp->data = data;
if(data < temp->data)
{
temp = temp->left;
Here you leak the memory that you allocated with the call to init().
temp->data = data;
}
else if(data > temp->data)
{
temp = temp->right;
Same again.
temp->data = data;
}
else
temp->right = temp->left = NULL;
memmove(node,temp,sizeof node);
memmove(node, temp, sizeof *node);
return node;
}

int print_tree(struct tree *node)
{
printf("%d\n",node->data);
while(node != NULL)
{
struct tree *temp;
memmove(temp,node,sizeof node);
memmove(temp, node, sizeof *node);
temp = temp -> right;
print_tree(temp);
temp = temp-> left;
memmove(node,temp,sizeof node);
memmove(node, temp, sizeof *node);
}
return 0;
}
Your function is called "print_tree". I would have thought it
would print the tree (and not modify it). But what it actually
does is to print the top node, then print its right node (recursively),
then replace the top node with the left node.

This mangles the whole tree, and only prints part of it.
int main(void)
{
struct tree *node;
node = assign(1 , node);
You pass the uninitialized pointer to the function. The assign
function goes on to copy data out of this pointer.
I can't suggest a fix as I really have no idea what you are
trying to accomplish here.
node = assign(2 , node);
node = assign(8 , node);
print_tree(node);
return 0;
}

This little, ugly code does not work as expected.


It would help us to fix your code, if you wrote what you were
expecting to see.

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Casper | last post by:
In scanning a drive and comparing file content, I need to remember filepaths to every single file I encounter so I can take actions on certain files later on. Rather than having a huge list...
5
by: nandor.sieben | last post by:
I'm working on a project analyzing a game and I am in need of a tree template library. I found a few implementations on the web but all of them were too complicated for me to understand. I wrote...
4
by: dam_fool_2003 | last post by:
I am just a beginner in tree data – struct. I have this little doubt. Left node ‘weights' lesser than the right one. I have seen, so far it is algorithm implementations. But why not vice-versa that...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
1
by: Foodbank | last post by:
Hi, I'm currently teaching myself C data structures in preparation for a job transition and I've just passed the point in which radix sorting was covered in my book. I've recently finished a...
1
by: nandor.sieben | last post by:
I have an n-ary tree implementation consisting of a header file. I include this ntree.hh file and an example code below called ntree.C. All this seems to be working. I have a problem with the...
0
by: coosa | last post by:
Dear all; My code is is a bit long but is modular at least. I'm attempting to implement the depth first search for an application that is supposed to: 1- Fetch based on an ID from the database a...
36
Ganon11
by: Ganon11 | last post by:
OK, first of all, thanks to everyone who helped me out with my Isomorphism problem - it finally works. Now, the other part of my homework I'm having trouble with is this: I don't want to post...
20
by: DemonFox | last post by:
i have started my midterm exersize than is on binary treescan anyone help me on the basics i have started and i have made the following on my tree.h file: struct treenode { int data;...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.