Oct 30, 2014 at 7:59am UTC
What is the difference between BST and linked list? Or are BST implemented through linked list?
Oct 30, 2014 at 10:02am UTC
But I seen some examples where BST implemented through linked list
Oct 30, 2014 at 10:27am UTC
They both have links, but I think you're mistaken.
Oct 31, 2014 at 12:37pm UTC
I am writing this code with my understanding of BST..please tell me if it is ok:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
in .h
class bst {
private :
struct hwareItem{
char barcode[10];
/*char description[50];
float price_per_unit;
int stock;*/
hwareItem *left;
hwareItem *right;
};
hwareItem *root;
public :
bst();
~bst();
void addNode(hwareItem*&, char []);
};
i
in .cpp
void bst::addNode(hwareItem*& root, char barcode[]){
if (root==NULL){
root=new hwareItem;
strcpy(root->barcode, barcode);
root->left=NULL;
root->right=NULL;
}
else if (root!=NULL){
if (root->left->barcode<root->barcode)
addNode(root->left,barcode);
else if (root->right->barcode>root->barcode)
addNode(root->right,barcode);
}
}
How do I implement this in main?
#include<iostream>
#include<fstream>
#include<cstring>
#include"bst.h"
using namespace std;
int main(){
bst val;
char bar[10];
strcpy(bar, "5505505" );
val.addNode(? , bar);
return 0;
}
Last edited on Oct 31, 2014 at 1:58pm UTC
Oct 31, 2014 at 3:08pm UTC
member of bst of what type?
type *root?
void* root?
am getting segmentation fault:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void bst::addNode(hwareItem*& root, char barcode[]){
if (root == NULL) {
root=new hwareItem;
strcpy(root->barcode, barcode);
root->left=NULL;
root->right=NULL;
} else if (strcmp(root->left->barcode, root->barcode) < 0) {
addNode(root->left,barcode);
}
else {
addNode(root->right,barcode);
}
}
int main(){
bst val;
char bar[10], bar1[10];
strcpy(bar, "5505505" );
val.addNode(bar);
strcpy(bar1, "1010101" );
val.addNode(bar);
val.addNode(bar1);
//val.printNodes();
return 0;
}
can add only root value
Last edited on Oct 31, 2014 at 3:41pm UTC