tutorial_2_without_solution
tutorial_2_without_solution
Contents
1 Exercises 2
1.1 Basic Operations on Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Exercise 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Tree Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Binary Search Trees (BST) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.1 Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.2 Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Heap Data Structure and Heap Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.1 Exercise 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.2 Exercise 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.3 Exercise 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Additional Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5.1 Exercise 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
1 Exercises
1.1 Basic Operations on Binary Trees
1.1.1 Exercise 0
Draw a diagram of a tree structure and label the following components:
• Root
• Edges(Branches)
• Leaves
• Nodes (including internal nodes, leaf nodes, parent, and child nodes)
• Levels
• Height
• Depth of nodes
• Node Degree
Explain the role of each component in the tree.
2
1.1.2 Exercise 1
Problem:
Implement functions in C to perform the following operations on a binary tree:
1. Define the Binary Tree Node: Define a structure for a binary tree node that includes:
• An integer value field to store data.
• Two pointers, one for the left child and one for the right child.
2. Create a Node: Write a function to create a new node with a given value.
3. Calculate the height and depth of the binary tree.
4. Count the total number of nodes in the binary tree.
3
1.2 Tree Traversals
1.2.1 Exercise 2
Problem:
Given the following binary tree:
B C
D E F
1. Pre-order traversal.
2. In-order traversal.
3. Post-order traversal.
4. Level-order traversal.
4
1.2.2 Exercise 3
• level-order insertion
• pre-order insertion
• post-order insertion
5
1.3 Binary Search Trees (BST)
1.3.1 Exercise 4
Problem:
Implement the following operations in a Binary Search Tree in C:
6
1.3.2 Exercise 5
Problem:
Given a Binary Search Tree, write a C function to convert it into a sorted doubly linked list in-place.
7
1.4 Heap Data Structure and Heap Sort
1.4.1 Exercise 7
Problem:
Implement the following operations for a Max Heap in C:
1.4.2 Exercise 8
Problem:
Use a Max Heap to sort an array of integers in ascending order using Heap Sort in C.
1.4.3 Exercise 9
Problem:
Given an array of n integers, find the k largest elements using a Max Heap in C.
8
1.5 Additional Exercises
1.5.1 Exercise 10
Problem:
Write a C function to verify if a binary tree is a Binary Search Tree (BST).