Assignment 4
Assignment 4
1.
You are given a binary tree in the form of its array representation. Decide if the tree is a binary
search tree or not. The first number in the input indicates the number of elements that follows,
and the blank spaces in the array are indicated by a -1.
Input: 14 17 12 25 9 15 21 29 -1 11 -1 -1 19 -1 27
Output: YES
Input: 14 17 12 25 9 18 21 29 -1 11 -1 -1 19 -1 27
Output: NO
2.
Given the in order and one of pre-order or post order traversals of a binary tree, output the
remaining of preorder or post order traversal of the tree. Your input will contain the number of
nodes, n, followed by 1 for the in-order and post-order, and 2 for in-order and pre-order
traversals, followed by 2n integers corresponding to the two traversals.
Input:
71
2351476
2537641
Output:
1325467
3.
You have a set of numbers and their frequency of use and want to create a huffman encoding for
them:
FREQ VALUE
5 1
7 2
10 3
15 4
1|Page
20 5
45 6
N – number of data items (0<N<=100000),next line contains N integers as data- items, next line
contains the frequency of the corresponding data-item
Input:
123456
5 7 10 15 20 45
Output:
1 0010
2 0011
3 000
4 010
5 011
61
2|Page