Here is the code so far but i know that is incorrect.... I can't find how to make it in C this pseudocode and binary tree confuses me more....
This pseudocode send messages to neighbours but I can't do it in the binary tree.....
How on binary tree find last branch and check how many leaves this branch have and after send a virtual message to parent?
This pseudocode will run in a machine and will be only an emulator and maybe this confuses me more in order to make it work....
Code:
#include <stdio.h>
#include<stdlib.h>
#include <stdbool.h>
typedef struct bnode{
int id;
char* id2;
struct bnode* left;
struct bnode* right;
}BNODE;
void printKeysReverse(BNODE* current);
void inorder(BNODE* current);
void insert(BNODE **root,int id,char* id2);
int main(void){
BNODE* root = NULL;
int i,wr,nodes;
bool ws,p;
int state[2]; //1=sleep,2=leader,3=lost
p = true;
insert(&root,27,"fghfhfgh");
insert(&root,63,"225");
insert(&root,3,"225");
printf("%d\n",size(root));
if(p){
ws = true;
wr = 0;
for(i=1;i<=size(root);i++){ //forall q ∈ Neigh do send <wakeup> to q;
//send wakeup neighbours
}
while (wr<size(root)){
//receive wakeup
wr++;
if (!ws){
ws=true;
for(i=1;i<=size(root);i++){
//send wakeup
}
}
}
/* Now start the tree algorithm */
/* while #{q: ¬rec[q]} >1 do
begin receive <tok,r> from q;
rec[q]:=true;
v:=min(v,r)
end;
send <tok, v> to q0 with ¬rec[q0];
receive <tok,r> from q0;
v:=min(v,r);
if v=p then state:=leader else state:=lost;
forall q ∈ Neigh, q≠q0 do send <tok,v> to q
end*/
//printf("---------\n");
//printf("---------\n");
//inorder(root);
}
}
//--------------------------FUNCTIONS------------------------------//
void insert(BNODE **root, int val, char* val2){
BNODE *newnode;
newnode=(BNODE*)malloc(sizeof(BNODE));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
*root=newnode;
(*root)->id=val;
(*root)->id2=val2;
return;
}
if (val<(*root)->id) insert(&(*root)->left,val,val2);
else insert(&(*root)->right,val,val2);
}//end
void inorder(BNODE *root){
if (root==NULL) return ;
inorder(root->left);
printf("%d,%s \n",root->id,root->id2);
inorder(root->right);
}//end inorder
int size(BNODE *root)
{
if (root == NULL)
return 0;
else
return 1 + size(root->left) + size(root->right);
}