Tất cả câu hỏi
Ngân hàng câu hỏi trắc nghiệm có giải thích chi tiết.
while(S is not empty){ch = top element of the stack S if ch is isolated then remove it from the stack and put it to E
elseselect the first vertex Y (by alphabet order), which is adjacent to ch,push Y to S and remove the edge (ch,Y) from the graph
}the last array E obtained is an Euler cycle of the graph Suppose a multigraph G is given below (view picture). Which of the followings is the Euler cycle from the vertex C, created by above algorithm?
void preOrder(Node p)
{ if (p != null) { visit(p); preOrder(p.left); preOrder(p.right); }
}void preOrder(Node p)
{ if (p != null) { preOrder(p.left); visit(p); preOrder(p.right); }
}void preOrder(Node p)
{ if (p == null) { visit(p); preOrder(p.left); preOrder(p.right); }
}void preOrder(Node p)
{ visit(p); preOrder(p.left); preOrder(p.right); }Đáp án đúng: "
void preOrder(Node p) {
if (p != null) {
visit(p);
preOrder(p.left);
preOrder(p.right);
}
}". Tiền thứ tự nghĩa là thăm gốc trước rồi mới xuống nhánh trái, sau đó nhánh phải, nên lệnh thăm phải đứng trước cả hai lời gọi đệ quy; thêm vào đó điều kiện p khác null chính là chỗ dừng, nó chặn lời gọi đi vào cây rỗng. Cách viết "
void preOrder(Node p) {
if (p != null) {
preOrder(p.left);
visit(p);
preOrder(p.right);
}
}" đặt lệnh thăm giữa hai lời gọi nên cho ra thứ tự trung tố, với cây tìm kiếm thì in dãy tăng dần chứ không phải tiền thứ tự. Cách viết "
void preOrder(Node p) {
if (p == null) {
visit(p);
preOrder(p.left);
preOrder(p.right);
}
}" đảo điều kiện thành p bằng null, tức chỉ làm việc khi con trỏ rỗng, nên nó thăm một nút không tồn tại và không bao giờ đi xuống cây thật. Cách viết "
void preOrder(Node p) {
visit(p);
preOrder(p.left);
preOrder(p.right);
}" đúng trật tự thăm nhưng bỏ hẳn chỗ dừng, mỗi lá vẫn gọi tiếp vào hai con rỗng nên đệ quy lún vô hạn và chương trình đổ vì truy cập trên con trỏ rỗng.
Node search(int x)
{ Node p = root;
while(p!=null && p.info != x)
{ if(x<p.info) p = p.left;
else p=p.right;
}
return(p);
}The complexity of this algorithm is:
O(n^2)O(n).O(log n).O(nlog n)