Lecture 18
Learning objectives
After this class, you should be able to:
- Explain the following terms: tree, root, level, height, parent, child, ancestor, descendant, leaf, subtree, path, length of a path, binary tree, complete binary tree, and binary search tree.
- Derive bounds on the heights of different types of trees.
- Given a tree, show the order in which the vertices are visited with the following traversals: (i) preorder, (ii) postorder, (iii) inorder, and (iv) level-order.
- Given a tree, determine whether or not it is a binary search tree.
- Given a binary search tree, show the nodes visited while searching for an element.
- Derive the time complexity of searching a binary search tree.
- Write code to implement the four traversals mentioned above, and to search for an element in a binary search tree.
Reading assignment
- Section 6.4, except 6.4.3. However, section 6.4.3 might give you some ideas for your homework, though you cannot directly use it.
- Page 243.
Exercises and review questions
- Exercises and review questions on current lecture's material
- Draw a binary search tree that stores the following data in a tree of minimum possible height: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 2.5, 3.5, 8.5.
- For the tree you drew above, give the value of the root node, and the parent and children of node with key n, where n is your group number. Also, give the value of at least two leaf nodes.
- Show the nodes visited while searching the above tree for key n, where n is your group number.
- Give the order in which nodes are visited when you traverse the above tree using level-order traversal. (If group number % 4 = 0)
- Give the order in which nodes are visited when you traverse the above tree using inorder traversal. (If group number % 4 = 1)
- Give the order in which nodes are visited when you traverse the above tree using preorder traversal. (If group number % 4 = 2)
- Give the order in which nodes are visited when you traverse the above tree using postorder traversal. (If group number % 4 = 3)
- Which type of traversal on a binary search tree will lead to the elements being visited in ascending order?
- Write code to perform a level-order traversal of a binary search tree.
- Let a ternary tree be one in which each node can have up to three children. Derive a formula for the number of nodes of a complete ternary tree of height
k
. What is the minimum height of a ternary tree with n
nodes? What is the maximum height of a ternary tree with n
nodes?
- Questions on next lecture's material
- Let a binary tree consist of only one node, with value
15
. If we wish to insert 3
and 18
, then what will the tree look like after the insertions?
Last modified: 21 Oct 2004