Baitap bài tập tuần 7 – cây nhị phân th tuan 7

4 1 0
Baitap bài tập tuần 7 – cây nhị phân th tuan 7

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

Thông tin tài liệu

Bài tập Tuần 7 – Cây nhị phân Bài 1 Cho cây nhị phân tìm kiếm BST Binary Search Tree, định nghĩa như sau // Node of BST Tree struct Node { int data; Node *pLeft; Node *pRight; }; // Hàm tạo một node N[.]

Bài tập Tuần – Cây nhị phân Bài Cho nhị phân tìm kiếm -BST- Binary Search Tree, định nghĩa sau: // Node of BST Tree struct Node { int data; Node *pLeft; Node *pRight; }; // Hàm tạo node Node* newNode(int data) { Node* tmp = new Node; tmp->data = data; tmp->pLeft = tmp->pRight = NULL; return tmp; } a) Viết hàm tìm node có khố 'key' Node* searchBST(Node* pRoot, int key); b) Tìm node có khố gần với x Node* search_nearX(Node* pRoot, int x); c) Viết hàm thêm node với khoá 'key' Node* insertBST(Node* node, int key); d) Tạo BST kiểm thử hàm viết Bài Cho nhị phân định nghĩa sau // A binary tree node struct Node { int data; Node *pLeft, *pRight; }; a) Viết hàm duyệt theo thứ tự Inorder(Left, Root, Right) b) Viết hàm duyệt theo thứ tự preorder(Root, Left, Right) c) Viết hàm duyệt theo thứ tự postorder(Left, Right, Root) d) Viết hàm duyệt nhị phân theo chiều rộng (Duyệt theo mức) void BFS_traversal(Node *pRoot); input: Output: 25 15 50 10 22 35 70 12 18 24 31 44 66 90 Bài Cho nhị phân định nghĩa sau // A binary tree node struct Node { int data; Node *pLeft, *pRight; }; a) Viết hàm xoá nút khỏi nhị phân b) Viết hàm thêm node vào nhị phân theo mức Sau thêm node 12 Bài – Cấu trúc Heap, Binary Heap Cấu trúc Heap định nghĩa sau: // Cấu trúc Heap struct Heap { int heap_size; int *harr; int capacity; }; // Hàm tạo Heap Heap* creatHeap(int capacity) { Heap* heap = new Heap; heap->harr = new int; heap->heap_size = 0; heap->capacity = capacity; return heap; } Hãy viết hàm thao tác cấu trúc Heap: Viết hàm Heapify() void Heapify(Heap* h, int i) Viết hàm extractMin int extractMin(Heap* h) Thêm khoá ‘k’ vào Heap void insertKey(Heap* heap, int k) Xoá khoá mục ‘i’ void deleteKey(Heap* h, int i) Viết chương trình tạo Heap kiểm thử lại hàm viết Hướng dẫn: Xem cấu trúc Heap Link: https://towardsdatascience.com/data-structure-heap-23d4c78a6962 ======================== ***** ======================== ... int data; Node *pLeft, *pRight; }; a) Viết hàm xoá nút khỏi nhị phân b) Viết hàm th? ?m node vào nhị phân theo mức Sau th? ?m node 12 Bài – Cấu trúc Heap, Binary Heap Cấu trúc Heap định nghĩa sau:...d) Viết hàm duyệt nhị phân theo chiều rộng (Duyệt theo mức) void BFS_traversal(Node *pRoot); input: Output: 25 15 50 10 22 35 70 12 18 24 31 44 66 90 Bài Cho nhị phân định nghĩa sau // A... // Hàm tạo Heap Heap* creatHeap(int capacity) { Heap* heap = new Heap; heap->harr = new int; heap->heap_size = 0; heap->capacity = capacity; return heap; } Hãy viết hàm thao tác cấu trúc Heap:

Ngày đăng: 25/03/2023, 07:20