Chào, Khách!
  1. conloc171
    Offline
    conloc171   [29/2]
    Bên dưới là code cấu trúc cây. Em tạo cây chữ cái nhưng sao xuất ra màn hình toàn mấy kí tự bậy bạ.Mọi người xem sửa giúp em cái. Cho em xin code cây xuất chuổi kí tự với sinh viên luôn thì càng tốt . Thank mọi người trước
    Mã:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream.h>
    typedef char TData;
    typedef struct TNode{
            TData Data;
            TNode* left;
            TNode* right;
            };
    typedef TNode* TTree;
    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("%c ",p->Data);
            PreOrder(p->left);
            PreOrder(p->right);
        }
    }
    void InOrder(TTree T)
    {TTree p;    p = T;
        if(p!=NULL){
            InOrder(p->left);
            printf("%c ",p->Data);
            InOrder(p->right);
        }
    }
    void PosOrder(TTree T)
    {TTree p;    p = T;
        if(p != NULL){
            PosOrder(p->left);
            PosOrder(p->right);
            printf("%c",p->Data);
        }
    }
    void main()
    {TTree T;
        char A,B,C,D,E,F,G,H,I;
         clrscr();
        T= insert(A,insert(B,insert(D,NULL,insert(F,insert(H,NULL,NULL),insert(I,NULL,NULL))),NULL),insert(C,insert(E,NULL,insert(G,NULL,NULL)),NULL));
        printf("TIEN TO:   ");
        PreOrder(T);printf("\n");
        printf("TRUNG TO:  ");
        InOrder(T); printf("\n");
        printf("HAU TO:    ");
        PosOrder(T); printf("\n");
        getch();
    }
  2. Special One
    Offline
    Special One   [1.055/858]
    Bạn nên sửa lại tiêu đề cho phù hợp. Nếu như bạn muốn nhận được sự giúp đỡ!
    Thân!
  3. conloc171
    Offline
    conloc171   [29/2]
    Bây giờ sửa không được anh ơi. Sửa code giúp em đi. Mai mốt em sẽ viết đàng hoàn hơn
  4. takechij
    Offline
    takechij   [386/165]
    bạn viết cây nhị phân sao không dùng đệ quy, híc, in ra 100 chữ thì phải gõ insert 100 lần à :-ss
    mình cũng lười edit code cho bạn, bạn tham khảo code dưới đây xem có giúp được gì không nhé
    PHP:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream>
    using namespace std;
    struct node{
        
    int data;
        
    struct node *left,*right;
    };
    typedef node *tree;
    tree root;
    void tao_cay(tree &p){
        
    p=NULL;
    }
    void chen_node(tree &p,int x){
        if(
    p==NULL){
            
    p=new node;
            
    p->data=x;
            
    p->left=p->right=NULL;
        }
        else if(
    x<=p->datachen_node(p->right,x);
        else 
    chen_node(p->left,x);
    }
    void nhap_cay(tree &p){
        
    int x;
        do{
            
    cout << "\nNhap gia tri: ";cin >> x;
            if(
    x!=0)
                
    chen_node(p,x);
        }while(
    x!=0);
    }
    void NLR(tree p){
        if(
    p!=NULL){
            
    cout << p->data << "\t";
            
    NLR(p->left);
            
    NLR(p->right);
        }
    }
    void LNR(tree p){
        if(
    p!=NULL){
            
    NLR(p->left);
            
    cout << p->data << "\t";
            
    NLR(p->right);
        }
    }
    void LRN(tree p){
        if(
    p!=NULL){
            
    NLR(p->left);NLR(p->right);cout << p->data << "\t";
        }
    }
    void dem_la(tree p,int &c){
        if(
    p!=NULL){
            
    dem_la(p->left,c);
            
    dem_la(p->right,c);
            if(
    p->left==NULL && p->right==NULL && p->data!=NULL)
                
    c++;
        }
    }
    void dem_nut(tree p,int &c){
        if(
    p!=NULL){
            
    c++;
            
    dem_nut(p->left,c);
            
    dem_nut(p->right,c);
        }
    }
    int Max(int aint b) {
       if(
    a>b)
          return 
    a;
       else
          return 
    b;
    }
    int chieu_cao(tree T) {
       if( 
    == NULL)
          return 
    0;
       else
          return 
    Max(chieu_cao(T->left),chieu_cao(T->right)) + 1;
    }
    void thaythe(tree &p,int x){
        
    tree q=p;
        while(
    q!=NULL && x!=q->data){
            if(
    x>q->data)
                
    q=q->right;
            else
                
    q=q->left;
        }
        
    q->data=x*x;
    }
    int tim_dequy(tree pint x){
        if(
    p==NULL) return 0;
        else
            if(
    p->data==x) return 1;
            else
                if(
    x>p->data) return tim_dequy(p->right,x);
                else return 
    tim_dequy(p->left,x);
    }
    int xoa_cucphai(tree &p){
        
    int k;
        if(
    p->right==NULL){
            
    k=p->data;
            
    p=p->left;
        }
        else return 
    xoa_cucphai(p->right);
        return 
    k;
    }
    void xoa_node(tree &p,int x){
        if(
    x<p->dataxoa_node(p->left,x);
        else
            if(
    x>p->dataxoa_node(p->right,x);
            else{
                if(
    p->left==NULL && p->right==NULL)
                    
    p=NULL;
                else
                    if(
    p->left==NULLp=p->right;
                    else
                        if(
    p->right==NULLp=p->left;
                        else 
    p->data=xoa_cucphai(p->left);
            }
    }

    void main()
    {
        
    tao_cay(root);
        
    nhap_cay(root);
        
    LRN(root);
        
    cout << endl;cout << endl;cout << endl;
    }
    à, bạn muốn in danh sách chữ cái ra màn hình thì cho chạy vòng for, dùng bảng mã ASCII để đưa dữ liệu vào cây, sau đó in ra, hoặc là nhập ký từ vào từ bàn phím sau đó in ra
  5. integer
    Offline
    integer   [1.695/1.313]
    Em biết khi nào nó in ra các ký tự đặc biệt không ? Hiểu được vấn đề này sẽ tự hiểu được nguyên nhân :)
  6. Jayhoo
    Offline
    Jayhoo   [39/41]
    Giúp được thì giúp cái đi chứ toàn Spam không chứ có giúp được người ta cái gì đâu.
  7. Xautrai_LT03B
    Offline
    Xautrai_LT03B   [38/15]
    không có thời jan lắm nhưng mình củng đã làm bài này rồi mình pos lên cho ban tham khảo


    Mã:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream.h>
    typedef char tdata;
    typedef struct sn
        {
        tdata so;
        sn *lef;
        sn *ref;
        };
    typedef sn *tree;
    tree nhap(tdata v,tree l,tree r)
        {
        tree k;
        k=new sn;
        k->so=v;
        k->lef=l;
        k->ref=r;
        return k;
        }
    void tiento(tree t)
    {
        tree p;
        p=t;
        if(p!=NULL)
            {
            cout<<" "<<p->so;
            tiento(p->lef);
            tiento(p->ref);
            }
    }
    void trungto(tree t)
    {
        tree p;
        p=t;
        if(p!=NULL)
            {
            trungto(p->lef);
            cout<<" "<<p->so;
            trungto(p->ref);
            }
    }
    void hauto(tree t)
    {
        tree p;
        p=t;
        if(p!=NULL)
            {
            hauto(p->lef);
            hauto(p->ref);
            cout<<" "<<p->so;
            }
    }
    void main()
    {
    clrscr();
    tree t;
    t=NULL;
    t=nhap('A',nhap('B',NULL,nhap('D',nhap('F',NULL,NULL),
    nhap('G',NULL,NULL))),nhap('C',NULL,nhap('E',NULL,NULL)));
        printf("\nTien To:  ");
        tiento(t);printf(" \n");
        printf("\nTrung To: ");
        trungto(t);printf(" \n");
        printf("\nHau To: ");
        hauto(t); printf("\n");
    
    getch();
    }