Hiện tại em có 1 lỗi là không xuất được các node còn lại khi xóa 1 node trường hợp node đó là lá. ai fix dùm em với .
Full code của em
Mã:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
// khai bao cau truc node
struct NODE
{
int info;
NODE* pLeft;
NODE* pRight;
}node;
// cai dat cay tim kiem nhi phan
typedef NODE *Tree;
// khoi tao 1 cay nhi phan
void khoitaocay(Tree &T)
{
T = NULL;
}
// kiem tra cay
int kiemtracay(Tree &T)
{
if ( T == NULL)
return 1;
return 0;
}
// cap phat bo nho cho cay
Tree Newnode()
{
Tree newnode;
newnode = (Tree)malloc(sizeof(NODE));
return newnode;
}
// giai phong bo nho
void freenode(Tree &T)
{
delete(T);
}
// khoi tao 1 node la x
Tree khoitaonode(int x)
{
Tree node = (Tree)malloc(sizeof(NODE));
node->info = x;
node ->pLeft = node->pRight = NULL;
return node;
}
// them 1 node vao cay
void add_node(Tree &T, int x)
{
if(kiemtracay(T) == 1)
{
Tree p = khoitaonode(x);
T = p;
}
else
if(x!=T->info)
{
{
if ( x > T->info)
add_node(T->pRight,x);
else
add_node(T->pLeft,x);
}
}
else
{
printf ("
Gia tri ban nhap da ton tai");
getch();
return;
}
}
// nhap gia tri cho cay
void input_tree(Tree &T)
{
int n;
int x;
printf("Nhap so luong phan tu: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Nhap phan tu thu %d: ",i+1);
scanf("%d",&x);
add_node(T,x);
}
}
// duyet giua
void output_LNR(Tree &T)
{
if(!kiemtracay(T))
{
output_LNR(T->pLeft);
printf ("%3d",T->info);
output_LNR(T->pRight);
}
}
void delete_x(Tree &temp)
{
int x;
Tree temp1 = temp;
printf ("
Nhap x: ");
scanf ("%d",&x);
while( temp != NULL)
{
if ( x == temp->info) // truong hop x la nut la
{
if ( temp->pLeft == NULL && temp->pRight == NULL);
delete(temp);
}
}
output_LNR(temp1);
}
void main()
{
Tree T;
khoitaocay(T);
input_tree(T);
output_LNR(T);
delete_x(T);
getch();
}
còn đây là hàm xóa 1 node trường hợp là lá của em
Mã:
void delete_x(Tree &temp)
{
int x;
Tree temp1 = temp;
printf ("
Nhap x: ");
scanf ("%d",&x);
while( temp != NULL)
{
if ( x == temp->info) // truong hop x la nut la
{
if ( temp->pLeft == NULL && temp->pRight == NULL);
delete(temp);
}
}
output_LNR(temp1);
}
Em xin cảm ơn nhiều .
View more random threads:
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...