#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __node_t
{
char b_word[50];
int frekans;
struct __node_t* right;
struct __node_t* left;
}node__t;
void preorder(node__t*);
void insert(node__t**,char[]);
int main(void)
{
node__t *array[50];
insert(&array[0],"muho");
insert(&array[0],"zed");
insert(&array[0],"a");
preorder(array[0]);
insert(&array[1],"rte");
insert(&array[1],"gfds");
insert(&array[1],"fdg");
preorder(array[1]);
printf("\n");
system("Pause");
return 0;
}
void preorder(node__t *root){
if (root==NULL) return ;
preorder(root->left);
printf("%s\n",root->b_word);
preorder(root->right);
}
void insert(node__t **root, char wordd[]){
node__t *newnode;
newnode=malloc(sizeof(node__t));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
(*root)=newnode;
strcpy((*root)->b_word,wordd);
return;
}
char str[25][25];
strcpy(str[1],(*root)->b_word);
strcpy(str[2],wordd);
if(strcmp(str[1],str[2])>0){
insert(&(*root)->left,wordd);
}
if(strcmp(str[1],str[2])<=0){
insert(&(*root)->right,wordd);
}
}
23 Replies - 849 Views - Last Post: 13 May 2012 - 02:53 AM
#1
Relation between array and binary search tree
Posted 11 May 2012 - 12:23 PM
Hi all.I want to storage trees in an array.I can insert array[0],but I cant insert array[1].why doesn't it work correctly? Look at 25th row.you will see what I want to do
Replies To: Relation between array and binary search tree
#2
Re: Relation between array and binary search tree
Posted 11 May 2012 - 12:38 PM
What are you trying to do in the following line?
Where do you allocate memory for that pointer?
Jim
node__t *array[50];
Where do you allocate memory for that pointer?
Jim
#3
Re: Relation between array and binary search tree
Posted 11 May 2012 - 01:03 PM
I try this.For example array[0],array[1]......array[n].this is trees.
int array[5];
array[0]=56;
array[1]=67;
...
I want to do this.
array[0]= binary search tree
array[1]=binary seacrh tree2
....
understand?
array's type is not integer,double exc. .Array type is struct type.In my code it is node__t.Is it false ?
int array[5];
array[0]=56;
array[1]=67;
...
I want to do this.
array[0]= binary search tree
array[1]=binary seacrh tree2
....
understand?
array's type is not integer,double exc. .Array type is struct type.In my code it is node__t.Is it false ?
#4
Re: Relation between array and binary search tree
Posted 11 May 2012 - 01:31 PM
The problem is not the type of variable. The problem is you are not allocating any memory for that pointer. If you want an array of 50 nodes then you don't need the pointer.
Although I doubt you need an array of your node in main. You probably should only need a single instance. The tree should be built by your insert() function.
Jim
// Create an array of node__t with a size of 50. node__t yourNode[50]
Although I doubt you need an array of your node in main. You probably should only need a single instance. The tree should be built by your insert() function.
Jim
#5
Re: Relation between array and binary search tree
Posted 11 May 2012 - 01:36 PM
You can create a binary tree using just an array. There's no need for pointers to the left and right nodes. If i is a node, the left node is 2*i+1 and the right node is 2*i+2 eg.
Otherwise just store the base node and you don't need an array as you can access all nodes from the base node by traversing the tree.
0
1 2
3 4 5 6
Otherwise just store the base node and you don't need an array as you can access all nodes from the base node by traversing the tree.
#6
Re: Relation between array and binary search tree
Posted 11 May 2012 - 01:39 PM
I can insert nodes to a tree.But I want to reach this tree with and array.for example my tree's name are "mdn","muh","naz".Three different tree.I want to do something like that.array[0]=mdn,array[1]=muh,array[2]=naz.Cant I do something like that?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __node_t
{
char b_word[50];
int frekans;
struct __node_t* right;
struct __node_t* left;
}node__t;
void preorder(node__t*);
void insert(node__t**,char[]);
int main(void)
{
int array[50];
node__t *tree=NULL;
insert(&tree,"muho");
insert(&tree,"zed");
insert(&tree,"a");
preorder(tree);
printf("\n");
system("Pause");
return 0;
}
void preorder(node__t *root){
if (root==NULL) return ;
preorder(root->left);
printf("%s\n",root->b_word);
preorder(root->right);
}
void insert(node__t **root, char wordd[]){
node__t *newnode;
newnode=malloc(sizeof(node__t));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
(*root)=newnode;
strcpy((*root)->b_word,wordd);
return;
}
char str[25][25];
strcpy(str[1],(*root)->b_word);
strcpy(str[2],wordd);
if(strcmp(str[1],str[2])>0){
insert(&(*root)->left,wordd);
}
if(strcmp(str[1],str[2])<=0){
insert(&(*root)->right,wordd);
}
}
#7
Re: Relation between array and binary search tree
Posted 11 May 2012 - 02:21 PM
There's nothing wrong with creating an array of trees, if that's what you need.
And there's "almost" nothing wrong with your implementation in Post #1. If you change the beginning of your main function slightly, and then run it, you'll see what the problem is:
Just add the printf statements after the declaration of the node__t *array, compile & run it, and see if that rings any bells.
(You forgot something very fundamental.)
And there's "almost" nothing wrong with your implementation in Post #1. If you change the beginning of your main function slightly, and then run it, you'll see what the problem is:
int main(void)
{
node__t *array[50];
for(int i = 0; i < 50; i++) {
printf("%p ", (void*)array[i]);
}
printf("\n");
// ...
Just add the printf statements after the declaration of the node__t *array, compile & run it, and see if that rings any bells.
(You forgot something very fundamental.)
#8
Re: Relation between array and binary search tree
Posted 11 May 2012 - 02:41 PM
Is it wrong? look at 30th,31th and 32th rows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __node_t
{
char b_word[50];
int frekans;
struct __node_t* right;
struct __node_t* left;
}node__t;
void preorder(node__t*);
void insert(node__t**,char[]);
int main(void)
{
node__t *array[50];
node__t *tree=NULL;
/* int i;
for(i = 0; i < 50; i++) {
printf("%p ",(void*)array[i]);
}*/
insert(&tree,"muhooooooo");
array[0]=tree;
printf("%p",(void*)array[0]);
printf("\n");
system("Pause");
return 0;
}
void preorder(node__t *root){
if (root==NULL) return ;
preorder(root->left);
printf("%s\n",root->b_word);
preorder(root->right);
}
void insert(node__t **root, char wordd[]){
node__t *newnode;
newnode=malloc(sizeof(node__t));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
(*root)=newnode;
strcpy((*root)->b_word,wordd);
return;
}
char str[25][25];
strcpy(str[1],(*root)->b_word);
strcpy(str[2],wordd);
if(strcmp(str[1],str[2])>0){
insert(&(*root)->left,wordd);
}
if(strcmp(str[1],str[2])<=0){
insert(&(*root)->right,wordd);
}
}
#9
Re: Relation between array and binary search tree
Posted 11 May 2012 - 02:51 PM
Output is an adress value
#10
Re: Relation between array and binary search tree
Posted 11 May 2012 - 03:10 PM
Your insert function says
but you don't know what *root equals because you didn't initialize the array. *root == garbage.
You need
if ((*root)==NULL){
// ...
but you don't know what *root equals because you didn't initialize the array. *root == garbage.
You need
int main(void)
{
node__t *array[50];
int i;
for(i = 0; i < 50; i++) {
array[i] = NULL;
}
// ...
#11
Re: Relation between array and binary search tree
Posted 11 May 2012 - 03:13 PM
Thanks you a lot
#12
Re: Relation between array and binary search tree
Posted 11 May 2012 - 03:26 PM
Ah, so he wanted an array of binary search trees, not to store a binary search tree in an array. If only English were an easier and more logical language...
#13
Re: Relation between array and binary search tree
Posted 11 May 2012 - 06:09 PM
#14
Re: Relation between array and binary search tree
Posted 12 May 2012 - 01:47 PM
Hi.Can you help me to find run time error? Look at 50th row.problem is here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __node_t
{
char filename[50];
char b_word[50];
int frekans;
struct __node_t* right;
struct __node_t* left;
}node__t;
void preorder(node__t*);
void insert(node__t**,char[]);
node__t *search_bst(node__t *,char[]);
int main(void)
{
node__t *bulunan;
node__t *array[50];
node__t *tree=NULL;
FILE *inp;
int n=0;
char dosyaadi[50];
int status;
inp = fopen("input.txt","r");
status=fscanf(inp,"%s",&dosyaadi);
while(status!=EOF){
status= fscanf(inp,"%s",dosyaadi);
n++;
}
printf("%d",n);
fclose(inp);
FILE *iny;
iny = fopen("input.txt","r");
int durum;
while(durum!=EOF){
durum=fscanf(inp,"%s",&dosyaadi);
int i;
for(i = 0; i < n; i++) {
array[i]=NULL;printf("OK");
strcpy(array[i]->filename,dosyaadi);
}
}
fclose(iny);
printf("\n");
system("Pause");
return 0;
}
void preorder(node__t *root){
if (root==NULL) return ;
preorder(root->left);
printf("%s\n",root->b_word);
preorder(root->right);
}
void insert(node__t **root, char wordd[]){
node__t *newnode;
newnode=malloc(sizeof(node__t));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
(*root)=newnode;
strcpy((*root)->b_word,wordd);
return;
}
char str[25][25];
strcpy(str[1],(*root)->b_word);
strcpy(str[2],wordd);
if(strcmp(str[1],str[2])>0){
insert(&(*root)->left,wordd);
}
if(strcmp(str[1],str[2])<=0){
insert(&(*root)->right,wordd);
}
}
node__t *search_bst(node__t *tree,char targetword[50]){
node__t *current;
current=tree;
if(tree==NULL)
return NULL;
if(strcmp(current->b_word,targetword)==0)
return current;
if(strcmp(current->b_word,targetword)>0)
return search_bst(current->left,targetword);
if(strcmp(current->b_word,targetword)<=0)
return search_bst(current->right,targetword);
}
#15
Re: Relation between array and binary search tree
Posted 12 May 2012 - 02:14 PM
I see a couple of problems. First I think you meant to use iny as the first argument on line 46. inp was closed on line 37, and then you reopened the file on line 42 as iny. (I don't know why you declared a new FILE*; you could have re-used inp.) So as a result there is nothing stored in dosyaadi.
But also, on line 49 you set array[i] = NULL. So array[i] is a null pointer; it doesn't point to any node. And therefore you will crash on line 50 where you try to access array[i]->filename. If there's no node, there's no memory space for a filename.
But also, on line 49 you set array[i] = NULL. So array[i] is a null pointer; it doesn't point to any node. And therefore you will crash on line 50 where you try to access array[i]->filename. If there's no node, there's no memory space for a filename.
|
|

New Topic/Question
Reply




MultiQuote






|