Data Structures and Algorithms Sheet #7 Trees: Part I: Exercises
Data Structures and Algorithms Sheet #7 Trees: Part I: Exercises
Data Structures and Algorithms Sheet #7 Trees: Part I: Exercises
Cairo University
Faculty of Engineering
Computer Engineering Department
Data Structures and Algorithms
Sheet #7
Trees
Part I: Exercises
A. General Trees
(a) (b)
B. Binary Trees
(h) (i)
(g)
(a) (b)
2. Draw the BST that results when you insert items with keys: E A S Y Q U E S T I O N
3. Traverse the BST no. (a) in the figure below using an in-order traversal.
(a) (b)
D. Heap
1. Define AVL trees and Heap trees. Draw an example for each.
2. Show which of the following figures are heaps and which are not.
3. Make a heap out of the following data read from the keyboard:
4. Apply the re-heap up algorithm to the non-heap structure shown in following figure.
5. Apply the re-heap down algorithm to the partial heap structure shown in the following figure.
A. Binary Trees
1. Write a member function that counts the number of nodes in a binary tree.
2. Write a member function that returns the sum of all the nodes in a binary tree.
3. Write a member function that counts the number of leaves in a binary tree.
4. Write a member function that counts the number of leaves that contain even values in a binary tree.
5. Write a member function that returns the maximum value of all the nodes in a binary tree. Assume
all values are non-negative; return -1 if the tree is empty.
6. Write a member function that prints all the keys that are less than a given value, V, in a binary tree.
7. Write a member function that returns true if the sum of all the nodes in a binary tree equal to a given
value, V.
8. Write a member function is_mirror that returns true if the two passed tree are mirrors; every left
child in the first tree is a right second tree and vice versa.
9. Write a member function that creates a mirror image of a binary tree. All left children become right
children and vice versa. The input tree must remain the same.
10. Write a member function double_tree that, for each node in a binary tree, it creates a new duplicate
node and insert it as the left child.
So the tree...
5
/ \
1 3
Is changed to...
5
/ \
5 3
/ /
1 3
/
1
11. Write a member function to delete all the leaves from a binary tree, leaving the root and intermediate
nodes in place. (Hint: Use a preorder traversal.)
12. Write a member function same_structure that returns true if the two passed tree are structurally the
same whatever the data inside them.
13. Write a member function has_path_sum that returns true if the tree contains any path from the root
to one of the leaves with the given sum.
14. Given a binary tree, write a member function that prints out all of its root-to-leaf paths, one per line.
15. The height of a tree is the maximum number of nodes on a path from the root to a leaf node. Write a
member function that returns the height of a binary tree.
16. A balanced binary tree is a tree in which all the following conditions are satisfied:
a. the left and right sub-trees heights differ by at most one level
b. the left sub-tree is balanced
c. the right sub-tree is balanced
Write a member function that checks if a given tree is balanced or not.
17. A full binary tree is a tree in which every node other than the leaves has two children.
Write a member function that checks whether a given tree is full or not.
18. Write a member function that determines whether a binary tree is complete.
19. Rewrite the binary tree pre-order, in-order and post-order traversal member functions using a
stack instead of recursion.
2. Write a member function that searches for a specific value in BST tree.
3. Write a member function that returns the minimum data value found in BST tree.
4. Write a member function that returns number of nodes less than a specific value in BST tree
5. Write a member function that returns a sub-tree that has nodes sum equal to an input value.