Tutorial 2
Tutorial 2
Tutorial 2
Tutorial #2 Trees
Exercise #7 Write a function that will print the keys from a binary tree in the bracketed form.
( key : LT, RT )
Where key is the key in the root, LT denotes the left subtree of the root printed in bracketed form, and RT denotes the right
subtree in bracketed form. )
Exercise #8 Write a function that will interchange all left and right subtrees in a linked binary tree (see example below).
1 1
2 3 Becomes 3 2
4 5 6 6 5 4
7 8 7 8
Data Structures Exercises on Trees Dr. Lossan BONDE PKFIE 2015 Page 1/5
Exercise # 9 Write a fucntion that will delete a node from a linked binary tree, using the following method in case when the
node to be deleted has both subtrees non-empty. First find the immediate predecessor of the node under inorder traversal (the
immediate successor would work just as well), by moving to its left child and then as far right as possible. The immadiate
predecessor is guaranteed to have at most one child (why?), so it can be deleted from its current position without difficulty. It
can then be placed into the tree in the position formerly occupied by the node that was supposed to be deleted, and the
properties of a search tree will still be satistied (why?).
Exercise #10 Write a function for searching, using a binary search tree with sentinel as follows. Introduce a new sentinel
node, and keep a pointer to it. See figure below. Replace all the NULL links within the search tree with links to the sentinel.
Then, for each search, first store the target into the sentinel. Run both this procedure and the original procedure TreeSearch
to compare the time needed both for successful and unsuccessful search.
X X
Exercise #11 Write a function that will traverse a binary tree level by level. That is, the root is visited first, the immediate
children of the root, then the grandchildren of the root, etc (Hint: remember to use a queue (BFS)).
Exercise #12 Write a function that will return the width of a linked binary tree, that is, the maximum number of nodes on the
same level.
Exercise #13 Write a function that converts a binary tree into a doubly linked list, in which the nodes have the order of inorder
traversal of the tree. At the conclusion of the function, the pointer root should point to the leftmost node of the doubly
linked list, and the links right and left should be used to move through the list, and be NULL at the two ends of the list.
Data Structures Exercises on Trees Dr. Lossan BONDE PKFIE 2015 Page 2/5
This sequential representation can, in fact, can be extended to arbitrary binary trees, provided that we can flag locations in the
array to show that the corresponding nodes do not exist. The results for several binary trees are shown below.
This kind of representation is interesting when all the leaves are on the same level, and all are as far to the left as possible.
Heaps and Heapsort
A heap is defined to be a binary tree with a key in each node, such that
1. All the leaves of the tree are on two adjacent levels,
2. All the leaves on the lower level occur to the left,
3. The key in the root is at least as large as the keys in its childern (if any), and the left and right children (if they
exist) are again heaps.
The examples provided below present a heap and other trees which are not heaps.
Data Structures Exercises on Trees Dr. Lossan BONDE PKFIE 2015 Page 3/5
Heapsort: is a process of constructing a heap. It proceeds in two phases. In the first phase, the entries in the array being
sorted are interpreted as a binary tree in contiguous representation. The first two properties of a heap are automatically
satisfied, but the keys will not generally satisfy the third property. In the second phase, the first entry is moved to the last
position, replacing an entry x. we then decrease a counter k that keeps track of the size of the list, thereby excluding the
largest entry from further sorting. The entry x that has been moved from the last position, however, may not belong on the
top of the heap, and therefore we must insert x into the proper position to restore the heap property before continuing to loop
in the same way. Implementation of heapsort is provided in module heap.
Exercise #21 Determine the contiguous representation of each of the following binary trees. Which of these are heaps? For
those that are not, state which rule(s) is (are) violated at which node(s).
Exercise #22 By hand, trace the action of HeapSort on each of the following lists. Draw the initial tree to which the list
corresponds; show how it is converted into a heap; and show the resulting heap as each item is removed from the top and the
new item inserted.
a) The list of seven numbers : 26 33 35 29 19 12 22
b) The list of fourteen names: Tim, Dot, Eva, Roy, Tom, Kim, Guy, Amy, Jon, Ann, Jim, Kay, Ron, Jan.
Exercise #23 Design a function that will insert a new item into a heap, obtaining a new heap (the function InsertHeap
provided in the module requires that the root is unoccupied, whereas for this exercise the root will already contain the item
with largest key, which must remain in the heap. Your function will increase the count n of items in the list.) Analyse the
time and space requirements of your function.
Exercise #24 Design a function that will delete the item with largest key (the root) from the top of the heap, and restore the
heap properties of the resulting, smaller list.
Exercise #25 Design a function that will delete the item with index I from a heap, and restore the heap properties of the
resulting, smaller list.
Data Structures Exercises on Trees Dr. Lossan BONDE PKFIE 2015 Page 4/5
VI/ B-Trees
(see module Btree provided)
Exercise #30 Insert the keys below, in the order stated, into an initially empty B-Tree of order:
A, B, C, D, E, F, G, H, I, J, K, L, X, W, Y, Z
a) 3
b) 4
c) 7
Exercise #31 Rewrite the function SearchNode to use a binary search.
Exercise #32 Remove the tail-end recursion from function Search.
Data Structures Exercises on Trees Dr. Lossan BONDE PKFIE 2015 Page 5/5