Im a beginner in C programming and im trying to do a binary tree c library.
heres my binary tree struct:
#include <stdio.h>
struct Noeud
{
int valeur ;
struct Noeud* gauche ;
struct Noeud* droit ;
};
typedef struct Noeud TNoeud;
typedef struct Noeud* TArbre;
Heres the way I create it
TArbre NouvelArbreVide( void )
{
return NULL;
}
However i would wonder on how to put a value to the root of the tree like
TArbre NouvelArbreVide(int value_root)
{
return NULL;
}
that would put the value_root value to the binary tree root.Im not sure on how to do that even though its probably very basic.
thank you
To start your tree with a single node, you want to allocate a new root like this:
TArbre NouvelArbreVide(int value_root)
{
TArbre newRoot = malloc(sizeof(TNoeud));
if (newRoot)
{
newRoot->valeur = value_root;
newRoot->gauche = NULL;
newRoot->droit = NULL;
}
return newRoot;
}
Related
I have this code to rightRotate a sub tree :
void rightRotate(mynode **parentPtr, mynode *child)
{
// make sure the arguments are valid for a right rotation
assert(parentPtr != NULL && child != NULL && child->left != NULL);
// save the three node addresses involved in the rotation
mynode *F = child;
mynode *D = F->left;
mynode *E = D->right;
// perform the rotation
*parentPtr = D;
D->right = F;
F->left = E;
}
Tree struct is :
typedef struct tree mynode; // differences between nametag and type????
struct tree{ // tree node struct
int value;
int key;
char color;
struct tree *left;
struct tree *right;
};
My question is : How should I call this function ?
Example : I have a tree and I want to right rotate son, since the first argument is a pointer to pointer to node and the second a pointer to node how should I write the call?
//...some code...
//I have two pointers to nodes : parent and son
rightRotate ( ??? , son)// I didn't get the first argument, what I should write?
//some code...
P.S. : This code is from an other post, sorry for this little "spam" but I'm very confused about this simple function and why it works in this way. Can someone explain me a little more?
Im working on a C library for binary tree, and Im wondering how I could save the value of the root of the tree to be able to display it later in a function.
My tree struct:
struct Node {
int value;
struct Node *left;
struct Node *right;
};
typedef struct Node TNode;
typedef struct Node *binary_tree;
The binary tree root is initialised with the 3 value like this:
caller:
tree = NewBinaryTree(3);
NewBinaryTree method:
binary_tree NewBinaryTree(int value_root) {
binary_tree newRoot = malloc(sizeof(TNode));
if (newRoot) {
newRoot->value = value_root;
newRoot->left = NULL;
newRoot->right = NULL;
}
return newRoot;
}
Basically I would like to be able to do a function that just display the value_root function even after adding elements to the binary tree and be able to still display the value_root value.This might be very basic but im learning C and im not sure.
thank you
In the caller: printf( "%d\n", tree->value )
– user3386109
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 am working for a C exam and while trying to insert an element to a linked list, I am encountering with a runtime problem. My only purpose is adding 4 elements to list and then printing the list. However, it gives an error. I already looked some insertion codes and my code seems right. Can't see the error. Any assistance would be appreciated.
#include <stdio.h>
#include <stdlib.h>
struct ders{
char kod;
struct ders *next;
}*header;
typedef struct ders Ders;
void add(Ders*,Ders*);
void print(Ders*);
int main(void)
{
header = NULL;
Ders *node = NULL;
int i = 0;
char c;
while(i<4)
{
scanf("%c",&c);
node = (Ders*)malloc(sizeof(Ders));
node->kod = c;
node->next = NULL;
add(header,node );
i++;
}
print(header);
return 0;
}
void add(Ders *header, Ders *node)
{
if(header == NULL){
header = node;
header->next = NULL; }
else{
node->next = header;
header = node;
}
}
void print(Ders *header)
{
Ders *gecici = header;
while(gecici != NULL){
printf("%c\n",gecici->kod);
gecici = gecici->next;
}
}
As nihirus stated,
"The pointer is passed by value. Thus you can change the memory it points but you can't change the actual pointer, i.e. make it point to something else."
Your modification resulted in error *header is not member of struct
because
->
has a higher precedence than
*
Try using
(*header)->next = NULL
instead.
C operator precedence:
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm
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.