I am trying to construct a BST given a string. It is assumed that my strlen will always be in the power of 2 so that I can have a structure with the ends always leveled, like so:
I am having trouble starting this function. I've written a helper that willd construct a tree up to the final points here:
tree* build(int height)
{
int i;
tree *t = malloc(sizeof(tree));
t.tag = NODE;
t.u = node;
t.u.node->left = build(height - 1);
t.u.node->right = build(height - 1);
if(height == 1)
{
return t;
}
}
I would like to reach the end of each node and append the first two chars and so forth throughout the rest of the tree. I am lost, though.
Here are the structs:
typedef struct tagged_tree tree;
struct node {
tree *left, *right;
};
union tree_union {
struct node node;
char leaf;
};
enum tree_tag {
NODE, LEAF
};
struct tagged_tree {
enum tree_tag tag;
union tree_union u;
};
Related
I've just tried to sort a linked list in my own way. I tried to bubble sort it entirely with recursion. Below is my code. Sorting works fine. I just wanted to know if there are some better ways to do this. Should I have done it iteratively rather than using recursion? I'd be happy to hear some suggestions.
// Some preprocessing stuff ...
// definition of node
typedef struct node {
int num;
struct node *next;
}node;
// ... functions
int main(void)
{
node *list = create_list(n); // n is list's length
sort(list, list, NULL);
}
Sort function
void sort(const node *first_node, node *left, node *last_node)
{
node *right = left->next;
if (first_node == last_node)
return;
if (right == last_node)
{
last_node = left;
left = first_node;
}
else if (right->num < left->num)
{
swap(&left->num, &right->num);
left = right;
}
else
left = right;
sort(first_node, left, last_node);
}
I'm trying to create a function that does sorted insertion based on two variables, level and name. Apparently I'm having some logic and syntax errors.
My linked list structure:
struct node {
struct node *next;
int level;
char name;
};
My string compare function:
int compare(struct node *one, struct node *two)
{
return strcmp(one->name, two->name);
}
My insertion function:
void insert(struct node **head, const int level, const char name, int(*cmp)(struct node *l, struct node *r))
{
struct node *new =NULL;
/* Find the insertion point */
for (; *head; head = &(*head)->next)
{
if ((*head)->level > level) { // I think this is what is causing the issue
if (compare(*head, new) > 0)
break;
}
}
new = malloc(sizeof *new);
new->level = level;
new->name = name;
new->next = *head;
*head = new;
}
and this is the call stack:
insert(node **head, const int level, const char name, int(*)(node *, node *))
Your syntax error is this line:
return strcmp(one->name, two->name);
The function strcmp expect two char* (aka char pointers) but you give it two char.
The problem is... Do you want
char name;
or
char* name;
That is important in order to get compare right.
Further you need to rearrange your insert function so that you create the new node before using it. Something like:
void insert(struct node **head, const int level, const char name, int(*cmp)(struct node *l, struct node *r))
{
struct node *new =NULL;
// Create and initialize new....
new = malloc(sizeof *new);
new->level = level;
new->name = name;
/* Find the insertion point */
for (; *head; head = &(*head)->next)
{
if ((*head)->level > level) { // I think this is what is causing the issue
if (cmp(*head, new) > 0)
// ^^^ So that you can use it here
break;
}
}
new->next = *head;
*head = new;
}
You are passing a NULL value to the cmp function (?!? probably the correct function is int compare(...). Try to initialize the value of the new variable before to pass it to the function.
You declare node.name to be of type char, but your comparison function is written as if they were null-terminated arrays of char or pointers into such arrays (i.e. C strings). You appear to want this:
struct node {
struct node *next;
int level;
char *name;
};
or maybe this:
struct node {
struct node *next;
int level;
char name[MY_MAXIMUM_NAME_LENGTH_PLUS_ONE];
};
Furthermore, your insert() function passes a NULL pointer to the comparison function as its second argument, because you never allocate any memory for pointer new, and, of course, never assign values to the non-existent members. That doesn't even make sense. What do you think you're comparing to? You seem to want something like this:
struct node *new = malloc(sizeof *new);
if (!new) {
// allocation failure -- abort ...
}
new->level = level;
new->name = /* hmmmm ... */;
Of course, the problem with the type of your names crops up here, too.
I've a nested struct like this
typedef struct Node_link {
struct Node_base *parent, *left, *right;
}Node_link;
typedef struct Node_base {
struct Node_link link[2];
}Node_base;
typedef struct Node{
struct Node_base base;
int size;
int *address;
}Node;
Node_base *head[2] ={NULL, NULL};
//head[0] stores int size and head[1] it's corresponding address
The node has right, left and parent link, all are nested e.g node->left->link.parent=node. I've to maintain all links(parent, left and right) and delete nodes.
I've tried a lot of cases and still missing some. Can someone tell me what all cases I need to use? Or refer me to some material? I searched a lot but no success.
My insert function is as follows:
Node_base * insert(Node_base *location, Node_base *n) {
if (head[0]==NULL)
head[0]=n;
else
{
if (location==NULL){
location=n;
return location;
}
else{
if(((Node *)n)->size < ((Node *)location)->size){
if(location->link[0].left==NULL)
{
location->link[0].left=n;
location->link[0].left->link[0].parent=location;
}
else
location->link[0].left=insert(location->link[0].left,n);
return location;
}
}
And I've the same nested insert function for head[1] which stores the size of node inserted in head[0].
It's hard to tell what's going on here. Your code doesn't look remotely like any BST implementation I've ever seen. Why the need for the Node_Link struct? The pointers in the Node structs should define what the links are. Why the parent pointer? That shouldn't be needed in a standard BST implementation. All you should need is:
struct node {
node *left;
node *right;
void *data;
int size;
};
struct bst {
node *root;
};
I've been stuck on the insertion part of the binary search tree. I get so confused with nested structs. The basic idea of this program is to create a bst that is able to hold names and double values which get stored by value (obviously).
Example: I want to store
Jane 3.14
John 3.233
Luke 6.4
Mike 1.4
so the bst would look like
3.14
/ \
1.4 3.233
\
6.4
however I'm having trouble with the insertHelper recursion portion of the code. The hash table is a bonus part of the code that I'll try implementing at a later time. Thank you for your help!
typedef struct name_val // holds name and value
{
char *name;
double value;
}NAME_VAL;
typedef struct node //binary search tree
{
NAME_VAL *nV;
struct node *left;
struct node *right;
}NODE;
struct tmap_struct //handle for bst and hashtable
{
int nL; //nodes on left
int nR; //nodes on right
NODE *root;
NODE **table;
};
int tmap_insert(TMAP_PTR hashTree, char * name, double val)
{
if(hashTree->root == NULL)
{
NODE *bst = (NODE *)malloc(sizeof(NODE));
NAME_VAL *root = (NAME_VAL *)malloc(sizeof(NAME_VAL));
bst->nV = root;
bst->nV->value = val;
strcpy(bst->nV->name, name);
hashTree->root = bst;
hashTree->nL = 0;
hashTree->nR = 0;
}
else
insertHelper(hashTree->root, val, name);
}
void insertHelper(TMAP_PTR hashTree, int val, char * name)
{
if(val < hashTree->root->nV->value)
{
if(hashTree->root->left == NULL)
{
hashTree->root->left = (NODE *)malloc(sizeof(NODE));
hashTree->root->left->nV = (NAME_VAL *) malloc(sizeof(NAME_VAL));
strcpy(hashTree->root->left->nV->name, name);
hashTree->root->nV->value = val;
(hashTree->nL)++;
}
else
insertHelper(hashTree->root->left, val, name);
}
else
{
if(hashTree->root->right == NULL)
{
hashTree->root->right = (NODE *)malloc(sizeof(NODE));
hashTree->root->right->nV = (NAME_VAL *)malloc(sizeof(NAME_VAL));
strcpy(hashTree->root->left->nV->name,name);
hashTree->root->nV->value = val;
(hashTree->nR)++;
}
else
insertHelper(hashTree->root->right, val, name);
}
}
I doubt this compiles. Is that the problem you're having?
From what I can see, you have declared insertHelper with the wrong type for its first parameter. It should take NODE* values, not TMAP_PTR values. That's because you always call it with nodes out of your tree.
So the first part of the function should look like this:
void insertHelper(NODE *node, int val, char * name)
{
if(val < node->nV->value)
{
if(node->left == NULL)
{
node->left = (NODE *)malloc(sizeof(NODE));
node->left->nV = (NAME_VAL *) malloc(sizeof(NAME_VAL));
strcpy(node->left->nV->name, name);
node->left->nV->value = val;
}
else
insertHelper(node->left, val, name);
}
//.....
Note that I removed the line:
(hashTree->nR)++;
It hardly even makes sense to track this information, unless maybe you do it at the node level.
But if you must, you could have insertHelper recursively return a positive or negative value to indicate what side it inserted on. But that doesn't makes sense. What is it on the right of? You may have inserted it on the right of a node that was in the left half of the tree.
If you store this information on each node, you can recursively update the node above as you return from insertHelper. Maybe that's what you were trying to do. Balanced tree implementations do something similar - AVL trees store the maximum depth of the tree at a node and use that to do branch rotations for rebalancing.
You'll have to adapt mine(It's almost standard C besides the unneeded template and class), but it's a similar algorithm: (I believe, I didn't look at any source for my own purposes.)
template<typename T>
class BST {
protected:
typedef struct node_t {
struct node_t * dir[2];
T data;
} node;
node * root;
void insert_node(node * active_node, T data){ //call with node *root;
int next = data < active_node->data ? 0 : 1;
if(active_node->dir[next] == NULL){
active_node->dir[next] = new node;
active_node->dir[next]->dir[0] = NULL;
active_node->dir[next]->dir[1] = NULL;
active_node->data = data;
} else
insert_node(active_node->dir[next], data);
}
public:
BST() : root(new node){root->dir[0] = NULL; root->dir[1] = NULL; root->data = 0;}
~BST(){}
}
I have to code some methods for a BST and I have some problems, let me explain.
I have the following structures :
struct node {
struct node *lChild;
struct node *rChild;
int value;
};
and
struct tree {
struct node *root;
};
along with the following functions :
struct tree* constructNewTree()
{
struct tree *T=malloc(sizeof(struct tree));
T->root=NULL;
return T;
}
and
struct node* constructNewNode(int i)
{
struct node *N=malloc(sizeof(struct node));
N->value=i;
N->lChild=NULL;
N->rChild=NULL;
return N;
}
And in my main I must call this (for example) :
int main()
{
struct tree *T;
T=constructNewTree();
insertKey(5,T);
insertKey(2,T);
insertKey(9,T);
return 0;
}
What I have to do is to create the function insertKey(int i, struct tree *T) using the recursion.
I wanted to do something like
void insertKey(int i, struct tree *T)
{
if (T->root==NULL) {
T->root=constructNewNode(i);
return;
}
else {
if (i<=T->root->value) {
T->root->lChild=constructNewNode(i);
else if (i>T->root->value) {
T->root->rChild=constructNewNode(i);
}
}
}
But it doesn't get very far, using the recursion would allow me to call insertKey again but I can't seem to use a node and a tree the same way.
Does anyone know how I could do that without altering the given structures?
Thank you very much.
Your insertKey takes a Tree as its argument. A Tree is only a pointer to the very top.
What I recommend you do is write a insertKey function that takes a Node for its argument. Also in this function, you have to check to see if there is another tree on the left/right child.
Currently you just construct a new node regardless of what is there. This will overwrite any previous insertions.