I can't make my code just to show the tree size.
Something like: left -1, right 1, root 0. I've tried to use a few codes that i found here but none has worked... :/
Code:
#include <conio.h>#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
struct Node *left;
struct Node *right;
int value;
} Node;
void insertNode(Node **tree,int element)
{
if(*tree == NULL)
{
Node *aux = (Node *)malloc(sizeof(Node));
aux->value = element;
aux->right= NULL;
aux->left = NULL;
*tree = aux;
printf("The value %d has been inserted.\n",element);
return;
}
if(element < (*tree)->value)
{
insertNode(&(*tree)->left,element);
return;
}
if(element > (*tree)->value)
{
insertNode(&(*tree)->right,element);
return;
}
printf("The value %d is already in the tree.\n",element);
}
Node *Removtwo(Node *two)
{
if(two==NULL)
return NULL;
else if(two->left == NULL)
return Removtwo(two->right);
else
return Removtwo(two->left);
}
void removerNode(Node **tree,int element)
{
if(element < (*tree)->value)
{
removerNode(&(*tree)->left,element);
}
else if(element > (*tree)->value)
{
removerNode(&(*tree)->right,element);
}
else if((*tree)->left!=NULL && (*tree)->right!=NULL)
{
Node *aux= NULL;
aux = Removtwo((*tree)->right);
(*tree)->value = aux->value;
removerNode(&(*tree)->right,(*tree)->value);
}
else
{
Node *aux = (*tree);
if((*tree)->left==NULL)
{
(*tree) = (*tree)->right;
}
else
{
*tree = (*tree)->left;
}
printf("The value %d was sucessfull removed.\n",element);
free(aux);
}
}
void Print(Node *tree)
{
if(tree == NULL)
{
return;
}else
Print(tree->left);
printf("%d->",tree->value);
Print(tree->right);
}
int main()
{
int num,insertion,removing;
Node *tree = NULL;
while(num != 4)
{
printf("-------------------------------------\n");
printf("------ 1. Insert ------------\n");
printf("------ 2. Remove ------------\n");
printf("------ 3. Print Tree -----------\n");
printf("------ 4. Exit ------------\n");
printf("-------------------------------------\n");
scanf("%d", &num);
system("cls");
switch(num)
{
case 1:
{
printf("Put the value: ");
scanf("%d",&insertion);
insertNode(&tree,insertion);
getch();
system("cls");
break;
}
case 2:
{
printf("Put the value: ");
scanf("%d",&removing);
removerNode(&tree,removing);
getch();
system("cls");
break;
}
case 3:
{
Print(tree);
getch();
system("cls");
break;
}
case 4:
{
system("cls");
break;
}
}
}
}