0% found this document useful (0 votes)
22 views8 pages

Binary Tree Operations in C

The document contains a C program that implements a binary search tree with various functionalities including adding nodes, traversing the tree in different orders, searching for a value, counting prime numbers, finding the maximum and minimum values, and deleting a node. It provides a menu-driven interface for user interaction to perform these operations. The program utilizes recursive functions for tree manipulation and traversal.

Uploaded by

trungbadu54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Binary Tree Operations in C

The document contains a C program that implements a binary search tree with various functionalities including adding nodes, traversing the tree in different orders, searching for a value, counting prime numbers, finding the maximum and minimum values, and deleting a node. It provides a menu-driven interface for user interaction to perform these operations. The program utilizes recursive functions for tree manipulation and traversal.

Uploaded by

trungbadu54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node{
int data;
struct node*left ;
struct node*right;
};
typedef struct node node;

void themnode(node**t,int x){


if((*t)==NULL ){

node*p=(node*)malloc(sizeof(node));
p->data=x;
p->left=NULL;
p->right=NULL ;
*t=p; }
else {
if ((*t)->data > x) {
themnode(&((*t)->left), x);
} else if ((*t)->data < x) {
themnode(&((*t)->right), x);
}
}
}
void duyetNLR(node*t){
if(t!=NULL){
printf("%d\n",t->data);
duyetNLR(t->left);
duyetNLR(t->right);
}
}
void duyetNRL(node*t){
if(t!=NULL){
printf("%d\n",t->data);
duyetNRL(t->right);
duyetNRL(t->left);

}
}
//DUYET SAP XEP TU BE DEN LON
void duyetLNR(node*t){
if(t!=NULL){
duyetLNR(t->left);
printf("%d\n",t->data);
duyetLNR(t->right);

}
}
//DUYET SAP XEP TU LON DEN BE
void duyetRNL(node*t){
if(t!=NULL){
duyetRNL(t->right);
printf("%d\n",t->data);
duyetRNL(t->left);
}
}
node* timkiem(node* t, int x) {
if (t == NULL) {
return NULL;
} else {
if (t->data == x) {
return t;
} else if (t->data < x) {
return timkiem(t->right, x);
} else {
return timkiem(t->left, x);
}
}
}

int snt(int x){


if(x<2){
return 0;
}
for(int i=2;i<sqrt(x);i++){
if(x%i==0){
return 0;
}
else
return x>1;
}
}

int demsnt(node**t,int &cnt){


if((*t)!=NULL){
if(snt((*t)->data)){

cnt++;}
demsnt(&(*t)->left,cnt);
demsnt(&(*t)->right,cnt);
}
return cnt;
}
//tim so lon nhat
int max=-10000001 ;
int timmax(node*t){
if (t!= NULL) {

if (max<t->data) {
max =t->data;

}
timmax(t->right);
timmax(t->left);
}
return max;
}
//tim so nho nhat
int min=10000001 ;
int timmin(node*t){
if (t!= NULL) {
if (min>t->data) {
min =t->data;

}
timmin(t->right);
timmin(t->left);
}
return min;
}
//tim so lon nhat cach 2

int timmax2(node*t)
{
if(t->left==NULL||t->right==NULL){
return t->data;
int max =t->data;
}

if(t->right!=NULL){
int maxright=timmax2(t->right);
if(max< maxright){
max=maxright;

}
}
return max;
}
node* xoanode(node* root, int x) {
if (root == NULL)
return root;

if (x < root->data)
root->left = xoanode(root->left, x);
else if (x > root->data)
root->right = xoanode(root->right, x);
else {

if (root->left == NULL) {
node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
node* temp = root->left;
free(root);
return temp;
}
}
}

int main (){


node*t=NULL;
while(1){
printf("....................menu......................\n") ;
printf("[Link] node\n");
printf("2 duyet node\n") ;
printf("[Link]\n");
printf("[Link] kiem\n") ;
printf("[Link] max\n");
printf("[Link] phan tu\n") ;
printf("[Link]\n");
printf(".................................................\n") ;
int lc;
printf("nhap lc:");
scanf("%d",&lc);
if(lc==1){
int x;
printf("nhap gia tri:\n ") ;
scanf("%d",&x);
themnode(&t,x);
}
else if(lc==2){
duyetNLR(t);
}
else if(lc==3){
int cnt=0;
printf("%d",demsnt(&t,cnt));}
else if(lc==4){
int x;
printf("nhap gia tri can tim:\n ") ;
scanf("%d",&x);
if(timkiem(t,x)==NULL){
printf("ko co phan tu can tim\n");
}
else
printf("phan tu co ton tai trong cay\n ") ;
}
else if(lc==5){
printf("%d\n",timmax2(t));
}
else if(lc==6) {
int x;
printf("nhap gia tri can xoa:");
scanf("%d",&x) ;
xoanode(t,x) ;
}

else
return 0 ;
}
}

You might also like