645842d96e8314d66777cbcc PracticeTest2 CSD201 2 SU24

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CSD201 PROGRESS TEST 2

Time (60 minutes) The Question will be submitted along with your source code
to edu-next.

PART 1: Answer the Question. (40 pts)


1. Given an empty AVL tree, how would you construct the AVL tree when a set of numbers
are given without performing any rotations?
a. just build the tree with the given input
b. find the median of the set of elements given, make it as root, and construct the tree
c. use trial and error
d. use dynamic programming to build the tree

2. What do the below definitions convey?


3. The set of all addition edges generated by the BFS tree starting at node C is.

CGDFBEA

4. The set of all addition edges generated by the DFS tree starting at node D is.
5. The common data structure required for Depth First Traversal on a graph is.
a. Stack
b. Queue
c. Tree
d. All of the above

6. Which is the correct order for Kruskal’s minimum spanning tree algorithm to add edges to
the minimum spanning tree for the figure shown Below?

7. For minimum spanning tree (MST) construction, Prim’s algorithm selects?


a. With minimum weight so that cost of MST is always minimum
b. with the maximum number of vertices connected to it so that MST has the least
diameter
c. that does not introduce a cycle
d. that adds a new vertex to a partially constructed tree with minimal increment in the
cost of MST

8. Suppose we remove the root node from the following BST tree, which could be the
new root?
a. 2
b. 1
c. 5
d. 4

9. Given this BST tree

a. Convert this tree to AVL tree


10. Given this binary tree
a. Perform inorder traversal on this tree
b. convert this tree to BST tree
a. N -> L -> K -> D -> B -> A -> J -> E -> G -> M -> C -> F -> H -> I
b.
11. Given this graph

a . Build the minimum spanning tree from this graph by Kruskal algorithm and explain

PART 2: (60 pts)


Given the above BST tree
Define the tree:
a. Write Constructor to initialize the above tree

b. Write BFS traversal for the tree


Explain:
Dùng Queue ở trường hợp này để có thể add cũng như duyệt
được các node theo từng tầng của TREE
Vào trước, duyệt trước. Vào sau duyệt sau
Duyệt xong 20 -> add 10, 30 -> duyệt xong 10 -> add 5, 11 ->
duyệt 30 -> add 21, 31 -> ….
 Duyệt BFS sẽ bắt đầu từ 20
 Sau đó khởi tạo các Queue, ArrayList để sử dụng về sau
 enQueue giá trị 20 vào queue đã tạo (queue)
 Sau đó duyệt qua các giá trị ở trong queue
 Check, nếu Vertex đã được đi qua -> bỏ qua
 Chưa đi qua -> thì in nó ra -> sout(20)
 Sau đó add các Vertex con của Vertex đó vào queue
Lúc này Queue sẽ là: 10 -> 30
 Add 20 vào visited
 Và cứ thế chạy đến lúc nào queue rỗng thì dừng

c. Write code to add a new node to the above tree

Đầu tiên, để add một node vào cây AVL thì trước hết cứ phải
add một node vào tree.
Bắt đầu từ root, hàm insert 1 sẽ được gọi và truyền vào tham
số root, data mới, và null là giá trị của parent.
 Check root -> nếu null -> trả về giá trị new Node (data) ->
set parent = null (root không có cha)
 Root không null -> check value –> quẹo trái -> quẹo phải
đến lúc tham số root truyền vào = null -> set null = new
Node (data)
Sau khi add xong -> trả về cây cân bằng, duyệt từ root của cây
Node: Hàm balance có giải thích ở dưới ạ
Hàm balance em sẽ check xem cái node được truyền vào lệch
trái hay lệch phải, lệch trái -> thì quay phải; lệch phải -> quay
trái
d. Write code to remove node 10 from the tree

e. Rotate right at node 5

f. Convert the tree to an AVL tree

Write code (20pts) and explain how you write your code
(40pts) . Note Writing code without explaining anything is
considered plagiarism and will not get marked.

You might also like