Chào, Khách!
  1. conloc171
    Offline
    conloc171   [29/2]
    Đây là code tạo cây của em nhưng nó không chạy, f9 nó báo lỗi thế này: Undefined symbol 'root'. Em không biết sửa như thế nào. Mong được anh chị giúp đỡ
    Mã:
    #include<conio.h>
    #include<stdio.h>
    #include<iostream.h>
    #include<string.h>
    typedef int TData;
    typedef struct TNode{
            TData Data;
            TNode* left;
            TNode* right;
            };
    typedef TNode* TTree;
    void chen(int e, TNode **root)
    {if ((*root) == NULL) 
        {
               (*root) = new TNode;
               (*root)->Data = e;
               (*root)->left = (*root)->right = NULL;
        }
     else
    if (e < (*root)->Data)
            chen(e, &(*root)->left);
    else
        if(e > (*root)->Data)
            chen(e, &(*root)->right);
        else printf("nut nay da ton tai");
    }
    void PreOrder(TTree T)
    {TTree p;
        p=T;
        if(p!=NULL)    {
            printf("%d ",p->Data);
            PreOrder(p->left);
            PreOrder(p->right);
        }
    }
    
    void InOrder(TTree T)
    {TTree p;    p = T;
        if(p!=NULL){
            InOrder(p->left);
            printf("%d ",p->Data);
            InOrder(p->right);
        }
    }
    void PosOrder(TTree T)
    {TTree p;    p = T;
        if(p != NULL){
            PosOrder(p->left);
            PosOrder(p->right);
            printf("%d ",p->Data);
        }
    }
    void main()
    {
        TTree T;
                int e;
        char ch,c;
        clrscr();
        do
        {
            printf("\n nhap phan tu cho cay:");
            cin>>e;
            chen(e,*root);                      Báo lỗi sai tại đây
            printf("\n k thoat,c tiep tuc:");
            cin>>ch;
        }
        while(ch!='k');
        cout<<"TIEN TO:   ";
        PreOrder(T);cout<<"\n";
        cout<<"TRUNG TO:  ";
        InOrder(T); cout<<"\n";
        cout<<"HAU TO:    ";
        PosOrder(T);cout<<"\n";
        getch();
    }
  2. integer
    Offline
    integer   [1.695/1.313]
    Bạn nhớ để vào thẻ Code cho dễ nhìn nhé.
    Bài trên bạn tạo TTree mà lại truyền root cho nó. root là biến dùng trong chuơng trình con mà.

    Tạo TTree thế này
    TTree *T =new TTree;
    rồi truyền vào chương trình con
    chen(e,&T);
    Code dưới đây bỏ vào thẻ và sửa 1 số chỗ, còn 1 số bạn sửa tiếp cho chạy hoàn thiện;
    Mã:
    #include<conio.h>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    typedef int TData;
    typedef struct TNode
    {
        TData Data;
        TNode* left;
        TNode* right;
    };
    typedef TNode* TTree;
    
    void chen(int e, TNode **root)
    {
        if ((*root) == NULL)
        {
            (*root) = new TNode;
            (*root)->Data = e;
            (*root)->left = (*root)->right = NULL;
        }
        else if (e < (*root)->Data)
            chen(e, &(*root)->left);
        else if(e > (*root)->Data)
            chen(e, &(*root)->right);
        else printf("nut nay da ton tai");
    }
    void PreOrder(TTree T)
    {
        TTree p;
        p=T;
        if(p!=NULL)
        {
            printf("%d ",p->Data);
            PreOrder(p->left);
            PreOrder(p->right);
        }
    }
    
    void InOrder(TTree T)
    {
        TTree p;
        p = T;
        if(p!=NULL)
        {
            InOrder(p->left);
            printf("%d ",p->Data);
            InOrder(p->right);
        }
    }
    void PosOrder(TTree T)
    {
        TTree p;
        p = T;
        if(p != NULL)
        {
            PosOrder(p->left);
            PosOrder(p->right);
            printf("%d ",p->Data);
        }
    }
    int main()
    {
        TTree *T =new TTree;
        int e;
        char ch,c;
        //clrscr();
    
        do
        {
            printf("\n nhap phan tu cho cay:");
            cin>>e;
            chen(e,*T);
            printf("\n k thoat,c tiep tuc:");
            cin>>ch;
        }
        while(ch!='k');
        cout<<"TIEN TO: ";
        PreOrder(*T);
        cout<<"\n";
        cout<<"TRUNG TO: ";
        InOrder(*T);
        cout<<"\n";
        cout<<"HAU TO: ";
        PosOrder(*T);
        cout<<"\n";
        //getch();
    }
    
    
    
    killyou117, conloc171, interpol1 người khác thích bài này.
  3. conloc171
    Offline
    conloc171   [29/2]
    Anh sửa giúp em hoàn chỉnh code được ko, thật sự em ko hiểu rỏ code này lắm
  4. mizz1233
    Offline
    mizz1233   [6/0]
    Code CÂY NHỊ PHÂN

    Mã:
    #include<stdio.h>
    #include<conio.h>
    typedef int tdata;
    typedef struct tnode{
        tdata data;
        tnode* left;
        tnode* right;
            };
    typedef tnode*ttree;
    void maketree(ttree*t)
    {
        (*t)=NULL;
    }
    int  etree(ttree t)
    {
    return t ==NULL;
    }
    ttree insert(tdata v,ttree l,ttree r)
    {ttree n;
    n=new tnode;
    n->data=v;
    n->left=l;
    n->right=r;
    return n;
    }
    void preorder(ttree t)
    {
        ttree p;
        p=t;
        if(p!=NULL){
            printf("%5d",p->data);
            preorder(p->left);
            preorder(p->right);
            }
    }
    void inorder(ttree t)
    {
        ttree p;
        p=t;
        if(p!=NULL)
            {
            inorder(p->left);
            printf("%5d",p->data);
            inorder(p->right);
            }
    }
    void posorder(ttree t)
    {
        ttree p;
        p=t;
        if(p!=NULL)
            {
            posorder(p->left);
            printf("%5d",p->data);
            posorder(p->right);
            }
    }
    void main()
    {
    ttree t;
    t=insert(1,insert(2,insert(3,NULL,
        insert(4,NULL,insert(5,NULL,
        insert(6,NULL,NULL)))),NULL),NULL);   //Day la cay
        printf("TIEN TO  :");
        preorder(t);
        printf("\n");
        printf("TRUNG TO :");
        inorder(t);
        printf("\n");
        printf("HAU TO   :");
        posorder(t);
        printf("\n");
        getch();
        }
    
  5. conloc171
    Offline
    conloc171   [29/2]
    Tạo cây theo thuật toán, chứ không phải đưa ra cây có sẵn đâu. Nghỉa là mình nhập các giá trị số vào, rồi theo thuật toán tạo thành 1 cây
  6. Xautrai_LT03B
    Offline
    Xautrai_LT03B   [38/15]
    Mình có code mới viết nhưng do thời gian bận quá không thể xem bài bạn được nên bạn lấy cái này xem qua đi