Skip to content

Instantly share code, notes, and snippets.

@phuctu1901
Created May 4, 2017 14:20
Show Gist options
  • Save phuctu1901/44ef800b99bfd0f21bec582b66ccdd75 to your computer and use it in GitHub Desktop.
Save phuctu1901/44ef800b99bfd0f21bec582b66ccdd75 to your computer and use it in GitHub Desktop.
#include "stdio.h"
#include "stdlib.h"
struct NODE
{
int Data;
struct NODE *left, *right;
};
typedef struct NODE *NODEPTR;
void Insert (NODEPTR *t, int x)
{
if (!*t)
{
*t=(NODEPTR)malloc(sizeof(struct NODE));
(*t)->Data=x;
(*t)->left=(*t)->right=NULL;
}
else (x>(*t)->Data)?Insert(&((*t)->left), x): Insert(&((*t)->right),x);/*Thay the "<" voi yeu cau sau*/
}
void InTree(NODEPTR *t)
{
int x;
printf("Nhap so 0 de dung\n");
do
{
scanf("%d",&x);
if (x) Insert(t,x);
}while(x);
}
void OutTree(NODEPTR t, char label, int d)
{
if (t)
{
OutTree(t->right, 'L', d+1);
printf("%*c[%c%d]\n",2*d, ' ', label, t->Data );
OutTree(t->left, 'R', d+1);
}
}
int main()
{
NODEPTR t=NULL;
InTree(&t);
printf("\nCay goc: \n");
OutTree(t, '*', 1);
printf("Nhap vao gia tri can chen: ");
int x;
scanf("%d", &x);
Insert(&t, x);
printf("\nCay goc: \n");
OutTree(t, '*', 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment