Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Lỗi hàm xóa 1 node trong cây nhị phân tìm kiếm không dùng đệ quy

    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 .

  2. #2
    Không có ai giúp em à .

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •