Announcement

Collapse
No announcement yet.

[Help] Cây nhị phân tìm kiếm!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [C++] [Help] Cây nhị phân tìm kiếm!

    Đây là code tạo 1 cây nhị phân! Mình đã kiểm tra kĩ lắm rồi, debug rồi mà mãi vẫn ko tìm ra lỗi nhưng khi chạy thử thì ko đc! Mong các anh chị và các bạn giúp mình với:
    #include "stdafx.h"
    #include <conio.h>
    #include <stdio.h>
    typedef struct tree_node
    {
    int key;
    struct tree_node *left,*right;
    }Node;
    typedef Node *tree;
    Node *creatnode(int k)
    {
    Node *p;
    p=new Node;
    p->key=k;
    p->left==NULL;
    p->right=NULL;
    return p;
    }
    int addnode(tree &t,Node *p)
    {
    if(t)
    {
    if(t->key==p->key)
    return 0;
    else
    {
    if(p->key<t->key)
    addnode(t->left,p);
    else
    addnode(t->right,p);

    }
    }
    else
    {
    t=p;
    return 1;
    }
    }
    void print(tree t)
    {
    if(t)
    {
    print(t->left);
    printf("%d ",t->key);
    print(t->right);
    }
    }
    void main()
    {
    Node *p;
    tree t;
    int k;
    t=NULL;
    do
    {
    printf("nhap vao khoa k");
    scanf("%d",&k);
    if(k==0)
    break;
    p=creatnode(k);
    if(addnode(t,p)==1)
    printf("nhap thanh cong");
    else
    printf("so nay ban da nhap roi");
    }
    while(k!=0);
    printf("cay ban vua nhap la");
    print(t);
    }

  • #2
    Originally posted by 11520002 View Post
    Đây là code tạo 1 cây nhị phân! Mình đã kiểm tra kĩ lắm rồi, debug rồi mà mãi vẫn ko tìm ra lỗi nhưng khi chạy thử thì ko đc! Mong các anh chị và các...
    Node *creatnode(int k)
    {
    Node *p;
    p=new Node;
    p->key=k;
    p->left==NULL;
    p->right=NULL;
    return p;
    }
    p->left=NULL;
    Blog: http://khuongntrd.blogspot.com/ Email: khuongntrd@gmail.com
    Facebook: https://www.facebook.com/dkuns2

    Comment


    • #3
      Hì! Thanks nhiều!!! Mình cứ nghĩ là ko thể sai đc nên khi kiểm tra chủ quan nên tìm ko ra lỗi!!! Thanks bác nhiều!!!

      Comment


      • #4
        Code:
        #include "stdafx.h"
        #include <conio.h>
        #include <stdio.h>
        typedef struct tree_node
        {
        	int key;
        	struct tree_node *left,*right;
        }Node;
        typedef Node *tree;
        Node *creatnode(int k)
        {
        	Node *p;
        	p=new Node;
        	p->key=k;
        	p->left==NULL;
        	p->right=NULL;
        	return p;
        }
        int addnode(tree &t,Node *p)
        {
        	if(t)
        	{
        		if(t->key==p->key)
        			return 0;
        		else
        		{
        			if(p->key<t->key)
        				addnode(t->left,p);
        			else
        				addnode(t->right,p);
        	
        		}
        	}
        	else
        	{
        		t=p;
        		return 1;
        	}
        }
        void print(tree t)
        {
        	if(t)
        	{
        		print(t->left);
        		printf("%d ",t->key);
        		print(t->right);
        	}
        }
        void main()
        {
        	Node *p;
        	tree t;
        	int k;
        	t=NULL;
        	do
        	{
        		printf("nhap vao khoa k");
        		scanf("%d",&k);
        		if(k==0)
        			break;
        		p=creatnode(k);
        		if(addnode(t,p)==1)
        			printf("nhap thanh cong");
        		else
        			printf("so nay ban da nhap roi");
        	}
        	while(k!=0);
        	printf("cay ban vua nhap la");
        	print(t);
        lân sau bạn cho vào thế này cho mọ người dễ xem nha:love:
        Originally posted by 11520002 View Post
        Đây là code tạo 1 cây nhị phân! Mình đã kiểm tra kĩ lắm rồi, debug rồi mà mãi vẫn ko tìm ra lỗi nhưng khi chạy thử thì ko đc! Mong các anh chị và các bạn giúp mình với:
        #include "stdafx.h"
        #include <conio.h>
        #include <stdio.h>
        typedef struct tree_node
        {
        int key;
        struct tree_node *left,*right;
        }Node;
        typedef Node *tree;
        Node *creatnode(int k)
        {
        Node *p;
        p=new Node;
        p->key=k;
        p->left==NULL;
        p->right=NULL;
        return p;
        }
        int addnode(tree &t,Node *p)
        {
        if(t)
        {
        if(t->key==p->key)
        return 0;
        else
        {
        if(p->key<t->key)
        addnode(t->left,p);
        else
        addnode(t->right,p);

        }
        }
        else
        {
        t=p;
        return 1;
        }
        }
        void print(tree t)
        {
        if(t)
        {
        print(t->left);
        printf("%d ",t->key);
        print(t->right);
        }
        }
        void main()
        {
        Node *p;
        tree t;
        int k;
        t=NULL;
        do
        {
        printf("nhap vao khoa k");
        scanf("%d",&k);
        if(k==0)
        break;
        p=creatnode(k);
        if(addnode(t,p)==1)
        printf("nhap thanh cong");
        else
        printf("so nay ban da nhap roi");
        }
        while(k!=0);
        printf("cay ban vua nhap la");
        print(t);
        }
        Hà Tĩnh Quê Choa:sunglasses:
        FACEBOOK

        Comment


        • #5
          Nếu có thêm cái BBCODE Syntax-Highlighter này thì hay quá. Hy vọng Admin thêm vào, Forum IT mà không có cái này thì thấy thiếu thiếu
          Blog: http://khuongntrd.blogspot.com/ Email: khuongntrd@gmail.com
          Facebook: https://www.facebook.com/dkuns2

          Comment

          LHQC

          Collapse
          Working...
          X