patterncMinor
Treap implementation in C
Viewed 0 times
implementationtreapstackoverflow
Problem
I was needed a SET-like data structure written in pure C for some university class, so I've implemented a simple one - the Treap (or cartesian tree).
Please check if everything is okay (actually, I'm not sure that there isn't a memory leak there).
cartesian_tree.h
cartesian_tree.c
```
#include
#include
#include
#include "cartesian_tree.h"
pNODE construct_tree(void) {
srand(time(0)); / treap is a randomized data structure, remember? /
pNODE root = (pNODE)malloc(sizeof(NODE));
root->key = root->priority = LONG_MIN;
root->assoc = NULL;
root->left = root->right = NULL;
return root;
}
char insert(pNODE root, long key, char* assoc) {
/ nope, Mr. Duplicate, we don't wanna see you at our party /
if(find(root, key))
return -1;
/ constructing a new node /
pNODE fresh_node = (pNODE)malloc(sizeof(NODE));
fresh_node->key = key;
fresh_node->priority
Please check if everything is okay (actually, I'm not sure that there isn't a memory leak there).
cartesian_tree.h
#ifndef _CARTESIAN_TREE_H_
#define _CARTESIAN_TREE_H_
#define NODE struct Node
#define pNODE struct Node*
#define ppNODE struct Node**
/* Each node of the tree has the following structure: */
struct Node {
long key, priority; /* priority is a kind of `technical' information */
char* assoc; /* pointer to a user's data (imagine MAP) */
NODE *left, *right; /* links to the left and right child */
};
/* Returns a pointer to the tree consisting only from the root */
pNODE construct_tree(void);
/* Inserts new element to the tree.
Returns 0 if there was no such element in tree and -1 otherwise
(in such case, nothing is inserted) */
char insert(pNODE, long, char*);
/* Erases a node with a particular key.
returns 0 if an element with key `key' was deleted
and -1 otherwise. */
char erase(pNODE, long);
/* Returns a pointer to node with the particular key
or NULL if there is no such node. */
pNODE find(pNODE, long);
/* Clean up. One should call this function every time
when the tree isn't needed no more. */
void destruct_tree(pNODE);
#endifcartesian_tree.c
```
#include
#include
#include
#include "cartesian_tree.h"
pNODE construct_tree(void) {
srand(time(0)); / treap is a randomized data structure, remember? /
pNODE root = (pNODE)malloc(sizeof(NODE));
root->key = root->priority = LONG_MIN;
root->assoc = NULL;
root->left = root->right = NULL;
return root;
}
char insert(pNODE root, long key, char* assoc) {
/ nope, Mr. Duplicate, we don't wanna see you at our party /
if(find(root, key))
return -1;
/ constructing a new node /
pNODE fresh_node = (pNODE)malloc(sizeof(NODE));
fresh_node->key = key;
fresh_node->priority
Solution
-
It's preferred to only call
-
You don't need to call
-
The indentation in
I would also refactor the loop to use fewer conditional statements. It's not only difficult to follow the flow, especially with few comments, but you could also experience a reduction in performance due to branch prediction (this may not be a problem, especially with modern architecture, but it's still something worth keeping in mind).
It's preferred to only call
srand() once in main(). It's easier to maintain in this way as it'll avoid the possibility of calling srand() multiple times, which will give you the same random value with rand() each time. It should only be called once.-
You don't need to call
exit() in main() since it is supposed to return an integer value related to the program's execution outcome. Just return 0 or 1.-
The indentation in
main()'s while(1) loop is a bit inconsistent. Statements under an if or else should still be indented.I would also refactor the loop to use fewer conditional statements. It's not only difficult to follow the flow, especially with few comments, but you could also experience a reduction in performance due to branch prediction (this may not be a problem, especially with modern architecture, but it's still something worth keeping in mind).
Context
StackExchange Code Review Q#49582, answer score: 5
Revisions (0)
No revisions yet.