You are on page 1of 7

TRƯỜNG ĐH NGUYỄN TẤT THÀNH ĐỀ THI KẾT THÚC HỌC PHẦN

KHOA CÔNG NGHỆ THÔNG TIN MÔN : Cấu trúc dữ liệu và thuật giải
THỜI GIAN : 90 phút
HỌC KỲ : 1 – Năm học 2021-2022
LỚP : 20DTH
Sinh viên được sử dụng tài liệu, cán bộ coi thi không giải thích gì thêm Mã đề: 02

Câu 1: (5 điểm) Lớp danh sách liên kết đơn chứa các số nguyên đã được cài đặt:

a. Sử dụng lớp trên viết hàm tách danh sách L thành 2 danh sách L1 chứa số lẻ và L2 chứa số chẵn
(2đ)
b. Không sử dụng các phương thức của lớp danh sách liên kết đơn, cài đặt phương thức tách danh
sách L thành 2 danh sách L1 chứa số lẻ và L2 chứa số chẵn (3đ)
Câu 2: (5 điểm) Hãy thực hiện các yêu cầu sau trên cây tìm kiếm nhị phân:
a. Khai báo cấu trúc cây tìm kiếm nhị phân chứa các số nguyên (1đ)
b. Viết hàm tính đếm số node có giá trị < x trên cây (1đ)
c. Viết hàm tính tổng giá trị các node trên mức < k của cây (1đ)
d. Mô tả quá trình hình thành cây tìm kiếm nhị phân khi đưa vào cây lần lượt các giá tri sau (2đ):
30, 20, 40, 10, 15, 18, 12, 38, 45, 8.
Và xóa 30, 10. 40
DUYỆT ĐỀ Tp.HCM, ngày 02 tháng 01 năm 2022
GIẢNG VIÊN RA ĐỀ

Lê Mậu Long
Tên:Nguyễn Trọng Đat MSSV:2000000100

Lớp:20DTH1D

Bài làm

Câu 1:

a).

Class SplitLinkList

import java.util.LinkedList;

public class SplitLinkedList<E> {

public void splitMid(LinkedList<E> sublist) {


Node midPoint = nodeBefore.next; // get the midpoint
LinkedList n = new LinkedList();
n.head = midPoint; // head pointer for the second linked list

for (int i = 0; i < n.size(); ++i) {

}
}}
import java.util.LinkedList;

Class TestSplitLinkedList

public class TestSplitLinkedList<E> {

public void main(String args[]) {


LinkedList<Integer> myList = new LinkedList<Integer>();
SplitLinkedList sublist = new SplitLindedList();

myList.addLast(34);
myList.addLast(65);
myList.addLast(27);
myList.addLast(89);
myList.addLast(12);

myList.splitMid(sublist);

}
}

1B).
// A Linked List Node
class Node {
int data;
Node next;

Node(int data, Node next) {


this.data = data;
this.next = next;
}
}
class Main {
// Helper function to print a given linked list
public static void printList(String msg, Node head) {
System.out.print(msg);

Node ptr = head;


while (ptr != null) {
System.out.print(ptr.data + " —> ");
ptr = ptr.next;
}

System.out.println("null");
}

/*
* Given the source list, split its nodes into two shorter lists. If we number
* the elements 0, 1, 2, … then all the even elements should go in the first
* list and all the odd elements in the second. The elements in the new lists
* may be in any order.
*/
public static Node[] alternatingSplit(Node source) {
// Split the nodes into `a` and `b` lists
Node a = null;
Node b = null;
Node current = source;

while (current != null) {


// Move a node to `a`

Node newNode = current; // the front source node


current = current.next; // advance the source

newNode.next = a; // link the old dest off the new node


a = newNode; // move dest to point to the new node

if (current != null) {
// Move a node to `b`

newNode = current; // the front source node


current = current.next; // advance the source

newNode.next = b; // link the old dest off the new node


b = newNode; // move dest to point to the new node
}
}
return new Node[] { a, b };
}

public static void main(String[] args) {


// input keys
int[] keys = { 1, 2, 3, 4, 5, 6, 7 };

// construct the first linked list


Node head = null;
for (int i = keys.length - 1; i >= 0; i--) {
head = new Node(keys[i], head);
}

Node[] nodes = alternatingSplit(head);

// print both lists


printList("First List: ", nodes[0]);
printList("Second List: ", nodes[1]);
}
}

