473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

binary search tree problem

the binary search tree node here contains another structure as it's
data field,
programs did successfully work when data field is int, char, this time
i got stucked, don't know why ? if there's something to do with
dynamic data object ?
thanx for your help.

================== begin of code ==============================
#include <stdio.h>
#include <stdlib.h>

typedef struct tagStock
{
char name[64];
int tag;
} Stock;

typedef struct tagBinarySearchTreeNode BSTNode;
struct tagBinarySearchTreeNode
{
Stock *stock;
BSTNode *left;
BSTNode *right;
};

typedef struct tagBinarySearchTree
{
size_t size;
BSTNode *root;
} BSTree;

Stock *
CreateStock (char *name, int tag)
{
Stock *stock;

stock = (Stock *) malloc (sizeof (Stock));
if (stock == NULL)
return NULL;

strcpy (stock->name, name);
stock->tag = tag;

return stock;
}

BSTNode *
InsertNode (BSTree * tree, Stock * stock)
{
BSTNode *tmp;
BSTNode *root = tree->root;
if (root == NULL)
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tree->size = 1;
}
else
{
while (root != NULL)
{
tmp = root;
if ((stock->tag) < (root->stock->tag))
root = root->left;
else
root = root->right;
}

if ((stock->tag) < (tmp->stock->tag))
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tmp->left = root;
tree->size++;
}
else if ((stock->tag) > (tmp->stock->tag))
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tmp->right = root;
tree->size++;
}
else return NULL;
}
return root;
}

void
Traversal (BSTNode * root)
{
if (root == NULL)
return;
Traversal (root->left);
printf ("%d\n", root->stock->tag);
Traversal (root->right);
}

int
main (int argc, char **argv)
{
char name[64];
int tag = 0, i;
Stock *stock;
BSTree *tree = NULL;

for (i = 0; i < 10; ++i)
{
scanf ("%s", name);
scanf ("%d", &tag);
stock = CreateStock (name, tag);
InsertNode (tree, stock);
}

Traversal (tree->root);

return 0;
}
======================== end of code
=========================================
Nov 14 '05 #1
7 1932
In article <ad**************************@posting.google.com >,
ru****@sohu.com (sugaray) wrote:
<snipped>


In function InsertNode, you have a loop

while (root != NULL) { ... }

Since there is no break statement in the loop, you will have root ==
NULL after executing this loop. However, in the following code you have
assignments to root->xxx - that's a bad idea.
Nov 14 '05 #2
ru****@sohu.com (sugaray) wrote:
the binary search tree node here contains another structure as it's
data field,
programs did successfully work when data field is int, char,
By accident.
this time i got stucked, don't know why ? if there's something to do with
dynamic data object ?
It's nothing to do with your struct member and everything with your
tree.
InsertNode (BSTree * tree, Stock * stock)
{
BSTNode *tmp;
BSTNode *root = tree->root;
This function asks for tree->root without checking whether tree points
at a valid tree object...
BSTree *tree = NULL; InsertNode (tree, stock);
....yet in your main function, you pass it a null pointer. That this
worked before is mere accident. Correct InsertNode() so that it checks
for null pointers, and does something about them.
if (root == NULL)
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tree->size = 1;
}


And while you're at it, it would be a very good idea not to
_intentionally_ write through a null pointer. You do this throughout
your insertion function. Where did you think all those nodes would end
up? They aren't assigned some memory by magic, you know.

Richard
Nov 14 '05 #3
sugaray wrote:

the binary search tree node here contains another structure as
it's data field,
programs did successfully work when data field is int, char, this
time i got stucked, don't know why ? if there's something to do
with dynamic data object ?
thanx for your help.

================== begin of code ==============================
#include <stdio.h>
#include <stdlib.h>

typedef struct tagStock
{
char name[64];
int tag;
} Stock;

typedef struct tagBinarySearchTreeNode BSTNode;
struct tagBinarySearchTreeNode
{
Stock *stock;
BSTNode *left;
BSTNode *right;
};

typedef struct tagBinarySearchTree
{
size_t size;
BSTNode *root;
} BSTree;

Stock *
CreateStock (char *name, int tag)
{
Stock *stock;

stock = (Stock *) malloc (sizeof (Stock));
if (stock == NULL)
return NULL;

strcpy (stock->name, name);
stock->tag = tag;

return stock;
}

BSTNode *
InsertNode (BSTree * tree, Stock * stock)
{
BSTNode *tmp;
BSTNode *root = tree->root;
if (root == NULL)
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tree->size = 1;
}


