S
sugaray
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
=========================================
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
=========================================