Câu 2:

a).
package Tree;

import java.util.Scanner;

class node {
int data;
node left, right;

public node(int data) {


this.data = data;
left = right = null;
}
}

class tree {
node root;

public tree() {
root = null;
}

public void setData(int data) {


root.data = data;
}

public int getData() {


return root.data;
}

public tree getLeft() {


tree t = new tree();
t.root = root.left;
return t;
}

public tree getRight() {


tree t = new tree();
t.root = root.right;
return t;
}

public boolean isEmpty() {


return root == null;
}

private node insert(node r, int data) {


if (r == null)
r = new node(data);
if (data < r.data)
r.left = insert(r.left, data);
else if (data > r.data)
r.right = insert(r.right, data);
return r;
}

public void insertTree(int data) {


root = insert(root, data);
}

public tree search(int key) {


tree t = new tree();
node c = root;
while (c != null && c.data != key) {
if (key < c.data)
c.left = root.left;
else
c.right = root.right;
}
t.root = c;
return t;
}

private node searchByRecursive(node c, int key) {


if (c == null)
return null;
if (key < c.data)
return searchByRecursive(c.left, key);
else if (key > c.data)
return searchByRecursive(c.right, key);
return c;
}

public tree searchT(int key) {


tree t = new tree();
t.root = searchByRecursive(root, key);
return t;
}
}

public class BinarySearchTree {


static Scanner sc = new Scanner(System.in);

static void inputTree(tree t) {


int x;
System.out.print("Nhap day(0 ket thuc): ");
do {
x = sc.nextInt();
if (x > 0)
t.insertTree(x);
} while (x != 0);
}

static void outputTree(tree t, int k) {


if (!t.isEmpty()) {
outputTree(t.getLeft(), k + 1);
System.out.println(blank(4 * k) + t.getData());
outputTree(t.getRight(), k + 1);
}
}

static String blank(int n) {


String s = "";
for (int i = 0; i < n; i++)
s += " ";
return s;
}

static void outputTree(tree t) {


outputTree(t, 1);
}
}

2b).

import java.util.*;

// Class representing a Node of an N-ary tree


class Node{

int key;
ArrayList<Node> child;

// Constructor to create a Node


Node(int val)
{
key = val;
child = new ArrayList<>();
}
}

class GFG{

// Recursive function to find number


// of nodes greater than x
public static int nodesGreaterThanX(Node root, int x)
{
if (root == null)
return 0;

int count = 0;

// If current root is greater


// than x increment count
if (root.key < x)
count++;

// Recursively calling for every


// child of current root
for(Node child : root.child)
{
count += nodesGreaterThanX(child, x);
}

// Return the count


return count;
}

// Driver code
public static void main(String[] args)
{

/* Let us create below tree


5
/ | \
1 2 3
/ / \ \
15 4 5 6
*/

Node root = new Node(5);


root.child.add(new Node(1));
root.child.add(new Node(2));
root.child.add(new Node(3));

root.child.get(0).child.add(new Node(15));
root.child.get(1).child.add(new Node(4));
root.child.get(1).child.add(new Node(5));
root.child.get(2).child.add(new Node(6));

int x = 5;

System.out.print("Number of nodes greater than " +


x + " are ");
System.out.println(nodesGreaterThanX(root, x));
}
}

2c).

static int tong(tree t)


{
if (t.isEmpty())
return 0;
return t.getData() + tong(t.getLeft()) + tong(t.getRight());
}

D).

You might also like