Академический Документы
Профессиональный Документы
Культура Документы
HCM
Khoa Khoa học & Kỹ thuật Máy tính
BINARY TREE
Question 1.
a. Given a list of integers as follows, insert those integers into an empty BST one-by-one.
Suppose that the numbers will be added to the tree in the same order as that of the list.
{59, 17, 4, 13, 72, 91, 87, 21, 33, 60, 71, 1, 19}
b. Randomly re-order the list in Question 1a as follows. Please generate the BST again
(from an empty BST). (The purpose is to observe the input-order-sensitive of the BST.)
{71, 1, 4, 13, 87, 91, 72, 33, 21, 60, 59, 17, 19}
1
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
c. Redraw the BST after deleting the node 60 from the BST in Question 1a.
d. Redraw the BST after deleting the node 71 from the BST in Question 1b.
Question 2.
a. For which trees do the preorder and inorder traversals generate the same sequence?
Trees have only right sub tree. Ex:
A
Preoder: A B C D
Inoder: A B C D
2
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
b. What are the minimum and maximum numbers of leaves in a balanced tree of height
h?
• Minimum:
minLeaf(h) = min(minLeaf(h-1)+minLeaf(h-2), 2*minLeaf(h-1))
= minLeaf(h-1) + minLeaf(h-2) minLeaf (h-2) < minLeaf(h-1)
minLeaf(1) = minLeaf(2) = 1
minLeaf(h) = minLeaf(h-1) + minLeaf(h-2) (h>2)
• Maximum:
maxLeaf(1) = 1
maxLeaf(h) = max(maxLeaf(h-1)+maxLeaf(h-2), 2*maxLeaf(h-1)) (h > 1)
= 2*maxLeaf(h-1) = 2h – 1
c. Using inorder, preorder, and postorder tree traversal, visit only leaves of a tree. What
can you observe? How can you explain this phenomenon?
Traversal results are the same.
Explain:
- The order when traversing of left and right sub-trees are the same in 3 tree
traversal types (left to right)
- Visit only leaves of the tree the internal nodes will not be included in
traversal results.
Inorder: L N R -> L R
Preorder: N L R -> L R
Postorder: L R N -> L R
The order of traversed leaves will be the same.
Question 3.
end checkBST_recur
algorithm isBST ()
This algorithm check if the tree is a BST
Pre
Post
Return true if the tree is a BST, false otherwise
end isBST
3
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
ANSWER:
algorithm isBST ()
This algorithm check if the tree is a BST
Pre
Post
Return true if the tree is a BST, false otherwise
1. return checkBST_recur(root, min, max)
end isBST
Question 4.
end checkAVL_recur
4
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
algorithm isAVL ()
This algorithm check if the tree is an AVL
Pre
Post
Return true if the tree is an AVL, false otherwise
end isAVL
ANSWER:
algorithm isAVL ()
This algorithm check if the tree is an AVL
Pre
Post
Return true if the tree is an AVL, false otherwise
1. return checkAVL_recur(root, min, max, height)
end isAVL
Question 5.
Write a global function in pseudocode to generate a BST from an input list by insert
elements in the list into an initial empty BST. Refer to question 1 for an example.
5
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
Question 6.
Also write a method in pseudocode to generate the AVL from an input ordered list.
6
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính
-- End --