Các anh cho em hỏi:
Khi em insert 1 dãy số tăng dần vào BST thì chương trình vẫn chạy nhưng tại sao khi insert 1 dãy số giảm dần thì chuơng trình lại không chạy được ạ?
Đây là code của em ạ!
/*
* ================================================== ===================================
*
* Filename: binaryTree.c
*
* Description:
*
* Version: 1.0
* Created: 11/12/2011 10:13:15 PM
* Revision: none
* Compiler: gcc
*
*
*
*
* ================================================== ===================================
*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
typedef struct tree {
long int item;
struct tree *parent;
struct tree *left;
struct tree *right;
} tree;
//Insert tree
void insertTree (tree **l, long int x, tree *parent)
{
tree *p;
if(*l == NULL){
p =(tree *)malloc (sizeof(tree));
p->item = x;
p->left = p->right = NULL;
p->parent = parent;
*l = p;
return;
}
if(x<(*l)->item)
insertTree(&(*l)->left, x, *l);
else
insertTree(&(*l)->right, x, *l);
}
//Insert Tang Dan
void insertTangDan(tree **l, long int soLuong){
long int i, j = 0;
for(i = 0; i< soLuong; i++){
insertTree(&(*l),j,*l);
j = j+2;
}
}
//Insert Giam Dan
void insertGiamDan(tree **l, long int soLuong){
long int i, j = soLuong;
for(i = soLuong; i> 0; i++){
insertTree(&(*l),j,*l);
j = j-1;
}
}
void Duyet(tree *l)
{
if (l!= NULL){
Duyet(l->left);
printf("%ld\n", l->item);
Duyet(l->right);
}
}
main()
{ tree *l;
long int soLuong;
l = (tree *)malloc(sizeof(tree));
printf("Nhap vao so luong: ");
scanf("%ld", &soLuong);
insertGiamDan(&l, soLuong);
Duyet(l);
free(l);
}
xin lỗi các anh!:p Em tìm thấy lỗi sai rồi! Hàm InsertGiamDan của em bị lặp vô tận!