1. Trang chủ
  2. » Tất cả

Cây tìm kiếm nhị phân Cấu trúc dữ liệu Đại học Cần Thơ CTU

7 17 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 16,27 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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;

Trang 1

Viết hàm khởi tạo và trả về một cây rỗngTree 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ânTree 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ânvoid insertNode (int x, Tree *T){

if((*T)==NULL){

Trang 2

f(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ânTree 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ânKeyType 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)

Trang 3

else

f (((*T)->Left == NULL) && ((*T)->Right == NULL)) (*T)=NULL; else f((*T)->Left == NULL) (*T)=(*T)->Right; else f ((*T)->Right == NULL) (*T)=(*T)->Left; else

(*T)->Key = deleteMin (&(*T)->Right);} else printf ("Cay rong");

}

Viết hàm liệt kê (in) các giá trị khóa trên đường đi của việc tìm kiếm một khóa x trong cây tìm kiếm nhị phân

void printPath (int x, Tree T){

if (T!=NULL) { if (x== T->Key) {

Trang 4

printf ("-> Khong thay"); return;

}}

Viết hàm tính chiều cao cây Tint 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ự cây tìm kiếm nhị phân Tvoid 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 của nút có khóa x trong cây Tint 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)

Trang 5

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 của nút có khóa x trong cây 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){ f (P!=B) return B; else return NULL;} B=P->Right; f (P->Key>x) P=P->Left; else P=P->Right; } return NULL;}

Viết hàm trả về con trỏ của nút đứng sau nút có khóa x cho trước trong 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) {

Trang 6

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ự cây tìm kiếm nhị phân Tvoid 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ự cây tìm kiếm nhị phân Tvoid posOrder (Tree T){

if (T!=NULL){ posOrder(T->Left); posOrder(T->Right); printf ("%d ",T->Key); }}

Viết hàm trả về con trỏ của nút đứng trước nút có khóa x cho trước trong phép duyệt trung tự (giả sử x chắc chắn có trong cây T)

Tree getMax (Tree T){

Trang 7

}

Tree getPrevious (int x, Tree T){

Tree P=T, Previous=NULL; while (P!=NULL){

if (P->Key == x) {

f (P->Left == NULL) return Previous; else return getMax (P->Left);

Ngày đăng: 06/11/2022, 14:02

🧩 Sản phẩm bạn có thể quan tâm

w