Cây tìm kiếm nhị phân Đại học cần thơ Viết hàm khởi tạo và trả về một cây rỗng Tree initTree (){ Tree T; T=NULL; return T; } Viết hàm kiểm tra cây có gốc là T có rỗng hay không? int isEmpty (Tree T){ return T==NULL; } Viết hàm tìm kiếm khóa x trong cây tìm kiếm nhị phân Tree searchNode (int x, Tree T){ if (T=NULL) { if (x== T>Key) return T; if (x < T>Key) return searchNode(x,T>Left); if (x > T>Key) return searchNode(x,T>Right); } return NULL; } Viết hàm thêm khóa X vào cây tìm kiếm nhị phân void insertNode (int x, Tree T){ if((T)==NULL){ (T) = (struct Node)malloc (sizeof(struct Node)); (T)>Key =x; (T)>Left=NULL; (T)>Right=NULL; } else if ((T)>Key == x) {} else if(x < (T)>Key) insertNode (x,(T)>Left); else insertNode (x,(T)>Right); } Viết hàm tìm kiếm nút cha của nút có khóa x trong cây tìm kiếm nhị phân Tree getParent (int x, Tree T){ Tree P,Parent; P=T; Parent = NULL; while (P=NULL){ if(P>Key == x) return Parent; Parent=P; if(P>Key >x) P=P>Left; else P=P>Right; } return NULL; } Viết hàm xóa một nút có khóa là x trong cây tìm kiếm nhị phân KeyType deleteMin (Tree T) { KeyType k; if ((T)>Left == NULL) { k = (T)>Key; (T) = (T)>Right; return k; } return deleteMin ( (T)>Left); } void deleteNode (int x, Tree T){ if (T=NULL) { if(x < (T)>Key) deleteNode (x,(T)>Left); else if (x > (T)>Key) deleteNode (x,(T)>Right); else if (((T)>Left == NULL) ((T)>Right == NULL)) (T)=NULL; else if((T)>Left == NULL) (T)=(T)>Right;
Viết hàm khởi tạo trả rỗng Tree initTree (){ Tree T; T=NULL; return T; } Viết hàm kiểm tra có gốc T có rỗng hay không? int isEmpty (Tree T){ return T==NULL; } Viết hàm tìm kiếm khóa x tìm kiếm nhị phân Tree searchNode (int x, Tree T){ if (T!=NULL) { if (x== T->Key) return T; if (x < T->Key) return searchNode(x,T->Left); if (x > T->Key) return searchNode(x,T->Right); } return NULL; } Viết hàm thêm khóa X vào tìm kiếm nhị phân void insertNode (int x, Tree *T){ if((*T)==NULL){ (*T) = (struct Node*)malloc (sizeof(struct Node)); (*T)->Key =x; (*T)->Left=NULL; (*T)->Right=NULL; } else if ((*T)->Key == x) {} else if(x < (*T)->Key) insertNode (x,&(*T)->Left); else insertNode (x,&(*T)->Right); } Viết hàm tìm kiếm nút cha nút có khóa x tìm kiếm nhị phân Tree getParent (int x, Tree T){ Tree P,Parent; P=T; Parent = NULL; while (P!=NULL){ if(P->Key == x) return Parent; Parent=P; if(P->Key >x) P=P->Left; else P=P->Right; } return NULL; } Viết hàm xóa nút có khóa x tìm kiếm nhị phân KeyType deleteMin (Tree *T) { KeyType k; if ((*T)->Left == NULL) { k = (*T)->Key; (*T) = (*T)->Right; return k; } return deleteMin (& (*T)->Left); } void deleteNode (int x, Tree *T){ if (T!=NULL) { if(x < (*T)->Key) deleteNode (x,&(*T)->Left); else if (x > (*T)->Key) deleteNode (x,&(*T)->Right); else if (((*T)->Left == NULL) && ((*T)->Right == NULL)) (*T)=NULL; else if((*T)->Left == NULL) (*T)=(*T)->Right; else if ((*T)->Right == NULL) (*T)=(*T)->Left; else (*T)->Key = deleteMin (&(*T)->Right);} else printf ("Cay rong"); } Viết hàm liệt kê (in) giá trị khóa đường việc tìm kiếm khóa x tìm kiếm nhị phân void printPath (int x, Tree T){ if (T!=NULL) { if (x== T->Key) { printf ("%d ",T->Key); printf ("-> Tim thay"); return; } if (x < T->Key) { printf ("%d ",T->Key); printPath (x,T->Left); } if (x> T->Key){ printf ("%d ",T->Key); printPath (x,T->Right); } } else { printf ("-> Khong thay"); return; } } Viết hàm tính chiều cao T int getHeight(Tree T) { if(T==NULL) return -1; int L = getHeight(T->Left); int R = getHeight(T->Right); if(L>R) return (L+1); return (R+1); } Viết hàm duyệt tiền tự tìm kiếm nhị phân T void preOrder (Tree T){ if (T!=NULL){ printf ("%d ",T->Key); preOrder (T->Left); preOrder(T->Right); } } Viết hàm tính chiều cao nút có khóa x T int hNode (int x, Tree T){ if (T==NULL) return -1; if (T->Key == x){ if (T->Left == NULL && T->Right == NULL) return 0; int l=0; int r=0; if (T->Left!=NULL) l=hNode(T->Left->Key,T->Left); if (T->Right!= NULL) r=hNode(T->Right->Key,T->Right); if (l>r) return l+1; else return r+1; } else if (T->Key>x) return hNode(x,T->Left); else return hNode(x,T->Right); } Viết hàm tìm kiếm nút anh em ruột phải nút có khóa x tìm kiếm nhị phân Tree rightSibling (int x, Tree T) { Tree P=T, B=NULL; while (P!=NULL){ if (P->Key==x){ if (P!=B) return B; else return NULL;} B=P->Right; if (P->Key>x) P=P->Left; else P=P->Right; } return NULL; } Viết hàm trả trỏ nút đứng sau nút có khóa x cho trước phép duyệt trung tự Tree getMin (Tree T){ if (T==NULL) return NULL; if (T->Left == NULL) return T; return getMin(T->Left); } Tree getNext (int x, Tree T){ Tree P=T, Next=NULL; while (P!=NULL){ if (P->Key == x) { if (P->Right == NULL) return Next; else return getMin (P->Right); } else if (P->Key >x){ Next=P; P=P->Left; } else {P=P->Right;} } return NULL; } Viết hàm duyệt trung tự tìm kiếm nhị phân T void inOrder (Tree T){ if (T!=NULL){ inOrder(T->Left); printf ("%d ",T->Key); inOrder(T->Right); } } Viết hàm duyệt hậu tự tìm kiếm nhị phân T void posOrder (Tree T){ if (T!=NULL){ posOrder(T->Left); posOrder(T->Right); printf ("%d ",T->Key); } } Viết hàm trả trỏ nút đứng trước nút có khóa x cho trước phép duyệt trung tự (giả sử x chắn có T) Tree getMax (Tree T){ if (T==NULL) return NULL; if (T->Right == NULL) return T; return getMax(T->Right); } Tree getPrevious (int x, Tree T){ Tree P=T, Previous=NULL; while (P!=NULL){ if (P->Key == x) { if (P->Left == NULL) return Previous; else return getMax (P->Left); } else if (P->Key >x){ P=P->Left; } else { Previous=P; P=P->Right;} } return NULL; } ... hàm duyệt trung tự tìm kiếm nhị phân T void inOrder (Tree T){ if (T!=NULL){ inOrder(T->Left); printf ("%d ",T->Key); inOrder(T->Right); } } Viết hàm duyệt hậu tự tìm kiếm nhị phân T void posOrder... (&(*T)->Right);} else printf ("Cay rong"); } Viết hàm liệt kê (in) giá trị khóa đường việc tìm kiếm khóa x tìm kiếm nhị phân void printPath (int x, Tree T){ if (T!=NULL) { if (x== T->Key) { printf ("%d... return hNode(x,T->Left); else return hNode(x,T->Right); } Viết hàm tìm kiếm nút anh em ruột phải nút có khóa x tìm kiếm nhị phân Tree rightSibling (int x, Tree T) { Tree P=T, B=NULL; while (P!=NULL){