Save C binary tree root value in variable - c

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

Related

Call rotate function in AVL Tree C

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?

How to insert a linked list at node of binary search tree?

I would like to populate a linked list which is in a node of a binary search tree.
If the user already exits in the list add the ip to the linked list for that specific user.
This is what I have tried so far:
My Data Structures:
typedef struct ip{
int ip;
struct ip *ipNext;
}IP;
typedef struct bstNode
{
char data[32];
struct bstNode* left;
struct bstNode* right;
IP *ipHead; //Pointer to linked list.
}bstNode;
This is where I am having issues
My insert function to insert the IP address into the list for user:
bstNode insertIP(bstNode *head, char *username, int ip){
if (search(username)==1)
{
if (head->ipHead == NULL)
{
IP *temp;
temp = (IP*)malloc(sizeof(IP));
temp->ip = ip;
temp->ipNext = NULL;
}else{
head->ipHead->ip= ip;
head->ipHead->ipNext=NULL;
}
}
Insert Function (This works):
bstNode *insert(bstNode *node, char *word)
{
if(node==NULL){
node= malloc(sizeof(bstNode));
//IP* ipNode=malloc(sizeof(IP));
strcpy(node->data, word);
node->left=NULL;
node->right=NULL;
}
else{
if(strcmp(word, node->data)<0)
node->left=insert(node->left, word);
else if(strcmp(word, node->data)>0)
node->right=insert(node->right, word);
}
return node;
}
Search Function (This works):
void search(char* user, bstNode* root)
{
int res;
if( root!= NULL ) {
res = strcmp(root, root->data);
if( res < 0)
search( user, root->left);
else if( res > 0)
search( user, root->right);
else
printf("User Found\n");
return 1;
}
else printf("\nNot in tree\n");
return 0;
}
1st issue: when you create temp you dont assign it to the head->ipHead.
2nd issue: when ipHead exists you still have te allocate memory for the new list node and have to iterate over the whole list if you want to add it at the end. You can also add it at the begining of the list.
3rd issue: the searching function should return the node in the tree if it found the username. I guess that you dont want to always add new list nodes tho the first tree node. I suggest that you traverse the tree using local variable in function - not recursion.
When you find that user already exist in the tree, then first you have to store that bstNode which contains the given user. After that you would have to traverse the linked list of that bstNode to append the ip at the last. There is one more problem with you code in case where the linked link is NULL. You would have to assign the new node address to the ipHead. Currently you have creating a temp node and not assigning that to head.
Recently I coded a binary search tree in which each tree node hosts a linked list (list nodes are singly linked) and I used the following data structure (if someone needs the whole code, let me know).
// data in each
// list node
struct data_info
{
//body of data
};
typedef struct data_info Item;
// list node
typedef struct lnode
{
Item data_item;
struct lnode * next;
} Lnode;
// List points to
// first list node
typedef Lnode * List;
// tree node contains
// linked list
typedef struct trnode
{
List list_item;
struct trnode * left; // pointer to right branch
struct trnode * right; // pointer to left branch
} Trnode;
// tree of linked lists
typedef struct tree
{
Trnode * root; // pointer to root of tree
int size; // number of list nodes in tree
} Tree;

Delete node from a C binary tree without messing it up

I am a beginner working on a C binary tree library.I am wondering on how could I delete a node from a binary tree without messing up the entire thing.Here is how I create the tree:
the structure:
struct Node {
int value;
struct Node *left;
struct Node *right;
};
typedef struct Node TNode;
typedef struct Node *binary_tree;
Creation of the tree:
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;
}
Adding elements to it:
void Insert(binary_tree *tree, int val) {
if (*tree == NULL) {
*tree = (binary_tree)malloc(sizeof(TNode));
(*tree)->value = val;
(*tree)->left = NULL;
(*tree)->right = NULL;
} else {
if (val < (*tree)->value) {
Insert(&(*tree)->left, val);
} else {
Insert(&(*tree)->right, val);
}
}
}
my question is basically how I could for example delete a left node,and then "link" the other nodes(or leaves) that were linked to that left node so the tree doesnt have a NULL leaf? Ex: if leaf 4 had 2 children(left3 and right8),then delete leaf 4, it link children left3 and right8 to the upper node(above leaf4).Its hard to explain im trying to do my best.
thank you
The algorithm for deleting from a BST looks conceptually like this:
You search for the node using its key.
Once you have found the node you check if it has only one child.
if it does, you remove the node and put on its place the child you just found.
if it doesn't, you search for the node with the minimum value key in the right subtree. Once you find it, you replace the key of the node you want to delete with this minimum key and you delete the minimum node in the right subtree.
How this whole concept works and how should it look in C code you can read for example here. My suggestion would be to first see this diagram, which illustrates all the possible scenarios.
Good luck!

Insert value to binary tree root in C

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;
}

BST with a recursion and given structures

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.

Resources