I stopped right here. If root is NULL, how can root possibly
point to any fields whatsoever?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
after a bit of modification of the faults everybody pointed out,
it still can't work correctly. i'm kinda in the middle of nowhere right
now.

BSTNode *InsertNode (BSTree * tree, Stock * stock)
{
BSTNode **tmp;
BSTNode *root;

if(tree!=NULL)
root=tree->root;

if (root == NULL)
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
tree->size = 1;
}
else
{
while (root != NULL)
{
tmp = &root;
if ((stock->tag) < (root->stock->tag))
root = root->left;
else
root = root->right;
}

if ((stock->tag) < ((*tmp)->stock->tag))
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
(*tmp)->left = root;
tree->size++;
}
else if ((stock->tag) > ((*tmp)->stock->tag))
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
(*tmp)->right = root;
tree->size++;
}
else return NULL;
}
return root;
}

int main (int argc, char **argv)
{
char name[64];
int tag = 0, i;
Stock *stock;
BSTree *tree;
tree=(BSTree *)malloc(sizeof(BSTree));
assert(tree!=NULL);
tree->root=NULL;
tree->size=0;

for (i = 0; i < 10; ++i)
{
scanf ("%s", name);
scanf ("%d", &tag);
stock = CreateStock (name, tag);
InsertNode (tree, stock);
}

Traversal (tree->root);

free(tree);

return 0;
}
Nov 14 '05 #5
On 9 Jun 2004 20:01:21 -0700, ru****@sohu.com (sugaray) wrote:
after a bit of modification of the faults everybody pointed out,
it still can't work correctly. i'm kinda in the middle of nowhere right
now.

BSTNode *InsertNode (BSTree * tree, Stock * stock)
{
BSTNode **tmp;
BSTNode *root;

if(tree!=NULL)
root=tree->root;

if (root == NULL)
If tree is NULL, root is uninitialized and this invokes undefined
behavior.
{
root->stock = stock;
Since root is NULL, you cannot dereference as you try to do here.
This invokes undefined behavior.
root->left = NULL;
root->right = NULL;
tree->size = 1;
Since you check for tree being NULL earlier, it is possible that it is
NULL here also.
}
else
{
while (root != NULL)
{
tmp = &root;
&root is a constant. Why is this assignment inside the loop?
if ((stock->tag) < (root->stock->tag))
root = root->left;
else
root = root->right;
}

if ((stock->tag) < ((*tmp)->stock->tag))
(*tmp) will always evaluate to root. What is accomplished by this
additional level of indirection?
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
(*tmp)->left = root;
tree->size++;
}
else if ((stock->tag) > ((*tmp)->stock->tag))
{
root->stock = stock;
root->left = NULL;
root->right = NULL;
(*tmp)->right = root;
These two assign values to the same variable.

Do you really want root->right pointing to the same struct that root
points to? The list will become circular.
tree->size++;
}
else return NULL;
}
return root;
}

int main (int argc, char **argv)
{
char name[64];
int tag = 0, i;
Stock *stock;
BSTree *tree;
tree=(BSTree *)malloc(sizeof(BSTree));
Don't cast the return from malloc. It cannot help in this case but
will suppress a useful diagnostic if you forget to include stdlib.h.
assert(tree!=NULL);
tree->root=NULL;
tree->size=0;

for (i = 0; i < 10; ++i)
{
scanf ("%s", name);
scanf ("%d", &tag);
stock = CreateStock (name, tag);
InsertNode (tree, stock);
}

Traversal (tree->root);

free(tree);

return 0;
}


<<Remove the del for email>>
Nov 14 '05 #6
sugaray wrote:

after a bit of modification of the faults everybody pointed out,
it still can't work correctly. i'm kinda in the middle of
nowhere right now.


You top-posted this as a reply to a message of mine, and totally
ignored the fundamental error I pointed out. Search google for
Richard Heathfields course on reading.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #7
ru****@sohu.com (sugaray) wrote:
after a bit of modification of the faults everybody pointed out,
it still can't work correctly.


You did not correct _any_ of the errors I pointed out to you. You did,
indeed, only "modify the faults"; the faults are still there, but in
some cases you've muddled the code by introducing an extra pointer and
leaving the original error intact. You still do not allocate any memory
for your nodes.
Before you continue trying to create dynamic data types, go back to the
chapter which explains allocated memory (i.e., malloc(), realloc(),
calloc() and free()). Do not go back to your tree until you _fully_
understand memory allocation. Reading it through once is not enough.
Until you understand completely what malloc() is for and how pointers
work (and more importantly, how they do not work), you will never write
a working binary tree implementation.

Richard
Nov 14 '05 #8

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

Similar topics

7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
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,...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
1
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc....
1
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.