CS3334 - Data Structures
Lab 4
Outline
• Tree Exercises
• Assignment 742: Simple Quadtree
• Assignment 749: Tree reconstruction
Exercise 1 for Tree
15
6 18
3 7 17 20
2 4 13
1. What’s the height of node 6?
2. What’s the height of node 3?
3. What is the depth of node 6?
4. What’s the depth of node 3?
5. What’s the preorder traversal of the tree?
Exercise 2 for Tree
1. A full binary tree of height = 10 will have _______ nodes.
2. How many leaves are there in a full binary tree of height 5?
Exercise 3 for Tree
• Create a binary search tree according to the input insertion
sequence:
{3, 5, 0, 2, 7, 6, 8, 9, 4, 1}.
• Then delete 3 from the constructed binary search tree.
Exercise 4 for Tree
• What’s the running time for the function “countleaf”?
Exercise 5 for Tree
• Reconstruct the binary tree whose postorder and inorder are
as follows.
– Postorder: BDFECA
– Inorder: BACDFE
Exercise 6 for Tree
• Showing the postorder traversal of the binary tree with
preorder and inorder shown as follows.
– Preorder: ABCDE
– Inorder: BDACE
Exercise 7 for Tree
• return the level of Node “ptr” in a binary tree with root node
“root”. Using the recursive function
struct Node {
int data;
struct Node *left, *right;
};
int level (Node* root, Node* ptr, int lev) {
… …
}
742: Simple Quadtree
Description
A quadtree is a representation format used to encode images.
The fundamental idea behind the quadtree is that any image can be split into four
quadrants. Each quadrant may again be split into four sub quadrants, etc.
In the quadtree, the image is represented by a parent node, while the four quadrants
are represented by four child nodes, in a predetermined order. Of course, if the whole
image only contains a single color, it definitely can be represented by a quadtree
consisting of a single node.
In general, a quadrant needs only to be subdivided if it consists of pixels of different
colors. As a result, the quadtree need not be of uniform depth. In this question, given a
picture containing only black and white pixels, please find out the number of nodes
(including the root) in the corresponding quadtree of the picture.
It is not necessary to implement the data structure of quadtree.
742: Simple Quadtree
742: Simple Quadtree
Input
The input contains multiple cases.
Each test case begins with one integer k, indicating thesize (n×n) of image, where 𝑛=2𝑘
and 0≤k≤10.
The following 𝑛 lines give the information of the pixels, where the i-th line contains a
binary string (containing ‘0’(white) and ‘1’(black)) of 𝑛 characters, indicating the pixel
information in the i-th row.
Output
For each test case print the number of nodes in the corresponding quadtree in a
separate line.
Sample Input Sample Output
2 13
0011 1
0001 77
1111 5
0111
2
0000
0000
0000
0000
3
11111101
01101011
10000000
10010111
00101111
01011101
11110100
11000010
1
11
10
749: Tree reconstruction
Description
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have
only one logical way to traverse them, binary tree can be traversed in different
ways.
In depth first traversal applications, a binary tree could be traversed in three
different ways: Preorder, Inorder and Postorder. For example, in the Inorder
traversal, we first traverse the left subtree, then the root, and finally the right
subtree, hence we could obtain the order of nodes in this traversal.
Given the result of inorder and preorder traversal, can you get the result of
postorder traversal?
749: Tree reconstruction
Input
The first line of input is an integer T (1 <= T <= 200) indicating the number of
test cases.
Each test case will follow the format shown below:
The first line: One integer N (1 <= N<= 100) indicating the number of tree nodes
(numbered from 1 to N) in the binary tree.
The second line: N integers n1, n2, ..., nN showing the result of preorder
traversal.
The third line: N integers m1, m2, ..., mN showing the result of inorder traversal.
Output
For each test case, print a single line containing N integers showing the result of
postorder traversal for each test case, separate two integers by one space.
749: Tree reconstruction
Sample Input Sample Output
2 74258631
8 45231
12473568
47215386
5
12453
42513