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 tagBinarySearch TreeNode BSTNode;
struct tagBinarySearch TreeNode
{
Stock *stock;
BSTNode *left;
BSTNode *right;
};
typedef struct tagBinarySearch Tree
{
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
=============== =============== =========== 7 1996
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. 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
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 tagBinarySearch TreeNode BSTNode; struct tagBinarySearch TreeNode { Stock *stock; BSTNode *left; BSTNode *right; };
typedef struct tagBinarySearch Tree { 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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
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!=NU LL);
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;
}
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!=NU LL); 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>>
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
/ \ / \ / \
|
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 random binary
search tree. We've tried everything but are in the land of pointer
hell. If someone could help it would be a huge help. our code follows.
We've tried 2 diff methods the split() and splitter() functions
#include <iostream>
#include...
|
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, even
the removal, I just don't know how to keep track of the parent, so
that I can set its child to the child of the node to be removed.
IE - if I had
C
/ \
B D
|
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 lists. I'm totally new
to binary searches by the way. Can anyone help me with the commented
sections below? Much of the code such as functions and printfs has
already been completed. Any help is greatly appreciated.
Thanks,
|
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. Now i have entries
something like this
IPaddress Phone No
192.168.2.1 2223447689
192.168.2.2 3334449898
192.168.2.3 5667869089
| |
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 and a less or equal
number is appended as a left child. Now i want to write a function
which deletes the node with the highest number in the tree. I started
the function as follows:
|
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 <typename Tclass Tree{
private:
Tree<T*left;
|
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 into an infinite
loop, i think im messing up with the pointers.
Heres the code.
//press 1, first, Do not press it a second time!!!
|
by: Defected |
last post by:
Hi,
How i can create a Binary Search Tree with a class ?
thanks
|
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 binary tree is a finite set of elements that is either empty or is
partitioned into three disjoint subsets. The first subset contains a
single element called the 'Root' of the tree. The other two subsets
are themselves binary trees, called the 'Left'...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |