Exercises
Binary Tree
Do it together Exercises: BT
1. Consider the given tree. Identify the following information in the box on the right.
Root___________
The highest level = ___________
Height = ___________
Nodes that are leaves
____________________________________
Nodes that are parent
____________________________________
Nodes that have 1 child
____________________________________
Nodes in level 3
____________________________________
Balance factor of node 60 _____________
Balance factor of node 74 _____________
Is this tree balanced? Yes No
Is this tree complete? Yes No
Is this tree nearly complete? Yes No
2. Use stack to make an expression tree for 2 * 35 – 15 / 3 + 2
3. a) Construct a Huffman tree using word frequency for the word “BOOKKEEPER”
b) Determine the codes for letters B, E, and P.
c) What is the Huffman code for PEPPER using the Huffman tree obtained from a).
4. a) Traverse the given tree with a preorder traversal
b) Traverse the given tree in Depth first traversal fashion using Stack
c) Traverse the given tree in Breadth first traversal fashion using Queue
In Class Exercises: BT
1. Consider a tree below.
Root _________________
B C
maximum outdegree _________________
maximum indegree _________________ D E
leaf nodes _________________
H
parent nodes _________________ F G I
child nodes _________________
level of node K _________________ J K
height of this tree _________________
L
Find the balance factor of each node _________________
Is this tree balanced? Yes No
Is this tree complete? Yes No
Is this tree nearly complete? Yes No
2. Use Stacks to draw expression trees from the following expression
A + B – C / D % E+F
3. a) Contruct a Huffman tree using word frequency for the sentence “seashells_seashore”. Remark:
Treat an underscore(_) as a character.
b) Determine the code for letter s, e and a.
c) Construct a Huffman code from the Huffman tree for the word “horse”
d) decode the Huffman code 0100100
4. Consider the following tree, do the following.
a) Traverse this tree with DFT using Stack
b) Traverse this tree with BFT using Queue
c) Traverse this tree with preorder. Show tracing.
d) Traverse this tree with inorder. Show tracing.
e) Traverse this tree with postorder. Show tracing.
Self Review Exercises: BT
1. Recursively Traverse the given binary
tree using
a) Preorder traversal
b) Inorder traversal
c) Postorder traversal
2. Traverse this tree using
d) DFT with the help of Stack
e) BFT with the help of Queue
2. Construct the expression tree for the following infix expression 15 – 8 + 2 + 21 / 3
3. Given the character frequencies below.
Letters R O H S E A
Frequencies 5 7 10 15 20 45
a) Construct a Huffman Tree from the given data.
b) What are the Huffman code for letters R and A.
c) Using the tree you created in a), decode the message 00000001010011.
Exercises
Binary Search Tree
Do It Together Exercises-BST
1. Consider the following BST.
a) How many comparisons do you
need to do in order to find 46 in this
BST? _________________
How many comparisons do you
need to do in order to find 74 in this
BST? _________________
In general at the worst case, the
number of comparisons you need to
search for a node in a BST of size n
and height h is
__________________________
b) Insert 48 to this tree.
c) Delete 60 from this tree
2. Follow the instructions.
a) Create a binary search tree using the following data entered as a sequential set.
89 56 78 99 55 45 61 23 5 115
b) Draw a binary search tree when the root is removed from a binary search tree obtained from 2 a)
3. a. Update this BST after 70 is removed c. Update this BST after 74 is removed
from it.
b. Update this BST after 16 is removed
d. Update this BST after 53 is removed from
it. (Use Maxleft in case the node to be
deleted has two children.)
e. Insert 68 to the BST.
g. Insert 61 to the BST
f. Insert 50 to the BST.
h. Insert 25 to the BST
In Class Exercises-BST
1. Consider the following BST.
How many comparisons do you
need to do in order to find 8 in this
BST? _________________
How many comparisons do you
need to do in order to find 40 in this
BST? _________________
How many comparisons do you
need to do in order to find 36 in this
BST? _________________
2. Update this BST after 36 is removed from it.
3. Update this BST after 30 is removed from it.
4. Update this BST after 1, 5, and 20 are removed from it, respectively.
5. a) Create a binary search tree using the following data entered as a sequential set.
55 44 66 87 63 45 89 12 31 18 64
b) Draw a binary search tree when the root is removed from a binary search tree obtained from 5 a)
Self-Review Exercises-BST
1. Consider the following BST.
9 a) How many comparisons do you need to
do in order to find 15 in this BST?
4 18 _________________
1
10 21
b) How many comparisons do you need to
15 do in order to find 1 in this BST?
_________________
12
c) Update a BST after 1 is removed from the given BST.
4 18
1
10 21
15
12
c) Update a BST after 4 is removed from the given BST.
4 18
1
10 21
15
12
d) Update a BST after 9 is removed from the given BST.
4 18
1
10 21
15
12
e) Update a BST after 18 is removed from the given BST
4 18
1
10 21
15
12
f) Insert 13 to the BST.
4 18
1
10 21
15
12
g) Insert 8 to the BST.
4 18
1
10 21
15
12
2. a) Create a binary search tree using the following data entered as a sequential set.
14 23 7 10 33 56 80 66 70
b) Draw a tree when 14 is removed from a tree obtained from 2 a)
Exercises
Heap
Do-IT-Together Exercises: Heaps
1. Which of the structures in following trees are heaps
2. Apply the reheap up algorithm to element 45 that is newly added to the heap below.
3. Apply the reheap down algorithm to the element 15 in the below tree.
4. a) add 60 to the following heap. b) delete 40 from the following heap.
In class Exercises: Heaps
1. Make a heap tree of the following data read from the keyboard
23 7 92 6 12 14 40 44 20 21
2. a) Without drawing the tree structure of a heap array below, identify the left and right children of 32 and
27 and the parent of 11.
40 27 32 15 14 20 25 11
H[0] H[1] H[2] H[3] H[4] H[5] H[6] H[7]
b) Without drawing the tree structure of a heap array, show reheap up in the array when 38 is inserted to the
heap array in 2a)
40 27 32 15 14 20 25 11
H[0] H[1] H[2] H[3] H[4] H[5] H[6] H[7]
c) Without drawing the tree structure of a heap array, show reheap down in the array when 40 is deleted
to the heap array in 2a)
40 27 32 15 14 20 25 11
H[0] H[1] H[2] H[3] H[4] H[5] H[6] H[7]
Self-Review Exercises: Heaps
1. Make a heap tree of the following data read from the keyboard
80 50 46 3 17 13 78 96 19 99
2. a) Make a heap array of the following data read from the keyboard
45 75 94 5 14 66 47 23 3 18 78
b) From the heap array obtained from a), add 88 to the heap array and fix the structure so that it still
a heap array
c) From the heap array obtained from a), delete the root to the heap array and fix the structure so that
it still a heap array
Exercises
Graph
Do-It-Together Exercises: Graphs
a) Find MST using Prim Jarnik Algorithm. Work this algorithm on this graph.
b) Find MST using Kruskal Algorithm. Work this algorithm on this grap.
3. Find the shortest path from A to M using Dijkstra Algorithm
In Class Exercise: Graphs
A) Find minimum spanning tree using Prim’s Algorithm. Start at vertex A. Show steps by labeling a vertex
in the order that it’s included to the MST.
a) The weight of the spanning tree is ________
b) The sequence of edges that are included in the minimum spanning tree is
_____________________________________
B) Find minimum spanning tree using Kruskal’s Algorithm.
a) The weight of the spanning tree is ________
b) The sequence of edges that are included in the minimum spanning tree is
_____________________________________
C) Find the shortest path from A to F using Dijkstra’s Algorithm to. Show steps by labeling the previous
vertex .How much is the weight of the solution?
a) The weight of the shortest path is ___________
b) The sequence of edges that are included in the shortest path is _______________________
c) Write out the shortest path from A to F
________________________________________
Self-Reviewed Exercises: Graph
1. Find the shortest path from A to F using Dijkstra Algorithm.
2. Find the MST using Prim Jarnik algorithm
3. Find the MST using Kruskal algorithm
Exercises
AVL
Do-It-together Exercises: AVL
1. Construct an AVL using the following data entered as a sequential set. Apply appropriate rotations (show
steps) every time the AVL becomes unbalanced. Also write a balance factor at the each node after each
rotation.
14 23 7 10 33 56 80 66 70
2. Remove 14 from an AVL tree obtained from1 a). Fix the structure so that it remains an AVL.
In Class Exercises: AVL
1. Construct an AVL using the following data entered as a sequential set. Apply appropriate rotations (show
steps) every time the AVL becomes unbalanced. Also write a balance factor at the each node after each
rotation.
60 41 53 70 80 75 90 93 97
2. Delete 70 from an AVL tree obtained from1 a)
Self-Review Exercises: AVL
1. Construct an AVL using the following data entered as a sequential set. Apply appropriate rotations (show
steps) every time the AVL becomes unbalanced. Also write a balance factor at the each node after each
rotation.
60 41 53 70 80 75 90 93 97
2. Remove 75 from AVL from problem 1